HDU - 1540 Tunnel Warfare 线段树区间合并与栈的结合

本文描述了一种模拟抗日战争期间地道战的网络模型。模型基于中国北方平原的地道网络,通过算法模拟日军对村庄的攻击及地道破坏情况,以及八路军如何请求最新的地道连接状态并快速恢复被破坏的连接。输入包括村庄数量和事件,输出为每个请求的响应,展示了地道战的战略意义和技术实现。

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

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
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<stack>
using namespace std;
const int maxn=50010;
typedef long long ll;
struct node
{
    int l,r;
    int ml,mr,mx;
}tree[maxn<<2];
int n,m;
void build(int l,int r,int cur)
{
    if(l==r)
    {
        tree[cur].ml=1;
        tree[cur].mr=1;
        tree[cur].mx=1;
        return;
    }
    int mid=(r+l)>>1;
    build(l,mid,cur*2);
    build(mid+1,r,cur*2+1);
    tree[cur].mx=max(max(tree[cur*2].mx,tree[cur*2+1].mx),tree[cur*2].mr+tree[cur*2+1].ml);
    tree[cur].ml=tree[cur*2].ml;
    tree[cur].mr=tree[cur*2+1].mr;
    if(tree[cur].ml==(mid-l+1))
        tree[cur].ml+=tree[cur*2+1].ml;
    if(tree[cur].mr==(r-mid))
        tree[cur].mr+=tree[cur*2].mr;
}
void update(int l,int r,int cur,int tar,int fg)
{
    if(l==r)
    {
        if(fg)
        {
            tree[cur].ml=1;
            tree[cur].mr=1;
            tree[cur].mx=1;
        }
        else
        {
            tree[cur].ml=0;
            tree[cur].mr=0;
            tree[cur].mx=0;
        }
        return;
    }
    int mid=(r+l)>>1;
    if(tar<=mid) update(l,mid,cur*2,tar,fg);
    else update(mid+1,r,cur*2+1,tar,fg);
    tree[cur].mx=max(max(tree[cur*2].mx,tree[cur*2+1].mx),tree[cur*2].mr+tree[cur*2+1].ml);
    tree[cur].ml=tree[cur*2].ml;
    tree[cur].mr=tree[cur*2+1].mr;
    if(tree[cur].ml==(mid-l+1))
        tree[cur].ml+=tree[cur*2+1].ml;
    if(tree[cur].mr==(r-mid))
        tree[cur].mr+=tree[cur*2].mr;

}
int query(int l,int r,int cur,int tar)
{
    if(tree[cur].mx==0||tree[cur].mx==(r-l+1)||l==r)
    {
        return tree[cur].mx;
    }
    int mid=(r+l)>>1;
    if(tar<=mid)
    {
        if(tar>mid-tree[cur*2].mr)
        {
            return tree[cur*2].mr+tree[cur*2+1].ml;
        }
        else
            return query(l,mid,cur*2,tar);
    }
    else
    {
        if(tar<mid+1+tree[cur*2+1].ml)
        {
            return tree[cur*2].mr+tree[cur*2+1].ml;
        }
        else
            return query(mid+1,r,cur*2+1,tar);
    }
}
int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        char op[2];
        int x;
        stack<int> s;
        build(1,n,1);
        for(int i=1;i<=m;i++)
        {
            scanf("%s",op);
            if(op[0]=='D')
            {
                scanf("%d",&x);
                s.push(x);
                update(1,n,1,x,0);
            }
            else if(op[0]=='Q')
            {
                scanf("%d",&x);
                printf("%d\n",query(1,n,1,x));
            }
            else
            {
                if(!s.empty())
                {
                    update(1,n,1,s.top(),1);
                    s.pop();
                }
            }
        }
    }

	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值