nrk009

SEG TREE SUM RANGE SINGLE UPDATE

Jul 25th, 2019
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.56 KB | None | 0 0
  1.  
  2.     /*If you are slow ,try hard and become fast
  3.         If you are making mistakes , learn from them and correct them
  4.             If you are weak in something , do practice and become good at it
  5.     */
  6. /* Don't do just for the sake ... learn from it , that is more important */
  7.  
  8. //Don't Think Just Do It!!
  9.  
  10. #include <bits/stdc++.h>
  11. using namespace std;
  12. #define fast ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL)
  13. #define ll long long
  14. #define ld long double
  15. #define pb push_back
  16. #define fe first
  17. #define se second
  18. #define st(x) sort(x.begin(),x.end())
  19. #define rst(x) sort(x.rbegin(), x.rend())
  20. #define all(x) x.begin(),x.end()
  21. long double pi = 3.14159265358979323;
  22.  
  23. const double EPS = 1e-12;
  24. const int N = 1e5 + 5;
  25. const int mod = 1e9 + 7;
  26.  
  27.  
  28. // segment tree
  29. ll tree[N << 2];
  30. ll a[N];
  31. void build(ll l , ll r , ll node)
  32. {
  33.     if(l == r){
  34.    
  35.         tree[node] = a[l];
  36.    
  37.         return ;
  38.     }
  39.     ll mid = ( l + r) >> 1;
  40.  
  41.     build(l,mid,node*2);
  42.     build(mid + 1 , r , node*2 + 1);
  43.     tree[node] = tree[node*2] + tree[node*2 + 1];
  44.  
  45. }
  46.  
  47. ll query(ll l , ll r ,ll node,ll ql , ll qr )
  48. {
  49.     if(r < ql || l > qr)//if queury is out of the range...
  50.         return 0 ;
  51.     if(l >= ql && r <= qr)//important condition like if our array is of size of 8 and we are
  52.                          //quering for 1 - 6 then in second iteration for block 1 - 4;
  53.          return tree[node];// // range represented by a node is completely inside the given range
  54.     ll mid = (l + r) >> 1;
  55.     ll s1 = query(l , mid ,node << 1, ql , qr );
  56.     ll s2 = query(mid + 1 , r ,(node << 1) + 1,ql , qr );
  57.     //learn very important note do NOT use node << 1 + 1 it will result in (node << 2)
  58.    
  59.     return s1 + s2;
  60.  
  61.  
  62. }
  63.  
  64. void update(ll l , ll r ,ll node,ll idx,ll val)
  65. {
  66.  
  67.     if(l == r){
  68.         if(l == idx){
  69.             tree[node] += val;
  70.             a[l] += val;
  71.         }
  72.         return ;
  73.     }
  74.     else{
  75.     ll mid = (l + r) >> 1;
  76.     if(idx <= mid)
  77.     update(l , mid  ,node << 1,idx,val);
  78.     else
  79.     update(mid + 1 , r ,(node << 1)  + 1,idx,val);
  80.     tree[node] = tree[node << 1] + tree[(node << 1) + 1];
  81. }
  82.  
  83. }
  84. int main()
  85. {
  86.     fast;
  87.     ll n;
  88.     cin >> n;
  89.    
  90.     for(int i = 1 ; i <= n;  i++ )
  91.         cin >> a[i];
  92.     build(1,n,1);
  93.    
  94.     ll q;
  95.     cin >> q;
  96.  
  97.     while(q--)
  98.     {
  99.         ll c;
  100.         cin >> c;
  101.         if(!c){
  102.             ll ql , qr;
  103.             cin >> ql >> qr;
  104.        
  105.             cout << query(1,n,1,ql,qr) << "\n";
  106.         }
  107.         else
  108.         {
  109.             ll idx,val;
  110.             cin >> idx >> val;
  111.             update(1,n,1,idx,val);
  112.         }
  113.     }
  114.  
  115.  
  116.  
  117. //time complexity
  118. //build O(n)
  119. //query O(logn)
  120. //update O(logn)
  121. //space complexity O(n)
  122.  
  123.  
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.  
  137.  
  138.  
  139.  
  140.  
  141.    
  142.     return 0;
  143.    
  144. }
Advertisement
Add Comment
Please, Sign In to add comment