World-Famous Oil Magnate
Time limit1sMemory limit128 MB
Maintain tree heights under two operations, fertilize the C smallest trees whose height is at least H by raising each by 1, and answer range count queries, efficiently.
- Level
Hard8 of 10
- Topics
- Segment tree, Binary search, Simulation
- Solved
- No attempts yet
Problem
A world-famous oil magnate has a garden with N apple trees. The gardener, Hong Taeseok, performs only two kinds of work: fertilizing trees and reporting statistics about tree heights.
Each bottle of fertilizer has two values C_i and H_i. Applying fertilizer to a tree immediately increases its height by 1 cm. For such a bottle, the gardener considers all trees whose current height is at least H_i cm, chooses C_i distinct trees with the smallest heights among them, and fertilizes each chosen tree once. If fewer than C_i trees satisfy the height condition, he fertilizes all satisfying trees and discards the rest.
A statistics task asks for the number of trees whose heights lie in a given inclusive interval. Given the tasks in chronological order, write a program that answers every statistics task.
Input
The first line contains two integers N and M: the number of trees and the number of tasks.
The second line contains the initial heights of the N trees. Each height is an integer between 1 and N, inclusive.
Each of the next M lines describes one task in chronological order. Each task starts with a character T_i.
- If T_i is
F, two integers C_i and H_i follow. Among the trees whose heights are at least H_i cm, fertilize C_i distinct trees with the smallest heights. The same tree cannot be fertilized twice during one task. - If T_i is
C, two integers min_i and max_i follow. Count the trees whose height H satisfies min_i <= H <= max_i.
The constraints are:
- 1 <= N, M <= 100,000
- 1 <= C_i <= N
- 0 <= H_i <= 1,000,000,000
- 1 <= min_i <= max_i <= 1,000,000,000
Output
For each task whose type is C, print the number of trees in the requested height interval on its own line.