Flooding
InterviewTime limit2sMemory limit1024 MB
Given heights and queries (l, r, x), count the maximal runs of heights strictly above x that lie within positions l to r.
- Level
Medium5 of 10
- Topics
- Array, Sorting, Prefix sum
- Solved
- No attempts yet
Problem
It is winter, and it has never been colder. Mr. Malnar looks at his photos from his last cruise on the Adriatic and remembers unforgettable moments. On the TV in the background, the news reports the latest proposals for slowing sea level rise. Looking at his coastal photos, Mr. Malnar wonders what they would have looked like if the sea had risen by a certain amount. There are so many pictures and so many questions that he asks for your help.
We model the coast as a sequence of numbers , where is the relief height at the -th point. Mr. Malnar has queries. The -th query asks: if the sea level rose by meters, how many islands would there be between the -th and -th points?

An island is a maximal interval in which every is strictly greater than the sea level. A maximal interval cannot be extended in either direction while keeping that condition. The sea level starts at meters.
Input
The first line contains integers and (), the length of the sequence and the number of queries.
The second line contains integers () that describe the relief of the coast.
Each of the next lines contains three integers , and (, ) that describe the -th query.
Output
Print lines. The -th line contains the answer to the -th query.
Hint
In the first query of the first sample, the islands are the intervals and (left image). The second query has one island, . The third query has no islands because everything is under water.
In the second sample, the first query has islands and . The second query (right image) has islands , , and . The third query has islands , and .