Tunnel Warfare HDU - 1540

本文介绍了一种使用线段树数据结构来模拟抗日战争期间中国北方平原上广泛使用的地道战战术的网络模拟方法。该模拟涉及到对连续村庄的连接状态进行查询和更新,通过线段树记录区间内村庄的连续连接长度,实现对破坏、重建和查询操作的高效处理。

摘要生成于 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

题意

给你一到n连续的数字,三种操作,(1)D 去掉数字x (2) Q 查询与x所在连续数字长度 (3) R 恢复上次去掉的数字

思路

用线段树记录当前区间左边连续长度 右边连续长度 和区间内的长度。

#include <bits/stdc++.h>

using namespace std;
const int maxn = 50050;
struct node
{
    int l,r;
    int ll,rl,ml;
}tree[maxn*3];

void build(int i,int l,int r)
{
    tree[i].l = l;
    tree[i].r=  r;
    tree[i].ll = tree[i].rl = tree[i].ml = r-l+1;
    if(l == r) return ;
    int m = (l+r)>>1;
    build(i<<1,l,m);
    build((i<<1)|1,m+1,r);
}
void update(int i,int t,int val)
{
    if(tree[i].l == tree[i].r)
    {
        if(val == 1) tree[i].ll = tree[i].rl = tree[i].ml = 1;
        else tree[i].ll = tree[i].rl = tree[i].ml = 0;
        return ;
    }
    int m = (tree[i].l + tree[i].r)>>1;
    if(t <= m) update(i<<1,t,val);
    else update((i<<1)|1,t,val);
    tree[i].ll = tree[i<<1].ll;
    tree[i].rl = tree[(i<<1)|1].rl;
    tree[i].ml = max(tree[i<<1].ml,tree[(i<<1)|1].ml);
    tree[i].ml = max(tree[i].ml,tree[i<<1].rl+tree[(i<<1)|1].ll);

    if(tree[i<<1].ll == tree[i<<1].r - tree[i<<1].l + 1) tree[i].ll += tree[i<<1|1].ll;
    if(tree[(i<<1)|1].rl == tree[(i<<1)|1].r - tree[(i<<1)|1].l +  1) tree[i].rl += tree[i<<1].rl;
}
int query(int i,int t)
{
    if(tree[i].l == tree[i].r||tree[i].ml == 0||tree[i].ml == tree[i].r - tree[i].l + 1)
    {
        return tree[i].ml;
    }
    int m = (tree[i].l + tree[i].r)>>1;
    if(t <= m)
    {
        if(t >= tree[i<<1].r - tree[i<<1].rl + 1)
        {
            return query(i<<1,t) + query((i<<1)|1,m+1);
        }
        else query(i<<1,t);
    }
    else
    {
        if(t <= tree[(i<<1)|1].l + tree[(i<<1)|1].ll-1)
            return query((i<<1)|1,t) + query(i<<1,m);
        else return query((i<<1)|1,t);
    }
}

int que[maxn];
int top;
int main()
{
    int n,m;
    char str[10];
    int x;
    while(scanf("%d%d",&n,&m) != EOF)
    {
        build(1,1,n);
        top = 0;
        while(m--)
        {
            scanf("%s",&str);
            if(str[0] == 'D')
            {
                scanf("%d",&x);
                que[top++] = x;
                update(1,x,0);
            }
            else if(str[0] == 'Q')
            {
                scanf("%d",&x);
                printf("%d\n",query(1,x));
            }
            else
            {
                if(x > 0)
                {
                    x = que[--top];
                    update(1,x,1);
                }
            }
        }
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值