zoukankan      html  css  js  c++  java
  • HDU 1540 Tunnel Warfare (线段树区间合并)

    Tunnel Warfare

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


    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
     
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <iostream>
    #include <set>
    #include <stack>
    using namespace std;
    #define lson l, m, rt<<1
    #define rson m+1, r, rt<<1|1
    const int maxn = 50010;
    int lsum[maxn<<2], rsum[maxn<<2];
    
    void PushUp(int rt, int m) {
        lsum[rt] = lsum[rt<<1];
        rsum[rt] = rsum[rt<<1|1];
        if(lsum[rt] == m - (m >> 1)) lsum[rt] += lsum[rt<<1|1];
        if(rsum[rt] == m >> 1) rsum[rt] += rsum[rt<<1];
    }
    
    void build(int l, int r, int rt) {
        lsum[rt] = rsum[rt] = r - l + 1;
        if(l == r) return;
        int m = (l + r) >> 1;
        build(lson);
        build(rson);
    }
    
    void update(int p, int c, int l, int r, int rt) {
        if(l == r) {
            lsum[rt] = rsum[rt] = c;
            return;
        }
        int m = (l + r) >> 1;
        if(p <= m) update(p, c, lson);
        else update(p, c, rson);
        PushUp(rt, r - l + 1);
    }
    int flag;
    int query(int p, int l, int r, int rt) {
        if(rsum[rt] >= r - p + 1) {
            flag = 1;
            return rsum[rt];
        }
        if(lsum[rt] >= p - l + 1) {
            flag = 1;
            return lsum[rt];
        }
        int t;
        if(l == r) return 0;
        int m = (l + r) >> 1;
        if(p > m) {
            t = query(p, rson);
            if(flag) {
                flag = 0;
                t += rsum[rt<<1];
            }
        }
        else {
            t = query(p, lson);
            if(flag) {
                flag = 0;
                t += lsum[rt<<1|1];
            }
        }
        return t;
    }
    
    int main() {
        int n, m;
        while(~scanf("%d %d", &n, &m)) {
            flag = 0;
            build(1, n, 1);
            stack<int> stk;
            for(int i = 1; i <= m; i++) {
                char cmd[2];
                scanf("%s", cmd);
                if(cmd[0] == 'D') {
                    int tmp;
                    scanf("%d", &tmp);
                    stk.push(tmp);
                    update(tmp, 0, 1, n, 1);
                }
                else if(cmd[0] == 'Q') {
                    int tmp;
                    scanf("%d", &tmp);
                    flag = 0;
                    printf("%d
    ", query(tmp, 1, n, 1));
                }
                else if(cmd[0] == 'R') {
                    update(stk.top(), 1, 1, n, 1);
                    stk.pop();
                }
            }
        }
    }
  • 相关阅读:
    帝国cms 同个IP可提交一次
    帝国cms 图片相对路径绝对路径设置问题+帝国cms 手机端调用图片问题
    帝国CMS 手机版制作+帝国PC跳转到手机+重新定向
    帝国cms 相关问题
    简洁 清晰弹出层讲解制作(图片点击放大)
    webstorm 2016最新版破解+汉化
    ajax点击不断加载数据列表
    js中substr,substring,indexOf,lastIndexOf,split的用法
    Parallax.js|强大的javascript视觉差特效引擎
    一行代码解决各种IE兼容问题,IE6,IE7,IE8,IE9,IE10
  • 原文地址:https://www.cnblogs.com/lonewanderer/p/5648136.html
Copyright © 2011-2022 走看看