Tunnel Warfare(线段树 + 区间合并)

Tunnel Warfare

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


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(1 ~ 50000),代表有 n 个连续的城市 和 m 个操作,操作有 3 类,D ans,代表要把编号为 ans 的城市毁灭,R 代表要重建最后毁灭的那么 城市,Q ans 代表询问包含 ans 的完好的连续城市有几个。每次 Q 给出一个结果。

  

      思路:

      线段树,区间合并。注意 updata 的时候要判断往左还是往右,如果不判断的话,则会 TLE。

 

      AC:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <stack>

using namespace std;

const int MAX = 500050;

int lmax[MAX], rmax[MAX], mmax[MAX];

void push_up(int node, int l, int r) {
        lmax[node] = lmax[node << 1];
        rmax[node] = rmax[node << 1 | 1];

        int mid = (r + l) >> 1;
        if (lmax[node] == mid - l + 1) lmax[node] += lmax[node << 1 | 1];
        if (rmax[node] == r - mid) rmax[node] += rmax[node << 1];

        mmax[node] = max(rmax[node << 1] + lmax[node << 1 | 1],
                         max(mmax[node << 1], mmax[node << 1 | 1]));
}

void build (int node, int l, int r) {
        if (l == r) {
                lmax[node] = rmax[node] = mmax[node] = 1;
        } else {
                int mid = (r + l) >> 1;
                build(node << 1, l, mid);
                build(node << 1 | 1, mid + 1, r);
                push_up(node, l, r);
        }
}

void update(int node, int l, int r, int loc, int s) {
        if (l == r) {
                if (l == loc) {
                        mmax[node] = lmax[node] =
                        rmax[node] = (s ? 0 : 1);
                }
                return;
        }

        int mid = (r + l) >> 1;
        if (loc <= mid) update(node << 1, l, mid, loc, s);
        else update(node << 1 | 1, mid + 1, r, loc, s);
        push_up(node, l, r);
}

int query (int node, int l, int r, int loc) {
        int mid = (r + l) >> 1;

        if (mmax[node] == r - l + 1 || !mmax[node])
            return mmax[node];

        if (loc <= mid) {
                if (loc >= mid - rmax[node << 1] + 1)
                        return query(node << 1, l, mid, loc) +
                               query(node << 1 | 1, mid + 1, r, mid + 1);
                else return query(node << 1, l, mid, loc);
        } else {
                if (loc <= mid + 1 + lmax[node << 1 | 1] - 1)
                        return query(node << 1, l, mid, mid) +
                               query(node << 1 | 1, mid + 1, r, loc);
                else return query(node << 1 | 1, mid + 1, r, loc);
        }
}

int main() {
        int n, m;

        while(~scanf("%d%d", &n, &m)) {
                stack<int> s;

                build (1, 1, n);

                while (m--) {
                        char op;
                        scanf(" %c", &op);

                        if (op == 'D') {
                                int loc;
                                scanf("%d", &loc);
                                update(1, 1, n, loc, 1);
                                s.push(loc);
                        } else if (op == 'Q') {
                                int loc;
                                scanf("%d", &loc);

                                printf("%d\n", query(1, 1, n, loc));

                        } else {
                                int loc = s.top();
                                s.pop();
                                update(1, 1, n, loc, 0);
                        }
                }

        }

        return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值