PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#PS0000139. Đếm đoạn có tổng bằng K (Subarray Sum Equals K)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Đếm đoạn có tổng bằng K (Subarray Sum Equals K)

Subarray Sum Equals K

Source: LeetCode

Version: Phuoc Hung OJ Extended

Problem Statement

Given an integer sequence a1,…,ana_1,\ldots,a_n and a target integer KK, count all nonempty contiguous intervals [l,r][l,r] such that 1≤l≤r≤n1\le l\le r\le n and ∑i=lrai=K\sum_{i=l}^{r}a_i=K. Different pairs of endpoints count as different intervals, even if their elements are identical. The array may contain negative numbers and zeros.

Input

  • Line 1: two integers nn and KK, the array length and the target sum.
  • Line 2: nn integers a1,…,ana_1,\ldots,a_n in order.

Output

Print one integer: the number of qualifying nonempty intervals. Print 0 if none exists.

Subtasks

  • Subtask 1 (20 points): 1≤n≤5001\le n\le 500; −1000≤ai≤1000-1000\le a_i\le 1000; −10000000≤K≤10000000-10000000\le K\le 10000000; 1≤i≤n1\le i\le n.
  • Subtask 2 (30 points): 1≤n≤200001\le n\le 20000; 0≤ai≤10000\le a_i\le 1000; −10000000≤K≤10000000-10000000\le K\le 10000000; 1≤i≤n1\le i\le n.
  • Subtask 3 (50 points): 1≤n≤200001\le n\le 20000; −1000≤ai≤1000-1000\le a_i\le 1000; −10000000≤K≤10000000-10000000\le K\le 10000000; 1≤i≤n1\le i\le n.

Example 1

Input

3 2
1 1 1

Output

2

Explanation

Intervals [1,2][1,2] and [2,3][2,3] both sum to 2. Singletons sum to 1 and the full array sums to 3.

Example 2

Input

3 3
1 2 3

Output

2

Explanation

The intervals [1,2][1,2] and [3,3][3,3] sum to 3; no other interval does.