I - 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<bits/stdc++.h>

using namespace std;

const int MAXN = 50000 + 10;
struct f
{
    int l;
    int r;
    int ls ,rs , ms;
    int len;
}a[4 * MAXN];

int top[MAXN];

void build(int left , int right , int rt)
{
    a[rt].l = left ; a[rt].r = right ;
    a[rt].len = right - left + 1;
    if(left == right)
    {
        a[rt].ls = a[rt].rs = a[rt].ms = 1;
        return;
    }
    int mid = (left + right) >> 1;
    build(left , mid , rt << 1);
    build(mid + 1 ,right , rt << 1 | 1);
    a[rt].ls = a[rt].rs = a[rt].ms = a[rt << 1].ms + a[rt << 1 | 1].ms;
}

void update(int rt , int k , int flag)
{
    if(a[rt].l == a[rt].r)
    {
        if(flag == 1)
            a[rt].ls = a[rt].rs = a[rt].ms = 1;
        else
            a[rt].ls = a[rt].rs = a[rt].ms = 0;
        return;
    }
    int mid = (a[rt].l + a[rt].r) >> 1;
    if(mid >= k)
        update(rt << 1 , k , flag);
    else
        update(rt << 1 | 1 , k , flag);
    if(a[rt << 1].ls == a[rt << 1].len)
        a[rt].ls = a[rt << 1].len + a[rt << 1 | 1].ls;
    else
        a[rt].ls = a[rt << 1].ls;
    if(a[rt << 1 | 1].rs == a[rt << 1 | 1].len)
        a[rt].rs = a[rt << 1 | 1].len + a[rt << 1].rs;
    else
        a[rt].rs = a[rt << 1 | 1].rs;
    a[rt].ms = max (max (a[rt << 1].ms , a[rt << 1 | 1].ms) , a[rt << 1].rs + a[rt << 1| 1].ls);
}

int query(int rt , int k)
{
    if(a[rt].l == a[rt].r || a[rt].ms == a[rt].len || a[rt].ms == 0)
        return a[rt].ms;
    int mid = (a[rt].l + a[rt].r) >> 1;
    if(mid >= k)
    {
        if(k >= a[rt << 1].r - a[rt << 1].rs + 1)
            return query(rt << 1 , k) + query(rt << 1 | 1 , mid + 1);
        else
            return query(rt << 1 , k);
    }
    else
    {
        if(k < a[rt << 1 | 1].l + a[rt << 1 | 1].ls)
            return query(rt << 1 | 1 , k) + query(rt << 1 , mid);
        else
            return query(rt << 1 | 1 , k);
    }
}

int main()
{
    int n , m , k;
    while(~scanf("%d %d" , &n , &m))
    {
        memset(a , 0 , sizeof(a));
        memset(top , 0 , sizeof(top));
        build(1 , n , 1);
        int num = 0;
        while(m --)
        {
            char q[2];
            scanf("%s" , q);
            if(q[0] == 'D')
            {
                scanf("%d" , &k);
                num ++;
                top[num] = k;
                update(1, k , 0);
            }
            else if(q[0] == 'R')
            {
                update(1 , top[num] , 1);
                num --;
            }
            else
            {
                scanf("%d" , &k);
                printf("%d\n" , query(1 , k));
            }
        }

    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值