Розбір
Let's implement a sliding window using two pointers, and . For each fixed , expand the interval as much as possible so that the sum of its elements does not exceed . In other words, for each , search for the largest possible such that the sum of elements in the interval does not exceed , while the sum of elements in the interval exceeds .
Let be the sum of numbers in the interval . If , expand the interval to . Otherwise, shrink it to . Count the number of intervals with a sum of .
Example
Let's examine the movement of the pointers for the given example.
, moves forward until the sum of the numbers in the interval exceeds . We stop at the interval , since the sum of the numbers in it is , while in the interval , the sum of the numbers is already .
, moves forward to . The sum of the numbers in the interval is , while in the interval , it is already .
, the considered interval is . The sum of the numbers in it is , but we cannot move the index forward because the sum of the numbers in the interval is , which is greater than .
, the considered interval is . The sum of the numbers in it is , but we cannot move the index forward because the sum of the numbers in the interval is , which is greater than .
, the considered interval is . The sum of the numbers in it is .
The number of subarrays with a sum of is . They start at indices , and .
Algorithm realization
Declare an array.
#define MAX 200001 long long a[MAX];
Read the input data.
scanf("%d %lld", &n, &x); for (i = 0; i < n; i++) scanf("%lld", &a[i]);
Initially, set the current interval . The sum of the numbers in this interval is .
s = j = 0;
For each value of , sequentially process the intervals .
for (i = 0; i < n; i++) {
For the fixed left end of the interval , search for the largest such that the sum of elements in this interval does not exceed .
while ((j < n) && (s + a[j] <= x)) s += a[j++];
If the sum of the numbers in the current interval equals , increase the desired subarray count by .
if (s == x) cnt++;
Recompute the sum for the interval .
s -= a[i]; }
Print the answer.
printf("%lld\n", cnt);