fork download
  1. #include <iostream>
  2. #include<bits/stdc++.h>
  3. using namespace std;
  4.  
  5. template<typename K,typename V>
  6. struct PairHash{
  7. size_t operator()(const pair<K,V>&p) const {
  8. auto h1 = hash<K>{}(p.first);
  9. auto h2= hash<V>{}(p.second);
  10. return h1^h2;
  11. }
  12. };
  13.  
  14. int main() {
  15. int n ; cin>>n;
  16. vector<int>arr(n);
  17. for(int i = 0 ; i<n;i++) cin>>arr[i];
  18. // your code goes here
  19. vector<int>pre(n);
  20. pre[0] = arr[0];
  21. for(int i = 1; i<n;i++){
  22. pre[i]=pre[i-1]+arr[i];
  23. }
  24. int count = 0 ;
  25. unordered_map<pair<int,int>,int,PairHash<int,int>>hash;
  26. for(int j = 0 ; j<n;j++){
  27. count+=hash[{arr[j],pre[j]-2*arr[j]}];
  28. hash[{arr[j],pre[j]}]++;
  29. }
  30. cout<<count;
  31.  
  32. return 0;
  33. }
Success #stdin #stdout 0.01s 5320KB
stdin
7
8 1 6 5 2 3 5
stdout
1