Online store~40 min2s · 256 MBtarget O((n + q) log n) time, O(n) spacesegment-tree
A grocery app shows the prices of the slots along one shelf, from slot 0 to the last slot. A promotion can set every slot in a stretch to one flat price. The layout team often asks for the highest price in a stretch.
The two kinds of requests arrive mixed together, thousands of times a day. Your team lead asks: "Answer every question about the highest price, using the prices at that moment."
Write top_prices(prices, ops).
prices is the starting price of each slot.ops is the list of operations, in order. Each is [kind, left, right, value], and left ≤ right.kind is 0, set the price of every slot from left to right, both included, to value.kind is 1, ask for the highest price among the slots from left to right, both included. The value is 0.Input: prices = [4, 9, 2, 6, 5, 1], ops = [[1, 0, 5, 0], [0, 1, 3, 3], [1, 0, 3, 0], [1, 2, 5, 0], [0, 4, 5, 8], [1, 0, 5, 0]]
Output: [9, 4, 5, 8]Each question uses the prices at that moment. The table shows the slot prices after every operation.
| Operation | Slots after it | Answer |
|---|---|---|
| highest of slots 0 to 5 | [4, 9, 2, 6, 5, 1] | 9 |
| assign 3 to slots 1 to 3 | [4, 3, 3, 3, 5, 1] | - |
| highest of slots 0 to 3 | [4, 3, 3, 3, 5, 1] | 4 |
| highest of slots 2 to 5 | [4, 3, 3, 3, 5, 1] | 5 |
| assign 8 to slots 4 to 5 | [4, 3, 3, 3, 8, 8] | - |
| highest of slots 0 to 5 | [4, 3, 3, 3, 8, 8] | 8 |
Input: prices = [7], ops = [[1, 0, 0, 0]]
Output: [7]One slot and one question: the answer is its price.
Input: prices = [5, 5, 5], ops = [[0, 0, 2, 0], [1, 0, 2, 0]]
Output: [0]The assignment sets the price of every slot to 0, so the highest price is 0, not the old 5.
1 ≤ len(prices) ≤ 10,0000 ≤ prices[i] ≤ 10,000,0000 ≤ len(ops) ≤ 30,001, and each operation has exactly 4 numbers.0 ≤ left ≤ right < len(prices), and 0 ≤ value ≤ 10,000,000.Writing a price into every slot of a stretch, or scanning it, costs up to 10,000 steps for each operation. With 30,000 operations over most of the shelf, that is about 225,000,000 steps. A segment tree splits the shelf into halves, and an operation touches about 4 nodes in each of the 14 levels.