Advertisement
Art_Uspen

Untitled

May 27th, 2021
756
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.46 KB | None | 0 0
  1. #include <vector>
  2. #include <cmath>
  3. #include<iostream>
  4.  
  5. using namespace std;
  6.  
  7. class Fenwick {
  8. private:
  9.     int64_t n_;
  10.     vector<int64_t> t;
  11. public:
  12.     explicit Fenwick(int64_t n) : n_(n) {
  13.         t.assign(n_, 0);
  14.     }
  15.  
  16.     explicit Fenwick(const vector<int64_t> &arr) : n_(arr.size()) {
  17.         t.assign(n_, 0);
  18.         for (size_t i = 0; i < arr.size(); i++) {
  19.             inc(i, arr[i]);
  20.         }
  21.     }
  22.  
  23.     void inc(int64_t i, int64_t delta) {
  24.         for (; i < n_; i = (i | (i + 1))) {
  25.             t[i] += delta;
  26.         }
  27.     }
  28.  
  29.     int64_t sum(int64_t r) {
  30.         int64_t result = 0;
  31.         for (; r >= 0; r = (r & (r + 1)) - 1) {
  32.             result += t[r];
  33.         }
  34.         return result;
  35.     }
  36.  
  37.     int64_t sum(int64_t l, int64_t r) {
  38.         return sum(r) - sum(l - 1);
  39.     }
  40. };
  41.  
  42. int main() {
  43.     int64_t N;
  44.     cin >> N;
  45.     vector<int64_t> arr;
  46.     int64_t value;
  47.     for (int i = 0; i < N; ++i) {
  48.         cin >> value;
  49.         arr.push_back(value);
  50.     }
  51.     Fenwick fenw(arr);
  52.     int64_t Q;
  53.     cin >> Q;
  54.     int64_t sum_ = 0;
  55.     for (int64_t i = 0; i < Q; ++i) {
  56.         int64_t T_i;
  57.         cin >> T_i;
  58.         if (T_i == 1) {
  59.             int64_t L_i, R_i;
  60.             cin >> L_i >> R_i;
  61.             sum_ = fenw.sum(L_i, R_i);
  62.             cout << sum_ << '\n';
  63.         } else {
  64.             int64_t I_i, X_i;
  65.             cin >> I_i >> X_i;
  66.             fenw.inc(I_i, X_i - arr[I_i]);
  67.         }
  68.     }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement