hdu 1540 Tunnel Warfare(区间合并)

本文介绍了一个基于线段树的数据结构算法解决抗日战争中地道战模拟问题的方法。通过模拟村庄的破坏与重建,算法能够快速查询并计算出村庄间的连续连接状态。

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

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
 

题意:D代表破坏村庄,R代表修复最后被破坏的那个村庄,Q代表询问包括x在内的最大连续区间是多少
思路:线段树的增删查操作,增删操作就是区间合并问题,当查的时候需要判断左区间右区间是否为满;
代码:
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<stack>
using namespace std;
const int N=50005;
char c[10];
struct node{
    int l,r;
    int lr,ll,ml;
}str[3*N];
void build(int l,int r,int n)
{
    str[n].l=l;
    str[n].r=r;
    str[n].lr=str[n].ll=str[n].ml=r-l+1;
    if(l==r)
        return;
    int temp=(l+r)/2;
    build(l,temp,2*n);
    build(temp+1,r,2*n+1);
}
void updata(int n)
{
    str[n].ll=str[2*n].ll;
    str[n].lr=str[2*n+1].lr;
    if(str[2*n].ml==str[2*n].r-str[2*n].l+1)
        str[n].ll+=str[2*n+1].ll;
    if(str[2*n+1].ml==str[2*n+1].r-str[2*n+1].l+1)
        str[n].lr+=str[2*n].lr;
    str[n].ml=max(max(str[2*n].ml,str[2*n+1].ml),str[2*n].lr+str[2*n+1].ll);
}
void del(int x,int n)
{
    if(str[n].l==str[n].r)
    {
        str[n].ll=str[n].lr=str[n].ml=0;
        return;
    }
    int temp=(str[n].l+str[n].r)/2;
    if(x<=temp)
        del(x,2*n);
    else
        del(x,2*n+1);
    updata(n);
}
void insert(int x,int n)
{
    if(x==str[n].l&&str[n].l==str[n].r)
    {
        str[n].ll=str[n].lr=str[n].ml=1;
        return;
    }
    int temp=(str[n].l+str[n].r)/2;
    if(x<=temp)
        insert(x,2*n);
    else
        insert(x,2*n+1);
    updata(n);
}
int query(int x,int n)
{
    if(str[n].l==str[n].r||str[n].ml==0||str[n].ml==str[n].r-str[n].l+1)
        return str[n].ml;
    int temp=(str[n].l+str[n].r)/2;
    if(x<=temp)
    {
        if(x>=str[2*n].r-str[2*n].lr+1)
            return query(x,2*n)+query(temp+1,2*n+1);
        else
            return query(x,2*n);
    }
    else
    {
        if(x<=str[2*n+1].l+str[2*n+1].ll-1)
            return query(temp,2*n)+query(x,2*n+1);
        else
            return query(x,2*n+1);
    }
}
int main()
{
    int n,m;
    while(~scanf("%d%d",&n,&m))
    {
        build(1,n,1);
        stack<int>s;
        while(m--)
        {
            scanf("%s",c);
            if(c[0]=='D')
            {
                int x;
                scanf("%d",&x);
                s.push(x);
                del(x,1);
            }
            else if(c[0]=='R')
            {
                int x=s.top();
                s.pop();
                insert(x,1);
            }
            else
            {
                int x;
                scanf("%d",&x);
                int sum=query(x,1);
                printf("%d\n",sum);
            }
        }
    }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值