fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. main(){
  5. int n,k,cnt=0;
  6. cin>>n>>k;
  7. vector<int>v(n),sum(n+1);
  8. unordered_map<int,int>mp;
  9. sum[0]=0;
  10. for(int i=0;i<n;i++){
  11. cin>>v[i];
  12. sum[i+1]=sum[i]+v[i];
  13. if(mp.find(sum[i+1]-k)!=mp.end()) cnt+=mp[sum[i+1]-k];
  14. if(mp.find(k-sum[i+1])!=mp.end())cnt+=mp[k-sum[i+1]];
  15. mp[sum[i+1]]++;
  16. }
  17. cout<<cnt<<"\n";
  18. }
Success #stdin #stdout 0s 5320KB
stdin
6 5
1 4 -2 2 -1 6
stdout
3