HDU-1540 Tunnel Warfare (线段树区间合并)

来源:互联网 发布:cnc编程培训班 编辑:程序博客网 时间:2024/05/22 01:29

Tunnel Warfare

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


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表示摧毁当前村庄,R表示恢复上一个被摧毁的村庄,Q进行查询问包含X在内的连续村庄有多少个。

题目思路:本题算是一个线段树区间合并查询最长区间的经典题了,具体操作会在代码中给出解释。


AC代码如下:

#include <bits/stdc++.h>#define INF 0x3f3f3f3f#define FIN freopen("in.txt","r",stdin)#define fuck(x) cout<<'['<<x<<']'<<endl#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1using namespace std;typedef long long LL;typedef pair<int, int>pii;const int MX = 5e4 + 10;struct node {    int l, r;    int lx, rx, mx;//lx表示左端的最大连续区间,rx表示右端的最大连续区间,mx表示整个区间的最大连续区间;} tree[MX << 2];int vis[MX];//数组模拟记录被摧毁的村庄,以便更新;int n, m;void build(int l, int r, int rt) {//建树;    tree[rt].l = l; tree[rt].r = r;    tree[rt].lx = tree[rt].rx = tree[rt].mx =  r - l + 1;    if(l == r) return;    int m = (l + r) >> 1;    build(lson);    build(rson);}void update(int p, int op, int l, int r, int rt) {    if(l == r) {        if(op)            tree[rt].lx = tree[rt].rx = tree[rt].mx = 1;//修复村庄;        else            tree[rt].lx = tree[rt].rx = tree[rt].mx = 0;//摧毁村庄;        return;    }    int m = (l + r) >> 1;    if(p <= m) update(p, op, lson);    else update(p, op, rson);    tree[rt].lx = tree[rt << 1].lx;    //父节点的左端最大连续区间等于左儿子节点的左端最大连续区间;    tree[rt].rx = tree[rt << 1 | 1].rx;    //父节点的右端最大连续区间等于右儿子节点的右端最大连续区间;    tree[rt].mx = max(max(tree[rt << 1].mx, tree[rt << 1 | 1].mx), tree[rt << 1].rx + tree[rt << 1 | 1].lx);    //父亲节点的最大连续区间等于左儿子左儿子,右儿子的最大连续区间,左儿子与右儿子合并后的中间区间长度的最大值;    if(tree[rt << 1].lx == tree[rt << 1].r - tree[rt << 1].l + 1)        tree[rt].lx += tree[rt << 1 | 1].lx;    //如果左儿子的左端最大连续区间是满的,那么父节点的左端最大连续区间得加上右儿子的左端最大连续区间;    if(tree[rt << 1 | 1].rx == tree[rt << 1 | 1].r - tree[rt << 1 | 1].l + 1)        tree[rt].rx += tree[rt << 1].rx;//右儿子同理;}int query(int p, int l, int r, int rt) {    if(tree[rt].l == tree[rt].r || tree[rt].mx == 0 || tree[rt].mx == tree[rt].r - tree[rt].l + 1) {        //当递归到叶子节点的时候即可返回区间最大值,区间为空或者满的时候也可以不用继续往下查询了;        return tree[rt].mx;    }    int m = (l + r) >> 1;    if(p <= m) {//当p<=m时,便向当前节点的左儿子继续查询,        if(p >= tree[rt << 1].r - tree[rt << 1].rx + 1)        //tree[rt<<1].r-tree[rt<<1].rx+1为左儿子节点的右区间的右端点,如果p大于右端点,那么就得加上右儿子的左区间的一部分;            return query(p, lson) + query(m + 1, rson);        else            return query(p, lson);//否则直接往左儿子查询即可;    } else {    //查询右儿子也与查询左儿子同理;        if(p <= tree[rt << 1 | 1].l + tree[rt << 1 | 1].lx - 1)            return query(p, rson) + query(m, lson);        else            return query(p, rson);    }}int main() {    //FIN;    while(~scanf("%d%d", &n, &m)) {        build(1, n, 1);        char op[2];        int x, cnt = 0;        while(m--) {            scanf("%s", op);            if(op[0] == 'D') {                scanf("%d", &x);                update(x, 0, 1, n, 1);                vis[++cnt] = x;//用vis记录当前被摧毁的是哪一个村庄;            } else if(op[0] == 'Q') {                scanf("%d", &x);                printf("%d\n", query(x, 1, n, 1));            } else {                if(cnt > 0) {                    update(vis[cnt], 1, 1, n, 1);//将上一个被摧毁的村庄恢复;                    cnt--;//将被恢复的村庄从数组内移除;                }            }        }    }    return 0;}


阅读全文
0 0
原创粉丝点击