Street Lamps
Time limit4sMemory limit1024 MB
Given N lamps with heights and Q single-lamp height updates, count after each update the pairs of equal-height lamps where every lamp between them is strictly shorter.
- Level
Hard8 of 10
- Topics
- Segment tree, Array
- Solved
- No attempts yet
Problem
Along a straight road, street lamps are installed. The initial height of the -th lamp is (). You want to string electric wires between the lamps.
To hang a wire between lamp and lamp (where ), both of the following conditions must hold.
- (the two lamps have the same height).
- For every with , (every lamp between them is shorter than both).
The administrator changes the heights of some lamps, and each change affects which wires can be hung. There are height changes in total. Each change sets the height of the -th lamp to . After each change, count the pairs of lamps between which a wire can be hung.
Input
The first line contains two integers and (, ).
The second line contains integers ().
Each of the next lines contains two integers and (, ), meaning is set to . The height of lamp just before the change is guaranteed to differ from .
Output
Print the number of wires that can be hung in the initial state on the first line.
Then print, on each of the next lines, the number of wires that can be hung after the corresponding height change.