当前位置: 首页 > 工具软件 > mod_tunnel > 使用案例 >

Tunnel Warfare

姜晨
2023-12-01

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;
}

 类似资料:

相关阅读

相关文章

相关问答