HDU 1540 Tunnel Warfare(线段树)

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表示查询和当前建筑相连的依旧存在的建筑的个数。

线段树的做法没想出来,用暴力试了试过了,待会补上线段树的做法。

AC代码:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<vector>
#include<stdlib.h>
#include<set>
#include<map>
#include<stack>
#include<iomanip>
#include<math.h>
using namespace std;
typedef long long ll;
typedef double ld;
int i,j,k;
int n,m;
set<int>S;
int res;
char op;
int main()
{
    while(cin>>n>>m)
    {
        int per;
        S.clear();
        S.insert(0);
        S.insert(n+1);
        stack<int>ST;
        for(i=0; i<m; i++)
        {
            cin>>op;
            if(op=='D')
            {
                scanf("%d",&res);
                ST.push(res);
                S.insert(res);
            }
            else if(op=='Q')
            {
                cin>>res;
                if(S.find(res)!=S.end())
                    printf("0\n");
                else
                {
                    set<int>::iterator it1=S.upper_bound(res);
                    set<int>::iterator it2=(S.lower_bound(res));
                    it2--;
                    cout<<(*it1-*it2-1)<<endl;
                }
            }
            else
            {
                S.erase(ST.top());
                ST.pop();
            }
        }
    }
    return 0;
}

线段树做法:

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

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

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<vector>
#include<stdlib.h>
#include<set>
#include<map>
#include<stack>
#include<iomanip>
#include<math.h>
using namespace std;
typedef long long ll;
typedef double ld;
int i,j,k;
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)
{
    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);
                }
            }
        }
    }
    return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值