Advertisement
cosenza987

Untitled

Jul 2nd, 2021
1,292
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.24 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. int main() {
  6.     ios_base::sync_with_stdio(false);
  7.     cin.tie(0);
  8.     int n, k;
  9.     long long ans = 0;
  10.     cin >> n >> k;
  11.     vector<int> vv(n);
  12.     for(int i = 0; i < n; i++) {
  13.         cin >> vv[i];
  14.     }
  15.     vector<int> v;
  16.     //v.push_back(0);
  17.     partial_sum(vv.begin(), vv.end(), back_inserter(v));
  18.     for(auto itr = v.begin(); itr != v.end(); itr++) {
  19.         if(itr == v.begin()) {
  20.             auto l = lower_bound(itr, v.end(), k);
  21.             auto r = upper_bound(itr, v.end(), k);
  22.             if(*(r - 1) == k and *l == k) {
  23.                 ans += r - l;
  24.                 //cout << *l << " " << *r << " " << *itr << " " << k + *itr << " " << r - l << endl;
  25.             }
  26.         } else {
  27.             auto l = lower_bound(itr, v.end(), k + *itr);
  28.             auto r = upper_bound(itr, v.end(), k + *itr);
  29.             if(*(r - 1) == *itr and *(r - 1) - *(l - 1) != k) {
  30.                 continue;
  31.             }
  32.             if(*(r - 1) - *itr == k and *l - *itr == k) {
  33.                 ans += r - l;
  34.                 //cout << *l << " " << *r << " " << *itr << " " << k + *itr << " " << r - l << endl;
  35.             }
  36.         }
  37.     }
  38.     cout << ans << "\n";
  39.     return 0;
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement