nullzero

Lazy Segment Tree

Oct 6th, 2012
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.45 KB | None | 0 0
  1. #include <cstdio>
  2. #include <algorithm>
  3.  
  4. #define MID ((l + r) / 2)
  5.  
  6. const int N(1 << 20)
  7. const int INF(1e9)
  8. const int UNDEF(-1)
  9.  
  10. using namespace std;
  11. typedef pair<int,int> II;
  12.  
  13. struct SegmentTree{
  14.     II T[N];
  15.     int a, b, v;
  16.  
  17.     inline bool intersect(int l, int r){
  18.         return max(l, a) <= min(r, b);
  19.     }
  20.  
  21.     inline void clearlazy(int node, int l, int r){
  22.         if(T[node].first == UNDEF) return;
  23.         T[2 * node] = T[node];
  24.         T[2 * node + 1] = T[node];
  25.         T[node].first = UNDEF;
  26.     }
  27.  
  28.     void uu(int node, int l, int r){
  29.         if(a <= l and r <= b){
  30.             T[node] = II(v, v);
  31.             return;
  32.         }
  33.         if(not intersect(l, r)) return;
  34.         clearlazy(node, l, r);
  35.         uu(2 * node, l, MID);
  36.         uu(2 * node + 1, MID + 1, r);
  37.         T[node].second = max(T[2 * node].second, T[2 * node + 1].second);
  38.     }
  39.  
  40.     int qq(int node, int l, int r){
  41.         if(a <= l and r <= b) return T[node].second;
  42.         if(not intersect(l, r)) return -INF;
  43.         clearlazy(node, l, r);
  44.         return max(qq(2 * node, l, MID), qq(2 * node + 1, MID + 1, r));
  45.     }
  46.  
  47.     int query(int aa, int bb){
  48.         a = aa;
  49.         b = bb;
  50.         return qq(1, 1, M.size());
  51.     }
  52.  
  53.     void update(int aa, int bb, int val){
  54.         a = aa;
  55.         b = bb;
  56.         v = val;
  57.         uu(1, 1, M.size());
  58.     }
  59. };
  60.  
  61. /*
  62. Lazy Segment Tree
  63. update: set value in [l,r] to v
  64. query:  find RMQ in [l,r]
  65. */
Advertisement
Add Comment
Please, Sign In to add comment