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;m[0]=1;
  6. for(int i=0;i<pre.size();i++){
  7. if(pre[i]==k)a++;
  8. if(m.find(pre[i]-k)!=m.end())a+=m[pre[i]-k];
  9. m[pre[i]]++;
  10. }
  11. return a;
  12. }
  13. int main() {
  14. // your code goes here
  15. int n;cin>>n;vector<int>pre(n);
  16. for(int i=0;i<n;i++){
  17. int e;cin>>e;
  18. if(i==0){
  19. pre[i]=e;
  20. }else{
  21. pre[i]=pre[i-1]+e;
  22. }
  23. }
  24. int k;cin>>k;
  25. cout<<ans(pre,k)<<endl;
  26. return 0;
  27. }
Success #stdin #stdout 0s 5320KB
stdin
5
1 0 1 2 3 
3
stdout
3