Advertisement
pankovamg

DO

Oct 13th, 2022 (edited)
704
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.75 KB | None | 0 0
  1. //#define _GLIBCXX_DEBUG
  2. //#include <bits/stdc++.h>
  3. //dd_definitions("-D_GLIBCXX_DEBUG")
  4.  
  5. #include <iostream>
  6. #include <sstream>
  7. #include <algorithm>
  8. #include <vector>
  9. #include <deque>
  10. #include <queue>
  11. #include <string>
  12. #include <set>
  13. #include <cmath>
  14. #include <tuple>
  15. #include <map>
  16. #include <random>
  17. #define int long long
  18.  
  19.  using namespace std;
  20.  
  21. typedef long long ll;
  22. typedef long double ld;
  23.  
  24. const int inf = 1e9 + 7;
  25. const int mod = 1e9 + 7;
  26.  
  27. vector <int> tree, a;
  28.  
  29. int f(int x, int y){
  30.     return x + y;
  31. }
  32.  
  33. int n, k, sz=1;
  34.  
  35. void build(int i = 0, int l = 0, int r = sz){
  36.     if (r == l + 1) tree[i] = a[l];
  37.     else{
  38.         int m = (r + l) / 2;
  39.         build(2 * i + 1, l, m);
  40.         build(2 * i + 2, m, r);
  41.         tree[i] = f(tree[2 * i + 1], tree[2 * i + 2]);
  42.     }
  43. }
  44.  
  45. int get(int ql, int qr, int v = 0, int vl = 0, int vr = sz){
  46.     if(ql <= vl && vr <= qr) return tree[v];
  47.     if(qr <= vl || vr <= ql) return 0;
  48.  
  49.     int m = (vr + vl) / 2;
  50.     int v1 = get(ql, qr, 2* v + 1, vl, m);
  51.     int v2 = get(ql, qr, 2* v + 2, m, vr);
  52.     return f(v1, v2);
  53. }
  54.  
  55. void update(int q_i, int val, int v = 0, int vl = 0, int vr = sz){
  56.     if(q_i < vl || q_i >= vr) return;
  57.     if(vr == vl + 1) tree[v] = val;
  58.     else{
  59.         int m = (vr + vl) / 2;
  60.         update(q_i, val, 2 * v + 1, vl, m);
  61.         update(q_i, val, 2 * v + 2, m, vr);
  62.         tree[v] = f(tree[2 * v + 1], tree[2 * v + 2]);
  63.     }
  64. }
  65.  
  66. signed main(){
  67.     cin >> n;
  68.     a.resize(n, 0);
  69.  
  70.     while (sz < n){
  71.         sz = 2 * sz;
  72.     }
  73.  
  74.    
  75.     a.resize(sz, 0);
  76.     tree.resize(2 * sz - 1, 0);
  77.  
  78.     for (int i = 0; i < n ;i++){
  79.         cin >> a[i];
  80.     }
  81.  
  82.     build();
  83.     cout << get(1, 3) <<'\n';
  84.     update(2, 100);
  85.     cout << get(2, 3) <<'\n';
  86.  
  87.  
  88.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement