HDU 1540 poj 2892 tunnel warfare

Source: Internet
Author: User
# Include  <  Iostream  >  
# Include < Algorithm >
# Include < Stack >
# Define L (x) <1)
# Define R (x) <1 | 1)
Using Namespace STD;
Const Int Maxn = 50000 ;
Struct Segtree {
Int L, R;
Int Mval, lval, rval;
Int Cover; // 1 No bad 0 bad-1 some bad some no bad
Void Init ( Int C ){
Lval = Rval = Mval = (C = 1 ? (R - L + 1 ): 0 );
Cover = C;
}
Int Getmid (){
Return (L + R) > 1 ;
}
Int Getdis (){
Return (R - L + 1 );
}
} Tree [maxn < 2 ];

Void Bulid ( Int Left, Int Right, Int T ){
Tree [T]. L = Left;
Tree [T]. r = Right;
Tree [T]. INIT ( 1 );
If (Left = Right)
Return ;
Int Mid = Tree [T]. getmid ();
Bulid (left, mid, L (t ));
Bulid (Mid + 1 , Right, r (t ));
}

Void Update ( Int X, Int C, Int T ){
If (Tree [T]. L = Tree [T]. r && Tree [T]. L = X ){
Tree [T]. INIT (C );
Return ;
}
If (Tree [T]. Cover ! = - 1 ){
Tree [L (t)]. Cover = Tree [r (t)]. Cover = Tree [T]. cover;
Tree [L (t)]. INIT (tree [T]. cover );
Tree [r (t)]. INIT (tree [T]. cover );
}
Int Mid = Tree [T]. getmid ();
If (X <= Mid ){
Update (x, C, L (t ));
} Else {
Update (x, C, r (t ));
}

Tree [T]. mval = Max (tree [L (t)]. rval + Tree [r (t)]. lval, max (tree [L (t)]. mval, tree [r (t)]. mval ));
Tree [T]. lval = Tree [L (t)]. lval + (Tree [L (t)]. Cover = 1 ? Tree [r (t)]. lval: 0 );
Tree [T]. rval = Tree [r (t)]. rval + (Tree [r (t)]. Cover = 1 ? Tree [L (t)]. rval: 0 );
If (Tree [L (t)]. Cover = Tree [r (t)]. Cover && Tree [L (t)]. Cover ! = - 1 ){ // In this case, the infinite wa still keeps searching for intervals. In this case, if the cover of both the left and right children is-1, the father's cover is also set to-1, and I use init to initialize- 1. The entire range is 0 !!!!!!!
Tree [T]. INIT (tree [r (t)]. cover ); // !!!!!!!!!!!!!!!!!!!!!!!!!
} Else {
Tree [T]. Cover = - 1 ;
}
}
Int Query ( Int X, Int T ){
If (Tree [T]. Cover = 1 )
Return Tree [T]. mval;
If (Tree [T]. Cover = 0 )
Return 0 ;
Int Mid = Tree [T]. getmid ();
If (X <= Mid ){
If (Mid - X + 1 <= Tree [L (t)]. rval)
Return Tree [L (t)]. rval + Tree [r (t)]. lval;
Return Query (x, L (t ));
} Else {
If (Mid + Tree [r (t)]. lval > = X)
Return Tree [r (t)]. lval + Tree [L (t)]. rval;
Return Query (x, r (t ));
}

}
Int Main (){
Int N, m, X, last;
Char CMD [ 3 ];
While (CIN > N > M ){
Bulid ( 1 , N, 1 );
Stack < Int > St;
For ( Int I = 0 ; I < M; ++ I ){
CIN > CMD;
If (CMD [ 0 ] = ' D ' ){
CIN > X;
St. Push (X );
Update (X, 0 , 1 );
} Else If (CMD [ 0 ] = ' Q ' ){
CIN > X;
Cout < Query (X, 1 ) < Endl;
} Else If (CMD [ 0 ] = ' R ' ){
If (St. Empty ())
Continue ;
Last = St. Top ();
St. Pop ();
Update (last, 1 , 1 );
}
}
}
Return 0 ;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.