I - Tunnel Warfare

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<bits/stdc++.h>

using namespace std;

const int MAXN = 50000 + 10;
struct f
{
    int l;
    int r;
    int ls ,rs , ms;
    int len;
}a[4 * MAXN];

int top[MAXN];

void build(int left , int right , int rt)
{
    a[rt].l = left ; a[rt].r = right ;
    a[rt].len = right - left + 1;
    if(left == right)
    {
        a[rt].ls = a[rt].rs = a[rt].ms = 1;
        return;
    }
    int mid = (left + right) >> 1;
    build(left , mid , rt << 1);
    build(mid + 1 ,right , rt << 1 | 1);
    a[rt].ls = a[rt].rs = a[rt].ms = a[rt << 1].ms + a[rt << 1 | 1].ms;
}

void update(int rt , int k , int flag)
{
    if(a[rt].l == a[rt].r)
    {
        if(flag == 1)
            a[rt].ls = a[rt].rs = a[rt].ms = 1;
        else
            a[rt].ls = a[rt].rs = a[rt].ms = 0;
        return;
    }
    int mid = (a[rt].l + a[rt].r) >> 1;
    if(mid >= k)
        update(rt << 1 , k , flag);
    else
        update(rt << 1 | 1 , k , flag);
    if(a[rt << 1].ls == a[rt << 1].len)
        a[rt].ls = a[rt << 1].len + a[rt << 1 | 1].ls;
    else
        a[rt].ls = a[rt << 1].ls;
    if(a[rt << 1 | 1].rs == a[rt << 1 | 1].len)
        a[rt].rs = a[rt << 1 | 1].len + a[rt << 1].rs;
    else
        a[rt].rs = a[rt << 1 | 1].rs;
    a[rt].ms = max (max (a[rt << 1].ms , a[rt << 1 | 1].ms) , a[rt << 1].rs + a[rt << 1| 1].ls);
}

int query(int rt , int k)
{
    if(a[rt].l == a[rt].r || a[rt].ms == a[rt].len || a[rt].ms == 0)
        return a[rt].ms;
    int mid = (a[rt].l + a[rt].r) >> 1;
    if(mid >= k)
    {
        if(k >= a[rt << 1].r - a[rt << 1].rs + 1)
            return query(rt << 1 , k) + query(rt << 1 | 1 , mid + 1);
        else
            return query(rt << 1 , k);
    }
    else
    {
        if(k < a[rt << 1 | 1].l + a[rt << 1 | 1].ls)
            return query(rt << 1 | 1 , k) + query(rt << 1 , mid);
        else
            return query(rt << 1 | 1 , k);
    }
}

int main()
{
    int n , m , k;
    while(~scanf("%d %d" , &n , &m))
    {
        memset(a , 0 , sizeof(a));
        memset(top , 0 , sizeof(top));
        build(1 , n , 1);
        int num = 0;
        while(m --)
        {
            char q[2];
            scanf("%s" , q);
            if(q[0] == 'D')
            {
                scanf("%d" , &k);
                num ++;
                top[num] = k;
                update(1, k , 0);
            }
            else if(q[0] == 'R')
            {
                update(1 , top[num] , 1);
                num --;
            }
            else
            {
                scanf("%d" , &k);
                printf("%d\n" , query(1 , k));
            }
        }

    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/ant_e_zz/article/details/81193538