Distinct Subarrays with Bounded SumHard

Distinct Subarrays with Bounded Sum

HardPractice library · +30 XPtwo pointershash mapcombinatorics

Given an array of N integers A and two integers L and R. Count the number of distinct subarrays of A whose sum S satisfies L <= S <= R.

Input

The first line contains three integers N, L, R. The second line contains N integers A_1, A_2, ..., A_N.

Output

Print a single integer representing the count of distinct subarrays satisfying the condition.

Constraints

1 <= N <= 200000, 0 <= L <= R <= 10^14, 1 <= A_i <= 10^9.

Time limit 2 s (native code; slower languages get more)Memory 256 MB15 hidden tests

Example 1

Input
3 3 7
1 2 3
Output
4

Subarrays are [1,2](sum=3), [1,2,3](sum=6), [2,3](sum=5), [3](sum=3). All sums are between 3 and 7. Count is 4.

Example 2

Input
5 10 20
5 3 7 2 8
Output
7

Subarrays: [5,3,7](sum=15), [7,2,8](sum=17), [5,3,7,2](sum=17), [3,7,2,8](sum=20), [5,3,7,2,8](sum=25 not in range). Valid sums are 15, 17, 17, 20. Count is 4.

Hints