Title Link: http://poj.org/problem?id=3667
Test instructions: There are n rooms in the hotel. Someone came to stay. A total of 2 operations
- Enter 1 and D to indicate the starting position of the left-most consecutive D-empty room number in a query.
- Enter 2,x and D, which means that a continuous room with a length of D from X will be emptied.
Idea: the bare interval merges. Each node interval [l,r] is stored from the left endpoint L to the right maximum contiguous empty room number LM, starting from right endpoint R to left maximum consecutive empty room number of RM and maximum consecutive empty rooms in the current interval.
Code:
#include <iostream>#include <stdio.h>#include <string.h>#include <math.h>#include <algorithm>#include <string>using namespace STD;#define Lson L, M, RT << 1#define Rson m + 1, R, RT << 1 | 1Const intN =5e5+Ten;Const intINF =0x7f7f7f7f;structNode {intlmintRmintmx Node () {} node (intXintYintZ) {lm = x; rm = y; MX = z; }}; Node Node[n <<2];intLazy[n <<2];voidPushup (intRtintLintR) {intm = (L + r) >>1; node[rt].mx = max (Node[rt <<1].rm + node[rt <<1|1].LM, Max (Node[rt <<1].mx, Node[rt <<1|1].mx));if(Node[rt <<1].LM = = M-l +1) NODE[RT].LM = Node[rt <<1].LM + node[rt <<1|1].LM;ElseNODE[RT].LM = Node[rt <<1].LM;if(Node[rt <<1|1].rm = = r-m) node[rt].rm = Node[rt <<1|1].rm + node[rt <<1].RM;ElseNODE[RT].RM = Node[rt <<1|1].RM;}voidPushdown (intRtintLintR) {if(LAZY[RT]! =-1) {Lazy[rt <<1] = Lazy[rt <<1|1] = Lazy[rt];intm = (L + r) >>1;intLENL = M-l +1;intlenr = r-m;if(Lazy[rt] = =1) {Node[rt <<1] = Node (lenl, LENL, lenl); Node[rt <<1|1] = Node (lenr, lenr, lenr); }Else{Node[rt <<1] = Node (0,0,0); Node[rt <<1|1] = Node (0,0,0); } Lazy[rt] =-1; }}voidBuildintLintRintRT) {Lazy[rt] =-1; NODE[RT].LM = node[rt].rm = node[rt].mx = R-l +1;if(L = = r)return;intm = (L + r) >>1; Build (Lson); Build (Rson);}voidUpdateintTintLintRintLintRintRT) {if(l <= L && R <= R) {if(T = =1) Node[rt] = node (r-l +1, R-l +1, R-l +1);ElseNODE[RT] = Node (0,0,0); LAZY[RT] = t;return; }if(L = = r)return; Pushdown (RT, L, R);intm = (L + r) >>1;if(l <= m) update (T, L, R, Lson);if(r > M) update (T, L, R, Rson); Pushup (RT, L, R);}intQueryintTintLintRintRT) {if(L = = r)returnLintm = (L + r) >>1; Pushdown (RT, L, R);intPif(Node[rt <<1].mx >= t) p = query (t, Lson);Else if(Node[rt <<1].rm + node[rt <<1|1].LM >= t) p = m-node[rt <<1].rm +1;Elsep = Query (t, Rson); Pushup (RT, L, R);returnP;}intMain () {intN, M; while(scanf("%d%d", &n, &m)! = EOF) {Build (1N1); for(intI_q =1; I_q <= m; i_q++) {intQscanf("%d", &q);if(q = =1) {intDscanf("%d", &d);if(node[1].mx < D) {//No updates required. WA got N-fat. puts("0");Continue; }intL = Query (d,1N1);printf("%d\n", l); Update0, L, L + D-1,1N1); }Else{intX, D;scanf("%d%d", &x, &d); Update1, x, X + D-1,1N1); } } }return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
POJ 3667 Hotel (interval merger)