hdu 1540 Tunnel Warfare(线段树+区间合并)

本文介绍了一个模拟抗日战争期间地道战的算法题目,通过线段树实现村庄与地道连接状态的实时更新与查询,以反映地道被破坏或修复后的连通情况。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Tunnel Warfare

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

 

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

Source

POJ Monthly

 有注释应该好理解,如果是对线段树的区间合并一无所知的童鞋,你先看看这个博客(这个人挺牛的):

 https://www.cnblogs.com/scau20110726/archive/2013/05/07/3065418.html

#include <bits/stdc++.h>
#define root 1,n,1
#define lc rt<<1
#define rc rt<<1|1
#define mid ((l+r)>>1)
#define lson l,mid,lc
#define rson mid+1,r,rc
using namespace std;
const int maxn=50000+10;
int lsum[maxn<<2],rsum[maxn<<2];
void build(int l,int r,int rt)
{
    lsum[rt]=rsum[rt]=r-l+1;
    if(l==r) return;
    build(lson);
    build(rson);
}
void update(int l,int r,int rt,int c,int f)
{
    if(l==r){
        if(f) lsum[rt]=rsum[rt]=0;
        else lsum[rt]=rsum[rt]=1;
        return;
    }
    if(c<=mid) update(lson,c,f);
    else update(rson,c,f);
    int len=r-l+1;
    lsum[rt]=lsum[lc];
    rsum[rt]=rsum[rc];
    if(lsum[rt]==len-(len>>1)) lsum[rt]+=lsum[rc];
    if(rsum[rt]==(len>>1)) rsum[rt]+=rsum[lc];
}
int query(int l,int r,int rt,int c)
{
    if(l==r) return lsum[rt];
    int ans=-1;
    if(l<=c&&c<=l+lsum[rt]-1) ans=lsum[rt];//如果在最左连续
    if(r-rsum[rt]+1<=c&&c<=r) ans=max(ans,rsum[rt]);//如果在最右连续
    //如果在横跨连续,右边界限不要写成mid+lsum[rc]-1,因为右区间是从mid+1开始的所以要加一(这地方找了许久,尴尬)
    if(mid-rsum[lc]+1<=c&&c<=mid+lsum[rc]) ans=max(ans,lsum[rc]+rsum[lc]);
    if(ans!=-1) return ans;//若有返回最大的,否则再去找更小区间
    if(c<=mid) return query(lson,c);
    else return query(rson,c);
}
int main ()
{
    int n,m,x;
    while(~scanf("%d%d",&n,&m))
    {
        int st[maxn],k=0;
        build(root);
        while(m--){
            char s[2];
            scanf("%s",s);
            if(s[0]=='D'){
                scanf("%d",&x);
                st[k++]=x;
                update(root,x,1);
            }else if(s[0]=='Q'){
                scanf("%d",&x);
                printf("%d\n",query(root,x));
            }
            else
                update(root,st[--k],0);
        }
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值