hdu 1540

Tunnel Warfare

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


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
 


题目大意:D代表破坏村庄,R代表修复最后被破坏的那个村庄,Q代表询问包括x在内的最大连续区间是多少

思     路:知道是利用线段树,打算写递归循环来着,发现自己越写越混乱,最后不得不看别人的博客求解答。
               很巧妙的利用线段树的特点,不断的二分,然后找到了每个子树的左右连续区间以及最大区间长

              同时在查找的时候考虑到左子树的右区间和右子树的左区间连续的情况  很巧妙!关键在与在二叉树的

              基础上能准确的定位应该存储什么意义的东西

              代码以及详解如下:


<pre name="code" class="cpp">/*踏实!!努力!!*/
#include<iostream>
#include<stdio.h>
#include<cmath>
#include<cstring>
#include<map>
#include<queue>
#include<stack>
using namespace std;
#define N 50000+10
int n,m;
struct{
    int l,r;
    int ls,rs,ms; //存储左连续区间长,右连续区间长,最大区间长
}sum[N<<2];
void build(int l,int r,int cur)
{
    sum[cur].l=l;
    sum[cur].r=r;
    //初始的时候全部为最长
    sum[cur].ls=sum[cur].rs=sum[cur].ms=r-l+1;
    if(l==r) return ;
    int mid=(l+r)>>1;
    build(l,mid,cur<<1);
    build(mid+1,r,cur<<1|1);
}
void upDate(int i,int val,int cur)
{
    if(sum[cur].l==sum[cur].r){
        //毁坏或者修复
        sum[cur].ls=sum[cur].rs=sum[cur].ms=val;
        return ;
    }

    int mid=(sum[cur].l+sum[cur].r)>>1;
    if(i<=mid)
        upDate(i,val,cur<<1);
    else
        upDate(i,val,cur<<1|1);

    //更新左右区间长以及最长区间
    sum[cur].ls=sum[cur<<1].ls;
    sum[cur].rs=sum[cur<<1|1].rs;
    sum[cur].ms=max(sum[cur<<1].rs+sum[cur<<1|1].ls,max(sum[cur<<1].ms,sum[cur<<1|1].ms));

    //如果左子树的左区间已满 要加上右子树的左区间
    if(sum[cur<<1].ls==sum[cur<<1].r-sum[cur<<1].l+1)
        sum[cur].ls+=sum[cur<<1|1].ls;
    //如果右子树的右区间已满 要加上左子树的右区间
    if(sum[cur<<1|1].rs==sum[cur<<1|1].r-sum[cur<<1|1].l+1)
        sum[cur].rs+=sum[cur<<1].rs;
}
int query(int x,int cur)
{
    //访问到叶子节点或者区间为空或者区间已满 
    //从这里可以看出设计ms的作用  防止在返回的时候左右区间混乱
    if(sum[cur].l==sum[cur].r||sum[cur].ms==0||sum[cur].ms==sum[cur].r-sum[cur].l+1)
        return sum[cur].ms;

    int mid=(sum[cur].l+sum[cur].r)>>1;

    if(x<=mid){
        //如果x在左子树的右区间内 要考虑右子树的左区间
        if(x>=sum[cur<<1].r-sum[cur<<1].rs+1)
            return query(x,cur<<1)+query(mid+1,cur<<1|1);
        else
            return query(x,cur<<1);
    }
    else{
        //如果x在右子树的左区间内 要考虑左子树的右区间
        if(x<=sum[cur<<1|1].l+sum[cur<<1|1].ls-1)
            return query(x,cur<<1|1)+query(mid,cur<<1);
        else
            return query(x,cur<<1|1);
    }
}
int main()
{
    int q[N],cnt;//模拟栈
    while(scanf("%d%d",&n,&m)!=EOF){
        getchar();
        build(1,n,1);
        char ch;
        int x;
        cnt=0;
        while(m--){
            scanf("%c",&ch);
            if(ch=='D'){
                scanf("%d",&x);
                q[cnt++]=x;
                upDate(x,0,1);
            }
            else if(ch=='R'){
                x=q[--cnt];
                upDate(x,1,1);
            }
            else{
                scanf("%d",&x);
                int temp=query(x,1);
                printf("%d\n",temp);
            }
            getchar();
        }
    }
    return 0;
}



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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值