HDU 1540——Tunnel Warfare(线段树,区间合并+单点更新+单点查询)

Tunnel Warfare

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


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个村庄有3种操作

1:D x 摧毁第x个村庄

2:R重建最后一次被摧毁的村庄

3:Q x查询与第x个村庄直接或者间接相连通的村庄数量,包括x村庄本身


思路:

维护一段区间的左右连续和总的连续,更新的时候是单点更新,查询的时候是单点查询:

如果点p在左子树,如果在左儿子右连续区间内,则要查询右子树

如果点p在右子树,如果在右儿子左连续区间内,则要查询左子树


这题有个坑点,就是题目没说要多组测试案例。。。但是没写就错了



#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<stack>
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
const int maxn=50001;
using namespace std;
int lc[maxn<<2],rc[maxn<<2],mc[maxn<<2];
void push_up(int rt,int m)
{
    lc[rt]=lc[rt<<1];
    rc[rt]=rc[rt<<1|1];
    if(lc[rt]==m-(m>>1)) lc[rt]+=lc[rt<<1|1];
    if(rc[rt]==m>>1) rc[rt]+=rc[rt<<1];
    mc[rt]=max(lc[rt<<1|1]+rc[rt<<1],max(mc[rt<<1],mc[rt<<1|1]));
}
void build(int l,int r,int rt)
{
    lc[rt]=rc[rt]=mc[rt]=r-l+1;
    if(l==r) return ;
    int m=(l+r)>>1;
    build(lson);
    build(rson);
}
void update(int p,int c,int l,int r,int rt)
{
    if(l==r) {
        lc[rt]=rc[rt]=mc[rt]=c? 1:0;
        return ;
    }
    int m=(l+r)>>1;
    if(p<=m) update(p,c,lson);
    else update(p,c,rson);
    push_up(rt,r-l+1);
}
int query(int p,int l,int r,int rt)
{
    if(l==r||mc[rt]==0||mc[rt]==r-l+1){
        return mc[rt];
    }
    int m=(l+r)>>1;
    if(p<=m){
        if(p>=m-rc[rt<<1]+1)
            return rc[rt<<1]+query(m+1,rson);
        else
            return query(p,lson);
    }else {
        if(p<=m+lc[rt<<1|1])
            return lc[rt<<1|1]+query(m,lson);
        else return query(p,rson);
    }
}
int main()
{
    int n,m;
    while(scanf("%d %d",&n,&m)!=EOF){
        stack<int> s;
        build(1,n,1);

        while(m--){
            char op;int x;
            getchar();
            scanf("%c",&op);
            if(op!='R') scanf("%d",&x);
            if(op=='D') {
                update(x,0,1,n,1);
                s.push(x);
            }
            if(op=='R') {
                update(s.top(),1,1,n,1);
                s.pop();
            }
            if(op=='Q') {
                printf("%d\n",query(x,1,n,1));
            }
        }
    }
    return 0;
}






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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值