1. 程式人生 > >HDU - 1540 Tunnel Warfare 線段樹區間合併與棧的結合

HDU - 1540 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<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<stack>
using namespace std;
const int maxn=50010;
typedef long long ll;
struct node
{
    int l,r;
    int ml,mr,mx;
}tree[maxn<<2];
int n,m;
void build(int l,int r,int cur)
{
    if(l==r)
    {
        tree[cur].ml=1;
        tree[cur].mr=1;
        tree[cur].mx=1;
        return;
    }
    int mid=(r+l)>>1;
    build(l,mid,cur*2);
    build(mid+1,r,cur*2+1);
    tree[cur].mx=max(max(tree[cur*2].mx,tree[cur*2+1].mx),tree[cur*2].mr+tree[cur*2+1].ml);
    tree[cur].ml=tree[cur*2].ml;
    tree[cur].mr=tree[cur*2+1].mr;
    if(tree[cur].ml==(mid-l+1))
        tree[cur].ml+=tree[cur*2+1].ml;
    if(tree[cur].mr==(r-mid))
        tree[cur].mr+=tree[cur*2].mr;
}
void update(int l,int r,int cur,int tar,int fg)
{
    if(l==r)
    {
        if(fg)
        {
            tree[cur].ml=1;
            tree[cur].mr=1;
            tree[cur].mx=1;
        }
        else
        {
            tree[cur].ml=0;
            tree[cur].mr=0;
            tree[cur].mx=0;
        }
        return;
    }
    int mid=(r+l)>>1;
    if(tar<=mid) update(l,mid,cur*2,tar,fg);
    else update(mid+1,r,cur*2+1,tar,fg);
    tree[cur].mx=max(max(tree[cur*2].mx,tree[cur*2+1].mx),tree[cur*2].mr+tree[cur*2+1].ml);
    tree[cur].ml=tree[cur*2].ml;
    tree[cur].mr=tree[cur*2+1].mr;
    if(tree[cur].ml==(mid-l+1))
        tree[cur].ml+=tree[cur*2+1].ml;
    if(tree[cur].mr==(r-mid))
        tree[cur].mr+=tree[cur*2].mr;

}
int query(int l,int r,int cur,int tar)
{
    if(tree[cur].mx==0||tree[cur].mx==(r-l+1)||l==r)
    {
        return tree[cur].mx;
    }
    int mid=(r+l)>>1;
    if(tar<=mid)
    {
        if(tar>mid-tree[cur*2].mr)
        {
            return tree[cur*2].mr+tree[cur*2+1].ml;
        }
        else
            return query(l,mid,cur*2,tar);
    }
    else
    {
        if(tar<mid+1+tree[cur*2+1].ml)
        {
            return tree[cur*2].mr+tree[cur*2+1].ml;
        }
        else
            return query(mid+1,r,cur*2+1,tar);
    }
}
int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        char op[2];
        int x;
        stack<int> s;
        build(1,n,1);
        for(int i=1;i<=m;i++)
        {
            scanf("%s",op);
            if(op[0]=='D')
            {
                scanf("%d",&x);
                s.push(x);
                update(1,n,1,x,0);
            }
            else if(op[0]=='Q')
            {
                scanf("%d",&x);
                printf("%d\n",query(1,n,1,x));
            }
            else
            {
                if(!s.empty())
                {
                    update(1,n,1,s.top(),1);
                    s.pop();
                }
            }
        }
    }

	return 0;
}