Weather Forecast
Time limit2sMemory limit128 MB
Maintain N snow depths under point increments and decrements, then answer range-count queries (depth between L and R) and T-th largest value queries online.
- Level
Hard8 of 10
- Topics
- Segment tree, Binary search, Sorting, Combinatorics
- Solved
- No attempts yet
Problem
Snow falls all day long in Snowflake Country. It keeps piling up, and it keeps melting.
Taekhee, the weather caster of Snowflake Country, talks about nothing but snow. Earthquake or typhoon, the forecast always has the same shape.
Snow now measures between mm and mm in A, B, C, ..., X and regions in total.
An administrative reform split the country into regions, and reading out region names one by one became too much work. So Taekhee decided to report only the count of regions whose snow depth is between mm and mm. That number alone says nothing about any single region, so Taekhee also reports the depth of the region with the -th deepest snow. With so many regions, doing this by hand is hopeless. Taekhee wants a program that tracks the snow depths automatically.
The program handles four operations.
- Region gains mm of snow.
- Region loses mm of snow to melting.
- Count the regions whose snow depth is at least mm and at most mm.
- Report the snow depth of the region with the -th deepest snow.
Operation 4 counts equal depths separately. For example, if five regions hold , then for equal to 1, 2, 3, 4, 5 the answers are 3, 2, 2, 1, 1 in that order.
A forecast has to be fast, so the program has to be fast. Write it for Taekhee.
Input
The first line contains the number of regions and the number of commands . ()
The second line contains the current snow depths . ()
Each of the next lines contains one command in one of the following four forms.
1 i x: region gains mm of snow. (, )2 i y: region loses mm of snow. (, )3 L R: count the regions whose snow depth is at least mm and at most mm. ()4 T: report the snow depth of the region with the -th deepest snow. ()
No command of type 2 ever makes a snow depth negative.
Output
For each command of type 3 and each command of type 4, print the answer as one integer on its own line.