HDOJ 1540 Tunnel Warfare

Tunnel Warfare
Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

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. 
 

题意还是很简单,毁坏或者修复某一个村子,求某个村子的最长连续区间长度,这里用线段树来维护某个节点中最长的区间长度,为了方便,在节点中加入ls,rs,ms表示从左端点开始最长连续长度,从右端点开始的最长连续长度和这个区间里面的最长连续长度,ls和rs是用于维护父节点的ms,当回溯更新父节点时,父节点的ms为左儿子的ms和右儿子的ms以及左右儿子中间相连接的最大值,这个最大值就用左儿子的rs+右儿子的ls来维护,再来维护父节点的ls和rs,这里要注意细节左右儿子可能为满的,思路也不是很清晰代码容易出错,下次做题应该想好整个过程注意处理细节再做,人笨就要多想。

#include<stdio.h>
#include<algorithm>
#include<string.h>
using namespace std;
#define maxn 50005
struct node
{
    int id;
    int l;
    int r;
    int ls;
    int rs;
    int ms;
}tree[4*maxn];
int use[maxn],len,q,n;
void update(int id,int flag,int x)
{
    int l=tree[id].l;
    int r=tree[id].r;
    int mid=(l+r)/2;
    if(l==r)
    {
        tree[id].ls=flag;
        tree[id].rs=flag;
        tree[id].ms=flag;
        //printf("%d %d\n",id,tree[id].ms);
        return ;
    }
    if(x<=mid)
        update(id*2,flag,x);
    else
        update(id*2+1,flag,x);

    tree[id].ms=max(max(tree[id*2].ms,tree[id*2+1].ms),tree[id*2].rs+tree[id*2+1].ls);  //回溯更新父节点
    if(tree[id*2].ls==(tree[id*2].r-tree[id*2].l+1))
        tree[id].ls=tree[id*2].ls+tree[id*2+1].ls;
    else
        tree[id].ls=tree[id*2].ls;

    if(tree[id*2+1].rs==(tree[id*2+1].r-tree[id*2+1].l+1))
        tree[id].rs=tree[id*2+1].rs+tree[id*2].rs;
    else
        tree[id].rs=tree[id*2+1].rs;

}
int query(int id,int x)
{
    int l=tree[id].l;
    int r=tree[id].r;
    int mid=(l+r)/2;
    if(tree[id].ms==0||(tree[id].ms==(tree[id].r-tree[id].l+1)))    //剪枝判断
        return tree[id].ms;
    if(l==r)
        return tree[id].ms;
    if(x<=mid)
    {
        if(x>=tree[id*2].r-tree[id*2].rs+1)
            return query(id*2,x)+query(id*2+1,mid+1);
        else
            return query(id*2,x);
    }
    else
    {
        if(x<=tree[id*2+1].l+tree[id*2+1].ls-1)
            return query(id*2+1,x)+query(id*2,mid);
        else
            return query(id*2+1,x);
    }
}
void build(int id,int l,int r)
{
    tree[id].l=l;
    tree[id].r=r;
    tree[id].ls=r-l+1;     //初始化
    tree[id].rs=r-l+1;
    tree[id].ms=r-l+1;
    if(l==r)
        return;
    build(id*2,l,(l+r)/2);
    build(id*2+1,(l+r)/2+1,r);


}
int main()
{
    char s[10];
    int x;
    while(scanf("%d%d",&n,&q)!=EOF)
    {
        len=0;
        build(1,1,n);
        while(q--)
        {
            scanf("%s",s);
            if(s[0]=='D')
            {
                scanf("%d",&x);
                update(1,0,x);
                use[len++]=x;
            }
            else if(s[0]=='Q')
            {
                scanf("%d",&x);
                int ans=query(1,x);
                printf("%d\n",ans);
            }
            else
            {
                x=use[len-1];   //维护
                len--;
                update(1,1,x);
            }
        }
    }
return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值