Hdu 1540 Tunnel Warfare【线段树区间合并学习】

本文介绍了一种利用线段树数据结构解决隧道战模拟问题的方法,通过单点更新和查询实现了对隧道网络状态的高效管理。

Tunnel Warfare

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


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

题目大意:


给你N个建筑,建筑排成一排,D操作表示要摧毁一个城市,R表示恢复重建上一次被摧毁的城市,Q表示查询和当前建筑相连的依旧存在的建筑的个数。

注意,这个题如果将一个城市摧毁3次,那么要R三次才能恢复重建。


思路:


线段树的单点更新和单点查询区间合并问题。

我们设定Lsum【rt】表示当前区间点【L,R】从L开始往右数连续的还存在的建筑的个数,同理,再设定Rsum【rt】表示当前区间点【L,R】从R开始往左数连续的还存在的建筑的个数。


①build的时候,将每个区间点都初始化为【R-L+1】;


②pushup的时候,我们要判定一下当前区间点的左右儿子区间是否能够连接起来。


③update的时候,我们和单点更新一样,直到找到当前点的叶子节点再返回pushup即可。


④query的时候,我们和单点查询一样,直到找到当前点的叶子节点一路查询即可。

这里重点在于区间合并的操作,首先我们讨论当前区间点的问题:

如果当前区间点从左到右能够覆盖查询点,那么ans=max(ans,Lsum【now】);

同理,如果当前区间从右到左能够覆盖查询点,那么ans=max(ans,Rsum【now】);

然后我们讨论当前区间和兄弟节点的合并查询问题:

如果当前区间点是左儿子,那么对应我们查询左儿子的Rsum和右儿子的Lsum是否能够连接并且覆盖查询点,如果可以,那么对应ans=max(ans,Rsum【now】+Lsum【now+1】)

如果当前区间点是右儿子,那么对应我们查询右儿子的Lsum和左儿子的Rsum是否能够连接并且覆盖查询点,如果可以,那么对应ans=max(ans,Lsum【now】+Rsum【now-1】);


过程维护 一下即可。


注意一个城市如果摧毁多次,那么需要恢复多次。


Ac代码:

#include<stdio.h>
#include<string.h>
#include<stack>
#include<algorithm>
#include<iostream>
using namespace std;
int n,q;
/****************************/
#define lson l,m,rt*2
#define rson m+1,r,rt*2+1
int Lsum[150000*4];
int Rsum[150000*4];
void pushup(int l,int r,int rt)
{
    int m=(l+r)/2;
    Lsum[rt]=Lsum[rt*2];
    Rsum[rt]=Rsum[rt*2+1];
    if(Lsum[rt*2]==(m-l+1))Lsum[rt]+=Lsum[rt*2+1];
    if(Rsum[rt*2+1]==(r-(m+1)+1))Rsum[rt]+=Rsum[rt*2];
}
void build(int l,int r,int rt)
{
  //  printf("---%d %d %d\n",l,r,rt);
    if(l==r)
    {
        Lsum[rt]=Rsum[rt]=r-l+1;
        return ;
    }
    int m=(l+r)/2;
    build(lson);build(rson);pushup(l,r,rt);
}
void update(int p,int c,int l,int r,int rt)
{
    if(l==r)
    {
        if(c==-1)Lsum[rt]=Rsum[rt]=0;
        else Lsum[rt]=Rsum[rt]=1;
        return ;
    }
    int m=(l+r)/2;
    if(p<=m)update(p,c,lson);
    if(p>m)update(p,c,rson);
    pushup(l,r,rt);
}
int query(int p,int l,int r,int rt)
{
    if(l==r)
    {
        return max(Lsum[rt],Rsum[rt]);
    }
    pushup(l,r,rt);
    int ans=0;
    int m=(l+r)/2;
    if(p>=l&&p<=r)
    {
        if(Lsum[rt]>=p-l+1)ans=max(ans,Lsum[rt]);
        if(Rsum[rt]>=r-p+1)ans=max(ans,Rsum[rt]);
    }
    if(rt%2==0)
    {
        if(Rsum[rt]>=r-p+1)ans=max(ans,Rsum[rt]+Lsum[rt+1]);
    }
    else
    {
        if(Lsum[rt]>=p-l+1)ans=max(ans,Lsum[rt]+Rsum[rt-1]);
    }
    if(p<=m)ans=max(ans,query(p,lson));
    if(p>m)ans=max(ans,query(p,rson));
    pushup(l,r,rt);
    return ans;
}
/****************************/
int main()
{
    while(~scanf("%d%d",&n,&q))
    {
        stack<int>ss;
        build(1,n,1);
        while(q--)
        {
            char op[5];
            scanf("%s",op);
            if(op[0]=='D')
            {
                int x;scanf("%d",&x);
                ss.push(x);
                update(x,-1,1,n,1);
            }
            else if(op[0]=='Q')
            {
                int x;scanf("%d",&x);
                printf("%d\n",query(x,1,n,1));
            }
            else
            {
                if(ss.size()==0)continue;
                else
                {
                    int x=ss.top();ss.pop();
                    update(x,1,1,n,1);
                }
            }
        }
    }
}


评论 2
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值