HDU 1540 Tunnel Warfare 线段树区间合并

http://acm.hdu.edu.cn/showproblem.php?pid=1540

Tunnel Warfare

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)


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个村庄,m个操作。操作有三种。1.'D x' 摧毁村庄x,2.'R' 把最后摧毁的村庄重建,3.'Q x'问与x相连的村庄有多少个

思路:线段树区间合并(学了一上午才会)

AC代码:

#include<cstdio>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<vector>
#include<stack>
#include<map>
#include<string>
#define LL long long
#define eps 1e-8
using namespace std;
const int mod2 = 1e9+7;
const int INF = 1e8;
const int inf = 0x3f3f3f3f;
const int maxx = 50010;
struct node
{
    int l,r;//l表示在当前区间从左往右数有多少个好村庄相连  直到数到坏村庄停止,r同理。
} t[maxx*4];
void build(int k,int l,int r)
{
    t[k].r=t[k].l=r-l+1;
    if(l==r) return ;
    int m=(l+r)>>1;
    build(k<<1,l,m);
    build(k<<1|1,m+1,r);
}
void up(int k,int p)
{
    int k1=k<<1,k2=k<<1|1;
    t[k].l=t[k1].l;
    t[k].r=t[k2].r;
    if(t[k].l==p-(p>>1)) t[k].l+=t[k2].l;
    if(t[k].r==p>>1) t[k].r+=t[k1].r;
}
void Insert(int k,int l,int r,int v,int id)
{
    if(l==r)
    {
        t[k].l=t[k].r=v;
        return ;
    }
    int m=(l+r)>>1;
    if(id<=m) Insert(k<<1,l,m,v,id);
    else Insert(k<<1|1,m+1,r,v,id);
    up(k,r-l+1);
}
int ans;
void Query(int k,int l,int r,int id)
{
    if(l==r)
    {
        ans=t[k].l;
        return ;
    }
    int m=(l+r)>>1;
    int k1=k<<1,k2=k<<1|1;
    if(id<=m)
    {
        Query(k1,l,m,id);
        if(m+1-t[k1].r<=id)  ans+=t[k2].l;
    }
    else
    {
        Query(k2,m+1,r,id);
        if(m+t[k2].l>=id)  ans+=t[k1].r;
    }
}
stack<int> s;
int main()
{
    int n,m;
    while(~scanf("%d%d",&n,&m))
    {
        while(!s.empty()) s.pop();
        build(1,1,n);
        char c[10];
        int a;
        while(m--)
        {
            scanf("%s",c);
            if(c[0]=='D')
            {
                scanf("%d",&a);
                s.push(a);
                Insert(1,1,n,0,a);
            }
            else if(c[0]=='Q')
            {
                ans=0;
                scanf("%d",&a);
                Query(1,1,n,a);
                printf("%d\n",ans);
            }
            else
            {
                if(!s.empty())
                {
                    a=s.top();
                    s.pop();
                    Insert(1,1,n,1,a);
                }
            }
        }
    }
}








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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值