Tunnel Warfare HDU - 1540 (线段树区间合并)

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! 
InputThe 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. 
OutputOutput 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

题意:D代表破坏村庄,R代表修复最后被破坏的那个村庄,Q代表询问包括x在内的最大连续区间是多少.

#include<stdio.h>
#include<string.h>
#include<map>
#include<vector>
#include<iostream>
#include<algorithm>
#include<queue>
#include<deque>
#include<math.h>
#define ll long long
#define ls rt<<1
#define rs rt<<1|1
#define maxn 50000+10
const int inf=0x3f3f3f3f;
using namespace std;
int n,m;
int s[maxn],top;
struct node
{
    int lls,rrs,ms;//lls代表包含最左端的最大连续区间    rrs代表包含最右端的最大连续区间    ms代表最大连续区间
}a[maxn<<2];
void build(int l,int r,int rt)//初始化
{
    a[rt].lls=a[rt].rrs=a[rt].ms=r-l+1;
    if(l!=r)
    {
        int m=(l+r)>>1;
        build(l,m,ls);
        build(m+1,r,rs);
    }
}
void update(int l,int r,int rt,int t,int x)
{
    if(l==r)//到达叶子节点
    {
        if(x==1)
            a[rt].lls=a[rt].rrs=a[rt].ms=1;//重建
        else
            a[rt].lls=a[rt].rrs=a[rt].ms=0;//摧毁
        return ;
    }
    int m=(l+r)>>1;
    if(t<=m)//村庄在左子树
        update(l,m,ls,t,x);
    else //村庄在右子树
        update(m+1,r,rs,t,x);
    a[rt].lls=a[ls].lls;//父亲节点的最大左区间就是左子树的最大左区间
    a[rt].rrs=a[rs].rrs;//同理
    a[rt].ms=max(max(a[ls].ms,a[rs].ms),a[ls].rrs+a[rs].lls);//最大区间  要么是左子树最大区间 要么是右子树最大区间  要么是 左子树右区间+右子树左区间
    if(a[ls].lls==m-l+1) //如果左子树最大左区间就是左子树区间值  ,说明父亲节点的最大左区间还得加上右子树的最大左区间
        a[rt].lls+=a[rs].lls;
    if(a[rs].rrs==r-m)//同理
        a[rt].rrs+=a[ls].rrs;
}
int query(int l,int r,int rt,int t)
{
    if(l==r||a[rt].ms==0||a[rt].ms==r-l+1)//到达叶子节点 或者最大连续区间 为0 要么已经满区间了
        return a[rt].ms;
    int m=(l+r)>>1;
    if(t<=m)//在左子树
    {
        if(t>=m-a[ls].rrs+1)//m-a[ls].rrs+1代表左子树右边连续区间的左边界值,如果t在左子树的右区间内,则要看右子树的左区间有多长并返回
            return query(l,m,ls,t)+query(m+1,r,rs,m+1);
        else
            return query(l,m,ls,t);
    }
    else
    {
        if(t<=m+a[rs].lls)//同理
            return query(m+1,r,rs,t)+query(l,m,ls,m);
        else
            return query(m+1,r,rs,t);
    }
}
int main()
{
    char ch[2];
    int x;
    while(~scanf("%d%d",&n,&m))
    {
        top=0;
        build(1,n,1);
        while(m--)
        {
            scanf("%s",ch);
            if(ch[0]=='D')
            {
                scanf("%d",&x);
                s[top++]=x;
                update(1,n,1,x,0);
            }
            else if(ch[0]=='Q')
            {
                scanf("%d",&x);
                printf("%d\n",query(1,n,1,x));
            }
            else
            {
                if(top>0)
                {
                    x=s[--top];
                    update(1,n,1,x,1);
                }
            }
        }
    }
}



猜你喜欢

转载自blog.csdn.net/dsaghjkye/article/details/81042276