Alex_tz307

Mountain IOI 2005

Sep 14th, 2020 (edited)
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.02 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. int N;
  6.  
  7. struct node {
  8.     int totsum, maxsum, lazyval;
  9.     bool lazy;
  10.     node *left, *right;
  11. }*root;
  12.  
  13. void propagate(node* x, int lx, int rx) {
  14.     if(!x -> lazy)
  15.         return;
  16.     if(!x -> left)
  17.         x -> left = new node();
  18.     if(!x -> right)
  19.         x -> right = new node();
  20.     x -> totsum = x -> maxsum = (rx - lx + 1) * x -> lazyval;
  21.     if(lx != rx) {
  22.         x -> left -> lazy = x -> right -> lazy = true;
  23.         x -> left -> lazyval = x -> right -> lazyval = x -> lazyval;
  24.     }
  25.     x -> lazy = false;
  26. }
  27.  
  28. void update(int st, int dr, int val, node* x = root, int lx = 1, int rx = N + 1) {
  29.     if(!x -> left)
  30.         x -> left = new node();
  31.     if(!x -> right)
  32.         x -> right = new node();
  33.     propagate(x, lx, rx);
  34.     if(st <= lx && rx <= dr) {
  35.         x -> lazy = 1;
  36.         x -> lazyval = val;
  37.         propagate(x, lx, rx);
  38.         return;
  39.     }
  40.     int mid = (lx + rx) >> 1;
  41.     if(st <= mid)
  42.         update(st, dr, val, x -> left, lx, mid), propagate(x -> right, mid + 1, rx);
  43.     if(mid < dr)
  44.         update(st, dr, val, x -> right, mid + 1, rx), propagate(x -> left, lx, mid);
  45.     x -> totsum = x -> left -> totsum + x -> right -> totsum;
  46.     x -> maxsum = max(x -> left -> maxsum, x -> left -> totsum + x -> right -> maxsum);
  47. }
  48.  
  49. int query(int val, node* x = root, int lx = 1, int rx = N + 1) {
  50.     if(lx == rx)
  51.         return lx;
  52.     if(!x -> left)
  53.         x -> left = new node();
  54.     if(!x -> right)
  55.         x -> right = new node();
  56.     int mid = (lx + rx) >> 1;
  57.     propagate(x, lx, rx);
  58.     propagate(x -> left, lx, mid);
  59.     if(x -> left -> maxsum > val)
  60.         return query(val, x -> left, lx, mid);
  61.     return query(val - x -> left -> totsum, x -> right, mid + 1, rx);
  62. }
  63.  
  64. int main() {
  65.     ios_base::sync_with_stdio(false);
  66.     cin.tie(nullptr);
  67.     cout.tie(nullptr);
  68.     cin >> N;
  69.     root = new node();
  70.     char type;
  71.     update(N + 1, N + 1, INT_MAX);
  72.     while(cin >> type, type != 'E') {
  73.         if(type == 'Q') {
  74.             int x;
  75.             cin >> x;
  76.             cout << query(x) - 1 << '\n';
  77.         }
  78.         else {
  79.             int st, dr, val;
  80.             cin >> st >> dr >> val;
  81.             update(st, dr, val);
  82.         }
  83.     }
  84. }
  85.  
Add Comment
Please, Sign In to add comment