fork download
  1. #include<bits/stdc++.h>
  2. #define ull unsigned long long
  3. #define ll long long
  4. #define all(x) x.begin(), x.end()
  5. using namespace std;
  6. const int maxn = 1e6 + 5;
  7. map<int, vector<int>> mp;
  8. int T[maxn];
  9. int main() {
  10. ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  11. int n, x;
  12. cin >> n;
  13. mp[0].push_back(0);
  14. cout<<T[0]<<" ";
  15. for (int i = 1; i <= n; ++i) {
  16. cin >> x;
  17. T[i] = T[i - 1] + x;
  18. mp[T[i]].push_back(i);
  19. cout<<T[i]<<" ";
  20. }
  21. int res = 0;
  22. cout<<endl;
  23. for (int i = 1; i < n; ++i) {
  24. auto it = lower_bound(mp[T[i + 1]].begin(), mp[T[i + 1]].end(), i + 1) - mp[T[i + 1]].begin();
  25. auto it2 = lower_bound(mp[T[i]].begin(), mp[T[i]].end(), i) - mp[T[i]].begin();
  26. res += (it * (mp[T[i]].size() - it2 - 1));
  27. }
  28. cout << res;
  29.  
  30. }
Success #stdin #stdout 0s 5272KB
stdin
Standard input is empty
stdout
0 0 0 
2