hdu - 1540

Posted by WildCow on February 2, 2018

Tunnel Warfare

Time Limit: 4000/2000 MS (Java/Others)
Memory Limit: 65536/32768 K (Java/Others)

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


给你一个初始状态全为 1 的区间
单点修改,把 1 变成 0,把 0 变成 1
输入一个 x
查询 x 所在的最长连续全 1 区间

线段树的区间合并卡了很久很久没有写
当初学线段树的时候因为这个直接放弃继续学线段树
今天补掉了
其实很多东西写出来以后就会发现其实并不难
刚开始写成了全数字的区间合并
难受得直接想放弃= =!
发现是 01 区间合并以后乖乖的搓完了
1Y还是比较开心

查询比较奇怪
自己想只想出了对于每次查询
二分边界O(logn * logn)的解法
不知道会不会被T

单纯的看代码挺难理解这种查询的
画一画图就很轻松的理解

这题数据好像没啥坑点


#include <bits/stdc++.h>
using namespace std;

const int N = 50050;

struct Tree
{
    int l;
    int r;
    int ll;
    int rl;
    int ml;
};

Tree tree[N * 4];
int n, m;
int st[N];
int now;

void up(int x)
{
    tree[x].ll = tree[x << 1].ll;
    tree[x].rl = tree[x << 1 | 1].rl;
    tree[x].ml = max(max(tree[x << 1].ml, tree[x << 1 | 1].ml), tree[x << 1].rl + tree[x << 1 | 1].ll);
    if(tree[x << 1].ll == tree[x << 1].r - tree[x << 1].l + 1){
        tree[x].ll += tree[x << 1 | 1].ll;
    }
    if(tree[x << 1 | 1].ll == tree[x << 1 | 1].r - tree[x << 1 | 1].l + 1){
        tree[x].rl += tree[x << 1].rl;
    }
}

void build(int x, int l, int r)
{
    tree[x].l = l;
    tree[x].r = r;
    if(l == r){
        tree[x].ll = tree[x].ml = tree[x].rl = 1;
    }
    else{
        int mid;

        mid = l + ((r - l) >> 1);
        build(x << 1, l, mid);
        build(x << 1 | 1, mid + 1, r);
        up(x);
    }
}

void update(int x, int t, int v)
{
    int l = tree[x].l;
    int r = tree[x].r;

    if(tree[x].l == tree[x].r && tree[x].l == t){
        tree[x].ll = tree[x].rl = tree[x].ml = v;
    }
    else{
        int mid;

        mid = l + ((r - l) >> 1);
        if(t <= mid){
            update(x << 1, t, v);
        }
        else{
            update(x << 1 | 1, t, v);
        }
        up(x);
    }
}

int query(int x, int v)
{
    int l = tree[x].l;
    int r = tree[x].r;
    int mid;

    if(l == r || !tree[x].ml || r - l + 1 == tree[x].ml){
        return tree[x].ml;
    }
    mid = l + ((r - l) >> 1);
    if(v <= mid){
        if(v >= tree[x << 1].r - tree[x << 1].rl + 1){
            return query(x << 1, v) + query(x << 1 | 1, mid + 1);
           }
           else{
               return query(x << 1, v);
           }
    }
    else{
        if(v <= tree[x << 1 | 1].l + tree[x << 1 | 1].ll - 1){
            return query(x << 1 | 1, v) + query(x << 1, mid);
        }
        else{
            return query(x << 1 | 1, v);
        }
    }
}

int main(int argc, char const *argv[])
{
    while(~scanf("%d%d", &n, &m)){
        now = 0;
        build(1, 1, n);
        for(int i = 0; i < m; i ++){
            char ma[5];

            scanf("%s", ma);
            if(ma[0] == 'D'){
                int x;

                scanf("%d", &x);
                st[now] = x;
                now ++;
                update(1, x, 0);
            }
            else if(ma[0] == 'Q'){
                int x;

                scanf("%d", &x);
                printf("%d\n", query(1, x));
            }
            else{
                int x;

                now --;
                x = st[now];
                update(1, x, 1);
            }
        }
    }

    return 0;
}