When marking a segment tree, use Lsum rsum msum to record the value of the leftmost zui to the right and the maximum contiguous space of the entire interval.
Pay attention to consolidation when maintaining
#include <cstdio> #include <cstring> #include <algorithm>using namespace std; #define Lson (pos< <1) #define RSON (pos<<1|1) const int MAXN = 55555;int n,m;struct node{int l,r,lsum,rsum,msum,col; int Len () {return r-l + 1; } int mid () {return (L + R) >> 1; }}NODE[MAXN << 2];void Build (int l,int r,int pos) {node[pos].l = l; node[pos].r = R; Node[pos].col =-1; Node[pos].lsum = Node[pos].rsum = Node[pos].msum = Node[pos].len (); if (L = = r) return; int mid = Node[pos].mid (); Build (L,mid,lson); Build (mid + 1,r,rson);} void pushdown (int pos) {if (Node[pos].col! =-1) {if (!node[pos].col) {node[lson].lsum = Node[lson].rsu m = node[lson].msum = 0; Node[rson].lsum = node[rson].rsum = node[rson].msum = 0; Node[lson].col = Node[rson].col = 0; } else{node[lson].lsum = Node[lson].rsum = Node[lson].msum = Node[lson].len (); Node[rson].lsum= Node[rson].rsum = Node[rson].msum = Node[rson].len (); Node[lson].col = Node[rson].col = 1; } Node[pos].col =-1; }}void pushup (int pos) {node[pos].lsum = Node[lson].lsum; Node[pos].rsum = Node[rson].rsum; if (node[lson].lsum = = Node[lson].len ()) node[pos].lsum + = Node[rson].lsum; if (node[rson].rsum = = Node[rson].len ()) node[pos].rsum + = Node[lson].rsum; Node[pos].msum = Node[lson].rsum + node[rson].lsum; Node[pos].msum = Max (Node[pos].msum,max (Node[lson].msum,node[rson].msum));} void update (int l,int r,int Pos,int d) {if (L <= node[pos].l && NODE[POS].R <= R) {if (d = = 1) { Node[pos].lsum = Node[pos].rsum = Node[pos].msum = Node[pos].len (); Node[pos].col = 1; } if (d = = 0) {node[pos].lsum = Node[pos].rsum = node[pos].msum = 0; Node[pos].col = 0; } return; } pushdown (POS); int mid = Node[pos].mid (); if (l <= mid) update (L,r,lson,D); if (R > Mid) update (L,R,RSON,D); Pushup (POS);} int query (int l,int r,int Pos,int v) {if (L = = r) return l; Pushdown (POS); if (node[lson].msum >= v) return query (L,R,LSON,V); else if (node[lson].rsum + node[rson].lsum >= v) return Node[pos].mid ()-node[lson].rsum + 1; return query (L,R,RSON,V);} int main () {while (scanf ("%d%d", &n,&m)! = EOF) {build (1,n,1); for (int i = 0; i < m; i++) {int op,l,r; scanf ("%d", &op); if (op = = 1) {scanf ("%d", &l); if (Node[1].msum < L) printf ("0\n"); else{int v = query (1,n,1,l); printf ("%d\n", V); Update (v,v + l-1,1,0); printf ("%d\n", node[1].msum); }} else{scanf ("%d%d", &l,&r); Update (l,l + r-1,1,1); } } } return 0;}
"POJ" 3667-hotel (interval merging of segment trees)