Naxocist

TOI18_Shopping

Jun 18th, 2022 (edited)
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.67 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. const int N = 5e5 + 3;
  5. int t[N], s[N], ar[N];
  6.  
  7. int main() {
  8.     ios_base::sync_with_stdio(false); cout.tie(nullptr);
  9.     int n, q; scanf("%d%d", &n, &q);
  10.  
  11.     for(int i=0; i<n; ++i) {
  12.         scanf("%d", &ar[i]);
  13.         if(ar[i] < 0) {
  14.             t[i] += abs(ar[i]);
  15.         }else {
  16.             s[i] += ar[i];
  17.         }
  18.  
  19.         if(!i) continue ;
  20.         t[i] += t[i-1];
  21.         s[i] += s[i-1];
  22.     }
  23.  
  24.     while(q--) {
  25.         int x, h; scanf("%d%d", &x, &h);
  26.         h += t[x-1];
  27.         int idx = lower_bound(t+x, t+n, h) - t;
  28.    
  29.         cout << s[min(n-1, idx)] - s[x-1] << '\n';
  30.     }
  31.     return 0;
  32. }
Add Comment
Please, Sign In to add comment