-
Notifications
You must be signed in to change notification settings - Fork 1
/
BOJ2015.java
35 lines (31 loc) · 1.14 KB
/
BOJ2015.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import java.io.*;
import java.util.*;
public class BOJ2015 {
public static void main(String[] args) throws IOException {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(bf.readLine());
HashMap<Integer, Integer> numSums = new HashMap<>();
long result = 0L;
int n = Integer.parseInt(st.nextToken());
int k = Integer.parseInt(st.nextToken());
int[] nums = new int[n + 1];
nums[0] = 0;
st = new StringTokenizer(bf.readLine());
for (int i = 1; i <= n; i++) {
nums[i] = Integer.parseInt(st.nextToken());
}
int[] prefixSum = new int[n + 1];
prefixSum[0] = 0;
for (int i = 1; i <= n; i++) {
prefixSum[i] = prefixSum[i - 1] + nums[i];
}
numSums.put(0, 1);
for (int i = 1; i <= n; i++) {
result += (long) numSums.getOrDefault(prefixSum[i] - k, 0);
int temp = numSums.getOrDefault(prefixSum[i], 0) + 1;
numSums.put(prefixSum[i], temp);
}
bf.close();
System.out.println(result);
}
}