Advertisement
mickypinata

PROG-T1147: Segment Tree

Dec 5th, 2020
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.42 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. const int N = (1 << 18);
  5. const int LOGN = 18;
  6. const int INF = 1e9;
  7.  
  8. int sgt[(1 << (LOGN + 1))];
  9. int nSeq, nExp, Q;
  10.  
  11. int main(){
  12.  
  13.     scanf("%d %d", &nSeq, &Q);
  14.     nExp = 0;
  15.     while((1 << nExp) < nSeq){
  16.         ++nExp;
  17.     }
  18.  
  19.     for(int i = (1 << nExp) + nSeq; i < (1 << (nExp + 1)); ++i){
  20.         sgt[i] = -INF;
  21.     }
  22.     for(int i = (1 << nExp) - 1; i >= 1; --i){
  23.         sgt[i] = max(sgt[i << 1], sgt[(i << 1) | 1]);
  24.     }
  25.  
  26.     while(Q--){
  27.         char cmd;
  28.         int a, b;
  29.         scanf(" %c %d %d", &cmd, &a, &b);
  30.         if(cmd == 'U'){
  31.             a = (1 << nExp) + a - 1;
  32.             sgt[a] = b;
  33.             while(a > 1){
  34.                 if(a % 2 == 0){
  35.                     sgt[a / 2] = max(sgt[a], sgt[a + 1]);
  36.                 } else {
  37.                     sgt[a / 2] = max(sgt[a], sgt[a - 1]);
  38.                 }
  39.                 a /= 2;
  40.             }
  41.         } else if(cmd == 'P'){
  42.             a = (1 << nExp) + a - 1;
  43.             b = (1 << nExp) + b - 1;
  44.             int ans = -INF;
  45.             while(a <= b){
  46.                 if(a % 2 == 1){
  47.                     ans = max(ans, sgt[a++]);
  48.                 }
  49.                 if(b % 2 == 0){
  50.                     ans = max(ans, sgt[b--]);
  51.                 }
  52.                 a /= 2;
  53.                 b /= 2;
  54.             }
  55.             cout << ans << "\n";
  56.         }
  57.     }
  58.  
  59.     return 0;
  60. }
  61.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement