【hdu 1540】Tunnel Warfare 线段树

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<stdio.h>
#include<string.h>
#include<string>
#include<iostream>
#include<algorithm>
#include<queue>
#include<math.h>
#include<map>
#include<vector>
#include<stack>
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll;
const int N=50005;

struct tre
{
    int r,m,l;//r表示右边的连续为1的长度,l为左边,m代表最长连续长度
}tree[N*4];
int n;
stack<int>t;//存破坏的村庄

void build(int a,int b,int r)//建树
{
    tree[r].r=tree[r].m=tree[r].l=b-a+1;
    if(a!=b)
    {
        int mid=(a+b)/2;
        build(a,mid,r<<1);
        build(mid+1,b,r<<1|1);
    }
}

void update(int a,int b,int r,int x,int op)
{
    if(a==b)
    {
        if(op==0)//破坏的村庄
            tree[r].m=tree[r].l=tree[r].r=0;
        else//修复的村庄
            tree[r].m=tree[r].l=tree[r].r=1;
        return;
    }
    int mid=(a+b)/2;
    if(mid>=x)
        update(a,mid,r<<1,x,op);
    else
        update(mid+1,b,r<<1|1,x,op);
    tree[r].l=tree[r<<1].l;//有区间
    tree[r].r=tree[r<<1|1].r;//左区间
    tree[r].m=max(max(tree[r<<1].m,tree[r<<1|1].m),tree[r<<1].r+tree[r<<1|1].l);
    //父亲区间内的最大区间必定是:左子树最大区间,右子树最大区间,左右子树合并的中间区间,三者中最大的区间值
    if(tree[r<<1].l==mid-a+1)//如果左子树区间满了的话,父亲左区间要加上右孩子的左区间
        tree[r].l+=tree[r<<1|1].l;
    if(tree[r<<1|1].r==b-mid)//同理
        tree[r].r+=tree[r<<1].r;
}

int qurey(int a,int b,int r,int x)
{
    if(a==b||tree[r].m==0||tree[r].m==b-a+1)
        return tree[r].m;
    int mid=(a+b)/2;
    if(mid>=x)
    {
        if(x>=mid-tree[r<<1].r+1)
        //因为t<=mid,看左子树,a[2*i].r-a[2*i].rs+1代表左子树右边连续区间
        //的左边界值,如果t在左子树的右区间内,则要看右子树的左区间有多长并返回
            return qurey(a,mid,r<<1,x)+qurey(mid+1,b,r<<1|1,mid+1);
        else//如果不在左子树的右边界区间内,则只需要看左子树
            return qurey(a,mid,r<<1,x);
    }
    else
    {
        if(x<=mid+tree[r<<1|1].l)//同理
            return qurey(mid+1,b,r<<1|1,x)+qurey(a,mid,r<<1,mid);
        else
            return qurey(mid+1,b,r<<1|1,x);
    }
}

int main()
{
    int m,i,a;
    char op;
    while(~scanf("%d %d",&n,&m))
    {
        memset(tree,0,sizeof(tre));
        build(1,n,1);
        for(i=0; i<m; i++)
        {
            getchar();
            scanf("%c",&op);
            if(op=='D')
            {
                scanf("%d",&a);
                update(1,n,1,a,0);
                t.push(a);
            }
            else if(op=='Q')
            {
                scanf("%d",&a);
                printf("%d\n",qurey(1,n,1,a));
            }
            else
            {
                int temp=t.top();
                t.pop();
                update(1,n,1,temp,1);
            }
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41984014/article/details/82831206