Pearlfromsu

own segment tree

Jun 5th, 2023
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.41 KB | None | 0 0
  1. /******************************************************************************
  2.  
  3.                               Online C++ Compiler.
  4.                Code, Compile, Run and Debug C++ program online.
  5. Write your code in this editor and press "Run" button to compile and execute it.
  6.  
  7. *******************************************************************************/
  8.  
  9. #include <iostream>
  10. #include <vector>
  11.  
  12. using namespace std;
  13.  
  14. vector<int> tree;
  15.  
  16. int getSum(int q, int tl, int tr, int l, int r) {
  17.     if(tl >= l && tr <= r)
  18.         return tree[q];
  19.     if(tl >= r || tr <= l)
  20.         return 0;
  21.    
  22.     int mid = (tl + tr)/2;
  23.     int getl = getSum(q*2, tl, mid, l, r);
  24.     int getr = getSum(q*2+1, mid, tr, l, r);
  25.     cout << "getl: [" << tl << ", " << mid << "]: " << getl << " (" << q*2 << ") tree[q*2]: " << tree[q*2] << '\n';
  26.     cout << "getr: [" << mid << ", " << tr << "]: " << getr << " (" << (q*2+1) << ") tree[q*2+1]: " << tree[q*2+1] << '\n';
  27.     return getl + getr;
  28. }
  29. int main()
  30. {
  31.     int n;
  32.     cin >> n;
  33.         //12:46
  34.     int k = 1;
  35.     while(k < n)
  36.         k *= 2;
  37.     cout << "k is " << k << '\n';
  38.     tree.resize(8*n, 0);
  39.    
  40.     for(int i = 0; i < n; i++) {
  41.         cin >> tree[k+i];
  42.         int uk1 = k+i;
  43.         while(uk1 != 1) {
  44.             tree[uk1/2] = tree[(uk1/2)*2] + tree[(uk1/2)*2 + 1];
  45.             uk1/=2;
  46.         }
  47.     }
  48.     int kkk;
  49.     cin >> kkk;
  50.     for(int i = 0; i < kkk; i++) {
  51.         char ch;
  52.         int a, b;
  53.         cin >> ch;
  54.         if(ch == '+') {
  55.             cin >> a >> b;
  56.             int uk1 = k+a;
  57.             tree[uk1] += b;
  58.             while(uk1 != 1) {
  59.                 tree[uk1/2] = tree[(uk1/2)*2] + tree[(uk1/2)*2+1];
  60.                 uk1/=2;
  61.             }
  62.         }
  63.        
  64.         if(ch == '?') {
  65.             cin >> a >> b;
  66.             cout << "[" << a << ", " << b << "]: ";
  67.             cout << getSum(1, 0, k, a, b) << '\n';
  68.         }
  69.        
  70.        
  71.         for(int j = 0; j < n; j++)
  72.             cout << tree[k+j] << ' ';
  73.         cout << '\n';
  74.     }
  75.    
  76.  
  77.     cout << "k is " << k << '\n';
  78.     for(int j = 0; j < k+n; j++)
  79.        cout << j << ' ';
  80.     cout << '\n';
  81.     for(int j = 0; j < k+n; j++)
  82.        cout << tree[j] << ' ';
  83. /*
  84. 0  1  2  3 4 5 6 7 8 9 10 11 12
  85. 35 15 10 5 3 7 5 0 1 2 3  4  5
  86.  
  87.  
  88.               15
  89.       10              5
  90.   3       7        5     0
  91. 1   2   3   4   5     0    0
  92. */
  93.     return 0;
  94. }
  95.  
Add Comment
Please, Sign In to add comment