Tunnel Warfare

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
摘要由CSDN通过智能技术生成

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<stdio.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const int MAX_N=5e4 + 10;
const int MOD = 1e9 + 7;
struct Segmenttree{
    int l, r;
    int ls, rs, ms;
    #define l(x) tree[x].l
    #define r(x) tree[x].r
    //最左的连续区间
    #define ls(x) tree[x].ls
    //最右的连续区间
    #define rs(x) tree[x].rs
    //最大的连续区间
    #define ms(x) tree[x].ms
}tree[4 * MAX_N];
int N, M;
int st[MAX_N], top;
void build(int p, int l, int r){
    l(p) = l; r(p) = r;
    ls(p) = rs(p) = ms(p) =  r - l + 1;
    if(l == r){
        return;
    }
    int mid = (l(p) + r(p) ) / 2;
    build(p * 2, l, mid);
    build(p * 2 + 1, mid + 1, r);
}
void change(int p, int x, int v){
    if(l(p) == r(p) ){
        ls(p) = rs(p) = ms(p) = v;
        return;
    }
    int mid = (l(p) + r(p) ) / 2;
    if(x <= mid) change(p * 2, x, v);
    if(x > mid) change(p * 2 + 1, x, v);
    //最大的连续区间只可能是,左子树的最大区间,右子树的最大区间,左子树右区间和右子树左区间和,三者中的最大值
    ms(p) = max(max(ms(p * 2), ms(p * 2 + 1) ), rs(p * 2) + ls(p * 2 + 1) );
    //左子树满的,则左区间需加上右子树左区间
    if(ls(p * 2) == r(p * 2) - l(p * 2) + 1){
        ls(p) = ls(p * 2) + ls(p * 2 + 1);
    }else{
        ls(p) = ls(p * 2);
    }
    //同理
    if(rs(p * 2 + 1) == r(p * 2 + 1) - l(p * 2 + 1) + 1){
        rs(p) = rs(p * 2 + 1) + rs(p * 2);
    }else{
        rs(p) = rs(p * 2 + 1);
    }
}
int ask(int p, int x){
    //访问节点为 叶节点 或者 为空 或者 为满 可直接返回
    if(l(p) == r(p) || ms(p) == 0 || ms(p) == r(p) - l(p) + 1){
        return ms(p);
    }
    int mid = (l(p) + r(p) ) / 2;
    if(x <= mid){
        //查询点位于左子树中, 当查询节点超过 左子树右区间的左边界,则需加上右子树的左区间
        if(x >= r(p * 2) - rs(p * 2) + 1){
            return  ask(p * 2, x) + ask(p * 2 + 1, mid + 1);
        }else{
            return ask(p * 2, x);
        }
    }else{
        //同理
        if(x <= l(p * 2 + 1) + ls(p * 2 + 1) - 1){
            return ask(p * 2 + 1, x) + ask(p * 2, mid);
        }else{
            return ask(p * 2 + 1, x);
        }
    }
}
void solve(){
    char s[2];
    int x;
    while(scanf("%d%d", &N, &M) != EOF){
        top = 0;
        build(1, 1, N);
        while(M--){
            scanf("%s", s);
            if(s[0] == 'D'){
                scanf("%d", &x);
                st[top++] = x;
                change(1, x, 0);
            }else if(s[0] == 'R'){
                    x = st[--top];
                    change(1, x, 1);
            }else{
                scanf("%d", &x);
                printf("%d\n", ask(1, x));
            }
        }
    }
}
int main(){
    int TCASE = 1;
//    scanf("%d", &TCASE);
    while(TCASE--){
        solve();
    }
    return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值