Advertisement
matheus_monteiro

BIT - Gemp

Jun 26th, 2021
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.72 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. const int MAX = 300000;
  4.  
  5. int n, q, BIT[MAX], arr[MAX];
  6.  
  7. int query(int x) {
  8.    int s = 0;
  9.    while(x) s += BIT[x], x -= x&-x;
  10.    return s;
  11. }
  12. void update(int x, int value) {
  13.    while(x <= n) BIT[x] += value, x += x&-x;
  14. }
  15.  
  16. int32_t main() {
  17.     ios_base::sync_with_stdio(false);
  18.     cin.tie(nullptr);
  19.  
  20.    cin >> n;
  21.    for(int i = 1; i <= n; i++) {
  22.       cin >> arr[i];
  23.       update(i, arr[i]);
  24.    }
  25.    cin >> q;
  26.    while(q--) {
  27.       int o, a, b;
  28.       cin >> o >> a >> b;
  29.       if(o == 1) {
  30.          cout << query(b) - query(a - 1) << endl;
  31.       } else {
  32.          update(a, -arr[a]);
  33.          arr[a] = b;
  34.          update(a, arr[a]);
  35.       }
  36.    }
  37.     return 0;
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement