Josif_tepe

Untitled

Sep 6th, 2026
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.87 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <vector>
  4. #include <algorithm>
  5. #include <cstring>
  6. #include <map>
  7. using namespace std;
  8. typedef long long ll;
  9. const int maxn = 1e5 + 10;
  10.  
  11. int main() {
  12.     ios_base::sync_with_stdio(false);
  13.     int n;
  14.     cin >> n;
  15.    
  16.     vector<ll> v(n + 1);
  17.     for(int i = 1; i <= n; i++) {
  18.         cin >> v[i];
  19.     }
  20.     vector<ll> pref_sum(n + 1, 0);
  21.     map<ll, ll> freq;
  22.    
  23.     freq[0]++;
  24.     ll sum = 0;
  25.     for(int i = 1; i <= n; i++) {
  26.         if(i % 2 == 1) {
  27.             sum += v[i];
  28.         }
  29.         else {
  30.             sum -= v[i];
  31.         }
  32.         pref_sum[i] = sum;
  33.         freq[sum]++;
  34.     }
  35.    
  36.     ll res = 0;
  37.     for(pair<ll, ll> p: freq) {
  38.         if(p.second >= 2) {
  39.             res += (p.second * (p.second - 1)) / 2;
  40.         }
  41.     }
  42.     cout << res << endl;
  43.    
  44.    
  45.     return 0;
  46. }
  47.  
Advertisement
Add Comment
Please, Sign In to add comment