Problem 504
Number of Subarrays with Bounded Maximum
Medium · 20 pts · Array, Two Pointers
Problem
Count the non-empty blocks of consecutive elements whose maximum value lies between L and R inclusive. Blocks at different positions count separately. A neat way: count the blocks whose maximum is at most R, and subtract the blocks whose maximum is at most L - 1; each of those counts comes from the lengths of runs of small enough elements.
Input
The first line contains N, L and R. The second line contains N integers.
Output
Print the number of qualifying subarrays.
Constraints
1 <= N <= 100000; 0 <= A[i] <= 1000000000; 0 <= L <= R <= 1000000000
Example input
4 2 3 2 1 4 3
Example output
3
Explanation
In 2 1 4 3 with L = 2 and R = 3, the qualifying blocks are [2], [2 1] and [3]. Any block containing 4 has a maximum that is too large, so the answer is 3.