Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=1540
To give you a row of consecutive points, you can do the following three kinds of operations:
1. Delete a point
2, inquiry and length of the segment of a point (if the point has been deleted, the length is 0)
3. Restore the last deleted point
The segment tree calculates a continuous interval, and the node holds the left continuous length LM, and the right continuous length of RM
1 /*2 * Problem:hdu 1540 tunnel Warfare3 * AUTHOR:SHJWUDP4 * Created time:2015/3/28 Saturday 13:10:495 * File name:233.cpp6 * state:accepted7 * Memo: Line tree8 */9#include <iostream>Ten#include <cstdio> One#include <cstring> A#include <algorithm> - - using namespacestd; the - Const intmaxa=5e4+7; - - structSegmenttree { + //This structure can be used to find the maximum continuous interval, only one variable is needed to save the maximum continuous interval under the current node. - structNode { + intLM, RM; A Node () {} atNode (intLintr): LM (L), RM (r) {} -} *C; - intN; - intp, sign; - -Segmenttree (intN): N (n) { inC=Newnode[(n+7) <<2]; -Build1N1); to } +~Segmenttree () { - DeleteC; the } * #defineINIT node& U=c[rt]; node& ls=c[rt<<1]; node& rs=c[rt<<1|1]; $ #defineLson L, M, rt<<1Panax Notoginseng #defineRson m+1, R, rt<<1|1 - the voidPushup (intLintMintRintRT) { + INIT; Au=Node (LS.LM, rs.rm); the if(m-l+1==LS.LM) u.lm+=rs.lm; + if(R-M==RS.RM) u.rm+=ls.rm; - } $ $ voidBuildintLintRintRT) { - if(l==r) { -C[rt]=node (1,1); the}Else { - intM= (l+r) >>1;Wuyi build (Lson); the build (Rson); - pushup (L, M, R, RT); Wu } - } About $ voidDoUpdate (intLintRintRT) { - if(l==r) { -C[rt]=sign? Node (1,1): Node (0,0); -}Else { A intM= (l+r) >>1; + if(p<=m) doupdate (Lson); the Elsedoupdate (Rson); - pushup (L, M, R, RT); $ } the } the the voidUpdateintPintSign ) { the This->p=p; This->sign=Sign ; -DoUpdate (1N1); in } the the intDoquery (intLintRintRT) { About INIT; the if(P-L<U.LM | | r-p<u.rm) { the returnP-l<u.lm?u.lm:u.rm; the}Else if(L==R)return 0; + Else { - intM= (l+r) >>1; the intres=0;Bayi if(p<=m) Res+=doquery (Lson) + (m-p+1<=LS.RM?RS.LM:0); the ElseRes+=doquery (Rson) + (p-m<=rs.lm?ls.rm:0); the returnRes; - } - } the the intQueryintp) { the This->p=p; the returnDoquery (1N1); - } the #undefInit the #undefLson the #undefRson94 }; the the intN, M; the intStk[maxa], top;98 intMain () { About #ifndef Online_judge -Freopen ("inch","R", stdin);101 //freopen ("Out", "w", stdout);102 #endif103 while(~SCANF ("%d%d", &n, &m)) {104 Segmenttree St (n); thetop=0;106 while(m--) {107 Charop[2];108 intx;109scanf"%s", op); the Switch(op[0]) {111 Case 'D': thescanf"%d", &x);113St.update (Stk[++top]=x,0); the Break; the Case 'Q': thescanf"%d", &x);117printf"%d\n", St.query (x));118 Break;119 Case 'R': -St.update (stk[top--],1);121 Break;122 }123 }124 } the return 0;126}
HDU 1540
HDU 1540 Tunnel Warfare