Advertisement
cosenza987

Untitled

Jul 17th, 2021
1,174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.03 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<long long> vv(n);
  12.     for(int i = 0; i < n; i++) {
  13.         cin >> vv[i];
  14.     }
  15.     vector<long long> v;
  16.     partial_sum(vv.begin(), vv.end(), back_inserter(v));
  17.     for(auto itr = v.begin(); itr != v.end(); itr++) {
  18.         if(itr == v.begin()) {
  19.             auto l = lower_bound(itr, v.end(), k);
  20.             auto r = upper_bound(itr, v.end(), k);
  21.             if(*(r - 1) == k and *l == k) {
  22.                 ans += r - l;
  23.             }
  24.         } else {
  25.             auto l = lower_bound(itr, v.end(), k + *itr);
  26.             auto r = upper_bound(itr, v.end(), k + *itr);
  27.             if(*(r - 1) == *itr and *(r - 1) - *(l - 1) != k) {
  28.                 continue;
  29.             }
  30.             if(*(r - 1) - *itr == k and *l - *itr == k) {
  31.                 ans += r - l;
  32.             }
  33.         }
  34.     }
  35.     cout << ans << "\n";
  36.     return 0;
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement