HDU 1540 POJ 2892 Tunnel Warfare

  
    
#include < iostream >
#include
< algorithm >
#include
< stack >
#define L(x) ((x) << 1)
#define R(x) ((x) << 1 | 1)
using namespace std;
const int MAXN = 50000 ;
struct SegTree{
int l, r;
int mval, lval, rval;
int cover; // 1 没坏 0 坏 -1 有的坏了 有的没坏
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 ){ // 此处无限wa 还一直找区间的问题 纠结 考虑一种情况 如果左右孩子的cover 都为-1 此时把父亲的cover也置为-1了 而我是用init 初始化 -1 传进去 则变成 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 ;
}

你可能感兴趣的:(poj)