hdu_1540Tunnel Warfare

Tunnel Warfare

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 4411    Accepted Submission(s): 1690


Problem Description
During the War of Resistance Against Japan, tunnel warfare was carried out extensively in the vast areas of north China Plain. Generally speaking, villages connected by tunnels lay in a line. Except the two at the ends, every village was directly connected with two neighboring ones.

Frequently the invaders launched attack on some of the villages and destroyed the parts of tunnels in them. The Eighth Route Army commanders requested the latest connection state of the tunnels and villages. If some villages are severely isolated, restoration of connection must be done immediately!
 

Input
The first line of the input contains two positive integers n and m (n, m ≤ 50,000) indicating the number of villages and events. Each of the next m lines describes an event.

There are three different events described in different format shown below:

D x: The x-th village was destroyed.

Q x: The Army commands requested the number of villages that x-th village was directly or indirectly connected with including itself.

R: The village destroyed last was rebuilt.
 

Output
Output the answer to each of the Army commanders’ request in order on a separate line.
 

Sample Input
  
  
7 9 D 3 D 6 D 5 Q 4 Q 5 R Q 4 R Q 4
 

Sample Output
  
  
1 0 2 4
 

Source
 

Recommend
LL
 

大概思想,本题是对子叶节点进行改变,因此不需要lazy延迟操作,同时在结构体里面设置该节点左起最大连续长度,右起最大连续长度以及中间最大连续常度。
注意,本题要求是查询某个节点所在区间的长度,因此在查询时首先判断该节点位于左右子树,然后判断是否位于中间最大连续区间内,最好在纸上画一下比较容易理解,最好求长度

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
#define INF 0x3f3f3f3f
#define inf -0x3f3f3f3f
#define mem0(a) memset(a,0,sizeof(a))
const int maxn = 50000+10;
struct node{
    int l,r;
    int lsum,rsum,msum;//分别代表从左数最大连续区间,右数最大连续区间,中间最大连续区间
}a[maxn<<2];//本题是对点更新,因此不需要延迟lazy操作
void build(int l,int r,int cur){
    a[cur].l = l ;
    a[cur].r = r ;
    a[cur].lsum = a[cur].rsum = a[cur].msum = r-l+1;
    if(a[cur].l == a[cur].r )return;
    int mid = ( l + r )>>1 ;
    build(l,mid,cur<<1);
    build(mid+1,r,cur<<1|1);
}
void pushup(int cur){
    a[cur].lsum =a[cur<<1].lsum ;
    a[cur].rsum =a[cur<<1|1].rsum;
    if(a[cur<<1].lsum == a[cur<<1].r -a[cur<<1].l + 1)//如果左子树左起连续的个数等于区间长度
        a[cur].lsum += a[cur<<1|1].lsum;           //那么连续长度还要加上右子树左起连续的长度
    if(a[cur<<1|1].rsum == a[cur<<1|1].r -a[cur<<1|1].l +1)
        a[cur].rsum += a[cur<<1].rsum ;
    a[cur].msum =max(max(a[cur<<1].msum,a[cur<<1|1].msum),(a[cur<<1].rsum+a[cur<<1|1].lsum));
    //节点的最大连续区间为左子树左起最大连续区间,右子树右起最大连续区间,以及中间跨越左右子树
    //最大连续区间 三者最大值
    
}
void update(int x,int v,int cur){
    if(a[cur].l == a[cur].r ) {//更新子叶节点
        if(v == 1 )
            a[cur].lsum = a[cur].rsum = a[cur].msum = 1;
        else
            a[cur].lsum = a[cur].rsum = a[cur].msum = 0;
        return ;
    }
    int mid = (a[cur].l + a[cur].r )>>1;
    if(x <= mid)//如果要更新的节点在左子树
        update(x,v,cur<<1);//更新该节点的左子树
    else         //在右子树
        update(x,v,cur<<1|1);
    pushup(cur);//回溯,更新节点信息
}
int query(int x,int cur){
    if(a[cur].l == a[cur].r || a[cur].msum == 0|| a[cur].msum == a[cur].r -a[cur].l+1){
        return a[cur].msum;
    }
    int mid = (a[cur].l + a[cur].r )>>1;
    if(x <= mid){//查询节点在左子树
        if(x >= a[cur<<1].r -a[cur<<1].rsum+1)//如果查询的节点在中间那部分
        //那么中间最长连续区域的左端点比小于要查询节点,也就是查询节点包含于中间连续区间
            return query(x,cur<<1)+query(mid+1,cur<<1|1);
        else
            return query(x,cur<<1);
    }
    else {//同理节点在右子树,若在中间连续区域内,则节点必小于右子树左起最大连续长度
        if(x <= a[cur<<1|1].l + a[cur<<1|1].lsum-1)
            return query(x,cur<<1|1)+query(mid,cur<<1);
        else
            return query(x,cur<<1|1);
    }
}
int main()
{
    int n,m;
    while(scanf("%d%d",&n,&m)!=EOF){
        build(1,n,1);
        char c[5];
        int x[maxn],i=0,q;
        while(m--){
            scanf("%s",c);
            if(c[0]=='D'){
                scanf("%d",&x[++i]);
                update(x[i],0,1);
            }
            else if(c[0]=='Q'){
                scanf("%d",&q);
                printf("%d\n",query(q,1));
            }
            else {
                update(x[i--],1,1);
            }
        }
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值