fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int ans(vector<int>&pre,int k){
  5. int a=0;unordered_map<int,int>m;
  6. for(int i=0;i<pre.size();i++){
  7. if(m.find(pre[i]-k)!=m.end())a+=m[pre[i]-k];
  8. m[pre[i]]++;
  9. }
  10. return a;
  11. }
  12. int main() {
  13. // your code goes here
  14. int n;cin>>n;vector<int>pre(n);
  15. for(int i=0;i<n;i++){
  16. int e;cin>>e;
  17. if(i==0){
  18. pre[i]=e;
  19. }else{
  20. pre[i]=pre[i-1]+e;
  21. }
  22. }
  23. int k;cin>>k;
  24. cout<<ans(pre,k)<<endl;
  25. return 0;
  26. }
Success #stdin #stdout 0s 5316KB
stdin
6
1 0 1 2 4 5 
3
stdout
2