hdu 1540 Tunnel Warfare 线段树

Tunnel Warfare

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 15090    Accepted Submission(s): 5957


 

Problem Description

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个地道,最开始是连续的,接下来有m个操作

 D x 炸掉x号地道

Q x 查询输出包括x的最大连续区间长度

R修复最后一个被炸的地道 

区间合并问题,维护从左开始最大连续区间长度,从右开始最大连续长度,query的时候查找x左边的后缀长度和右边的前缀长度。

#include <cstdio>
#include <vector>
#include <algorithm>
#include <cstring>
using namespace std;

const int N = 1e5 + 10;
#define lson o * 2, l, m
#define rson o * 2 + 1, m + 1, r
#define ls o * 2
#define rs o * 2 + 1

int n, m;
int a[N * 4], b[N * 4], lab[N];
vector<int> last;

void pushup(int o, int l, int r) {
    int m = (l + r) / 2;
    a[o] = (a[ls] == (m - l + 1)) ? a[ls] + a[rs] : a[ls];
    b[o] = (b[rs] == (r - m)) ? b[rs] + b[ls] : b[rs];
}

void build(int o, int l, int r) {
    if (l == r) {
        a[o] = b[o] = 1;
        lab[l] = 1;
        return;
    }
    int m = (l + r) / 2;
    build(lson);
    build(rson);
    pushup(o, l, r);
}

void update(int o, int l, int r, int p, int op) {
    if (l == r) {
        a[o] = b[o] = op;
        return;
    }
    int m = (l + r) / 2;
    if (p <= m) update(lson, p, op);
    else update(rson, p, op);
    pushup(o, l, r);
}

int query_suf(int o, int l, int r, int L, int R) {
    if (L <= l && r <= R) return b[o];
    int m = (l + r) / 2;
    if (R <= m) return query_suf(lson, L, R);
    else if (L > m) return query_suf(rson, L, R);
    else {
        if (m + a[rs] >= R) return m - max(L, m - b[ls] + 1) + 1 + R - m;
        else return query_suf(rson, L, R);
    }
}

int query_pre(int o, int l, int r, int L, int R) {
    if (L <= l && r <= R) return a[o];
    int m = (l + r) / 2;
    if (R <= m) return query_pre(lson, L, R);
    else if (L > m) return query_pre(rson, L, R);
    else {
        if (L + b[ls] > m) return min(R, m + a[rs]) - L + 1;
        else return query_pre(lson, L, R);
    }
}

int main() {
    while (scanf("%d%d", &n, &m) != EOF) {
        build(1, 1, n);
        int  pos;
        char op[3];
        last.clear();
        for (int i = 1; i <= m; i++) {
            scanf("%s", op);
            if (op[0] == 'D') {
                scanf("%d", &pos);
                last.push_back(pos);
                update(1, 1, n, pos, 0);
                lab[pos] = 0;
            }
            else if (op[0] == 'Q') {
                scanf("%d", &pos);
                if (lab[pos] == 0) printf("0\n");
                else {
                    int ans = 1;
                    if (pos != 1) ans += query_suf(1, 1, n, 1, pos - 1);
                    if (pos != n) ans += query_pre(1, 1, n, pos + 1, n);
                    printf("%d\n", ans);
                }
            }
            else if (op[0] == 'R') {
                update(1, 1, n, last.back(), 1);
                lab[last.back()] = 1;
                last.pop_back();
            }
        }
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值