You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
28 lines
550 B
28 lines
550 B
#include <bits/stdc++.h>
|
|
using namespace std;
|
|
|
|
int subarraySum(vector<int>& nums, int k) {
|
|
unordered_map<int, int> mp;
|
|
mp[0] = 1;
|
|
int count = 0, pre = 0;
|
|
for (auto& x:nums) {
|
|
pre += x;
|
|
if (mp.find(pre - k) != mp.end()) {
|
|
count += mp[pre - k];
|
|
}
|
|
mp[pre]++;
|
|
}
|
|
return count;
|
|
}
|
|
int main() {
|
|
int n;
|
|
cin >> n;
|
|
vector<int> nums(n);
|
|
for(int i = 0; i < n; i++){
|
|
cin >> nums[i];
|
|
}
|
|
int k;
|
|
cin >> k;
|
|
cout << subarraySum(nums, k) << endl;
|
|
return 0;
|
|
}
|