Desperate Fire Survive
Time limit3sMemory limit256 MB
For each query [l, r, k], count the sub-segments that can be merged and trimmed into one node of level exactly k.
- Level
Hard9 of 10
- Topics
- Segment tree, Greedy, Divide and conquer
- Solved
- No attempts yet
Problem
Rikka is running with her every cell to the hall where the contest will be held.
Well, EC Final is growing bigger and bigger. More and more computers are connected to the weak 2000W main wires, and heat builds up in every row.
When Rikka enters the hall, she finds LCR there rearranging the wires with the volunteers. However, the girl might not be able to do anything helpful but observe.
"Listen. We have no time to calculate parameters for the new circuit. Are you good at data structures? Help us, please..."
The circuitry is a sequence of nodes, where there are possible levels of nodes in total, numbered from to . A -level node has a power limit twice that of a -level node, so Rikka can merge two adjacent -level nodes into a -level node if . She can also remove any node at any time from the circuitry, and the order of the remaining nodes stays the same.
The volunteers have queries in total. Each query gives a segment and an integer level . Rikka needs to count how many sub-segments of the given segment (that is, segments with ) can provide a -level node. A sub-segment provides a -level node if it is possible to turn the sequence into a single -level node by merging adjacent nodes at the same level and removing nodes. These two operations can be performed any number of times in any order. The level must be exactly , not higher or lower.
Input
The first line contains three integers , , (), the length of the circuitry sequence, the maximal level, and the number of queries.
The second line contains integers (), the levels of the nodes in order.
Each of the following lines contains three integers , , (, ), describing a query. The integers in a line are separated by spaces.
Output
Output lines. Each line contains one integer, the answer to the corresponding query.