HDU1540 Tunnel Warfare(线段树,区间合并)

Tunnel Warfare

Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 25 Accepted Submission(s): 15
 
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
 
 
Source
POJ Monthly
 
emmm,具体看代码部分吧
#include <iostream>
#include <algorithm>
#include <stack>
#include <cstdio>
using namespace std;
const int maxn = 50010;
int n, m;
struct Node {
    int left, right;
    int ls, ms, rs;//左端最大连续区间,区间内最大连续区间,右端最大连续区间(注意是最大连续区间。这三者可能相等
}tree[maxn << 2];

void init(int node, int left, int right) {
    tree[node].left = left;
    tree[node].right = right;
    tree[node].ls = tree[node].ms = tree[node].rs = right - left + 1;
    
    if (left != right) {
        int mid = (left + right) >> 1;
        init(node << 1, left, mid);
        init(node << 1 | 1, mid + 1, right);
    }
}

void insert(int node, int pos, int val) {
    int l = tree[node].left, r = tree[node].right;
    if (l == r) {
        tree[node].ls = tree[node].rs = tree[node].ms = val;
        return ;
    }
    int mid = (l + r) >> 1;
    
    if (pos <= mid) {
        insert(node << 1, pos, val);
    } else {
        insert(node << 1 | 1, pos, val);
    }
    tree[node].ls = tree[node << 1].ls;//父亲的左连续区间至少等于左二字的左连续区间
    tree[node].rs = tree[node << 1 | 1].rs;//同上
    //区间最大值等于两个儿子中的其中一个最大值,或者左右儿子中间相加的部分
    tree[node].ms = max(max(tree[node << 1].ms, tree[node << 1 | 1].ms), tree[node << 1].rs + tree[node << 1 | 1].ls);
    
    if (tree[node << 1].ls == tree[node << 1].right - tree[node << 1].left + 1)
        tree[node].ls += tree[node << 1 | 1].ls;//左子树的左连续区间值满的话,父节点的左连续区间就加上右子树的
    if (tree[node << 1 | 1].rs == tree[node << 1 | 1].right - tree[node << 1 | 1].left + 1)
        tree[node].rs += tree[node << 1].rs;//同上
    
}

int query(int node, int pos) {
        //符合条件直接返回
    if (tree[node].left == tree[node].right || tree[node].ms == 0 || tree[node].ms == tree[node].right - tree[node].left + 1)
        return tree[node].ms;
    int mid = (tree[node].left + tree[node].right) >> 1;
    
    if (pos <= mid) {//这个地方画个图比较好理解
        if (pos >= tree[node << 1].right - tree[node << 1].rs + 1)
            return query(node << 1, pos) + query(node << 1 | 1, mid + 1);//因为t<=mid,看左子树,a[2*i].r-a[2*i].rs+1代表左子树右边连续区间的左边界值,如果t在左子树的右区间内,则要看右子树的左区间有多长并返回
        else
            return query(node << 1, pos);
    } else {
        if (pos <= tree[node << 1 | 1].ls + tree[node << 1 | 1].left - 1)
            return query(node << 1, mid) + query(node << 1 | 1, pos);
        else
            return query(node << 1 | 1, pos);
    }
}

int main() {
        //freopen("in.txt", "r", stdin);
    char ch[2];
    while (scanf("%d%d", &n, &m) != EOF) {
        stack<int> st;
        init(1, 1, n);
        while (m--) {
            int x;
            scanf("%s", ch);
            if (ch[0] == 'D') {
                scanf("%d", &x);
                st.push(x);
                insert(1, x, 0);
            } else if (ch[0] == 'Q') {
                scanf("%d", &x);
                printf("%d\n", query(1, x));
            } else {
                x = st.top();
                st.pop();
                insert(1, x, 1);
            }
        }
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值