#CT0000145. Đoạn có tổng cho trước (Subarrays with a Given Sum)
Bài tập này chưa được xếp vào lộ trình.
Mục lục đề ⌄
Đoạn có tổng cho trước (Subarrays with a Given Sum)
Subarrays with a Given Sum
Version: Phuoc Hung OJ Extended
Problem Statement

An observation station records an integer change over consecutive days. The change on day is : positive means an increase, negative means a decrease, and zero means no change.
For , the sum of the contiguous interval is . Intervals with different endpoint pairs count as different intervals, even if their elements have identical values.
Given an integer , count the nonempty contiguous intervals whose sum is exactly .
Input
The first line contains two integers and .
The second line contains exactly integers , separated by spaces.
Output
Print one integer: the number of contiguous intervals with sum . The answer may exceed the range of a signed -bit integer.
Subtasks
- Subtask 1 (30 points): ; ; for every .
- Subtask 2 (30 points): ; ; for every .
- Subtask 3 (40 points): ; ; for every .
Examples
Example 1
Input
5 3
1 2 1 -1 2
Output
3
Explanation
The array is . The valid intervals are (), (), and (). Thus the answer is .
Example 2
Input
6 0
1 -1 2 -2 3 -3
Output
6
Explanation
Prefix sums at positions through are . The value occurs at four positions, . Choosing any two produces a zero-sum interval, so there are : , , , , , .
Example 3
Input
4 10
-1 2 -3 4
Output
0
Explanation
The only positive values are and , whose sum is . No contiguous interval can reach a sum of , so the answer is .
Liên quan
Trong các cuộc thi sau: