Road Construction
InterviewTime limit1sMemory limit128 MB
Given a permutation, for each query [l,r] reverse that segment and report the number of maximal increasing runs in the resulting array.
- Level
Medium7 of 10
- Topics
- Array, Math, Implementation, Prefix sum
- Solved
- No attempts yet
Problem
Namgyu likes to drink and drinks every day. His drunken habit is an unusual one: road construction.
The road Namgyu walks home after drinking is one way and only goes forward, and it splits into sections whose heights are all different. The heights he meets on the way home can be written down in order as integers.
On every day he drinks, Namgyu picks one part of the road on his way home, a start position and an end position, and rebuilds it so that the heights between them come out in reverse order. Suppose a road of length 5 has heights 1 2 3 4 5 in order. Rebuilding the part that starts at position 2 and ends at position 4 turns the heights into 1 4 3 2 5.
Namgyu has a conscience, so the day after he drinks he gets up, walks back to the part he rebuilt, and restores it to its original shape.
Jaehyuk drinks with Namgyu every day and has watched all of this from the side. He wants to know how many uphill roads the whole road has each time the road changes. Count the uphill roads for him.
An uphill road is a run of consecutive positions whose heights keep increasing. If the height at position is greater than the height at position , the two positions belong to the same uphill road. Every uphill road is taken as long as it can be, so each position falls into exactly one uphill road, and a run of a single position counts as one uphill road.
Input
The first line contains the length of the road and the number of days Jaehyuk watched, . ()
The second line contains integers , the height of each position. ()
Each of the next lines contains and , the part rebuilt on day . ()
Positions are numbered from 1, and no two positions have the same height.
Output
Print lines. On line , print the number of uphill roads on the whole road after the construction of day .
The construction does not pile up. Each day's construction applies to the original road independently.