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 ;
}

转载于:https://www.cnblogs.com/lxf90/archive/2011/04/10/2011076.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值