Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstdio>
- #include <algorithm>
- #define MID ((l + r) / 2)
- const int N(1 << 20)
- const int INF(1e9)
- const int UNDEF(-1)
- using namespace std;
- typedef pair<int,int> II;
- struct SegmentTree{
- II T[N];
- int a, b, v;
- inline bool intersect(int l, int r){
- return max(l, a) <= min(r, b);
- }
- inline void clearlazy(int node, int l, int r){
- if(T[node].first == UNDEF) return;
- T[2 * node] = T[node];
- T[2 * node + 1] = T[node];
- T[node].first = UNDEF;
- }
- void uu(int node, int l, int r){
- if(a <= l and r <= b){
- T[node] = II(v, v);
- return;
- }
- if(not intersect(l, r)) return;
- clearlazy(node, l, r);
- uu(2 * node, l, MID);
- uu(2 * node + 1, MID + 1, r);
- T[node].second = max(T[2 * node].second, T[2 * node + 1].second);
- }
- int qq(int node, int l, int r){
- if(a <= l and r <= b) return T[node].second;
- if(not intersect(l, r)) return -INF;
- clearlazy(node, l, r);
- return max(qq(2 * node, l, MID), qq(2 * node + 1, MID + 1, r));
- }
- int query(int aa, int bb){
- a = aa;
- b = bb;
- return qq(1, 1, M.size());
- }
- void update(int aa, int bb, int val){
- a = aa;
- b = bb;
- v = val;
- uu(1, 1, M.size());
- }
- };
- /*
- Lazy Segment Tree
- update: set value in [l,r] to v
- query: find RMQ in [l,r]
- */
Advertisement
Add Comment
Please, Sign In to add comment