poj 2892 Tunnel Warfare 查询从x开始向左右能到达的最大区间 线段树

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 (nm  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:

  1. D x: The x-th village was destroyed.
  2. Q x: The Army commands requested the number of villages that x-th village was directly or indirectly connected with including itself.
  3. 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

Hint

An illustration of the sample input:

      OOOOOOO

D 3   OOXOOOO

D 6   OOXOOXO

D 5   OOXOXXO

R     OOXOOXO

R     OOXOOOO

//

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=201000;
struct Node
{
    int left,right;
    int cval;//当前段的最长连续区间长度
    int lval;//当前段从左端点开始的最长连续区间长度
    int rval;//当前段从右端点开始的最长连续区间长度
};
Node tree[maxn];


inline int getLen(int id)//当前段的总长度
{
    return tree[id].right-tree[id].left+1;
}
void buildtree(int id,int l,int r)//建树
{
    tree[id].left=l,tree[id].right=r;
    tree[id].cval=tree[id].lval=tree[id].rval=getLen(id);
    if(l!=r)
    {
        int mid=(l+r)>>1;
        buildtree(id<<1,l,mid);
        buildtree((id<<1)|1,mid+1,r);
    }
}
//查看是否存在连续长度为need的区间,若存在返回左端点,否则返回0
int search(int id,int need)
{
    if(tree[id].cval<need) return 0;
    if(tree[id].lval>=need) return tree[id].left;
    if(tree[id<<1].cval>=need) return search(id<<1,need);
    else if(tree[id<<1].rval+tree[(id<<1)|1].lval>=need)
            return tree[id<<1].right-tree[id<<1].rval+1;
    else return search((id<<1)|1,need);
}
//填充或者删除区间:state:0 填充,state:1删除
void update(int id,int l,int r,int state)
{
    if(tree[id].left>=l&&tree[id].right<=r)//当前段在要修改的区间内,不需要再往下修改
    {
        if(!state) tree[id].cval=tree[id].lval=tree[id].rval=0;
        else tree[id].cval=tree[id].lval=tree[id].rval=getLen(id);
        return ;
    }
    //当前段拆开,弥补上一步操作
    if(tree[id].cval==getLen(id))//没有放置东西
    {
        tree[id<<1].cval=tree[id<<1].lval=tree[id<<1].rval=getLen(id<<1);
        tree[(id<<1)|1].cval=tree[(id<<1)|1].lval=tree[(id<<1)|1].rval=getLen((id<<1)|1);
    }
    if(tree[id].cval==0)//东西已经放满当前段
    {
        tree[id<<1].cval=tree[id<<1].lval=tree[id<<1].rval=0;
        tree[(id<<1)|1].cval=tree[(id<<1)|1].lval=tree[(id<<1)|1].rval=0;
    }
    int mid=(tree[id].left+tree[id].right)>>1;
    if(r<=mid) update(id<<1,l,r,state);
    else if(l>=mid+1) update((id<<1)|1,l,r,state);
    else
    {
        update(id<<1,l,mid,state);
        update((id<<1)|1,mid+1,r,state);
    }
    //更新当点段
    tree[id].lval=tree[id<<1].lval;
    if(tree[id].lval==getLen(id<<1)) tree[id].lval+=tree[(id<<1)|1].lval;
    tree[id].rval=tree[(id<<1)|1].rval;
    if(tree[id].rval==getLen((id<<1)|1)) tree[id].rval+=tree[id<<1].rval;
    tree[id].cval=tree[id<<1].rval+tree[(id<<1)|1].lval;
    tree[id].cval=max(tree[id<<1].cval,tree[id].cval);
    tree[id].cval=max(tree[(id<<1)|1].cval,tree[id].cval);
}
//从x开始向左右能到达的最大区间
int query(int id,int x)
{
    if(tree[id].cval==getLen(id)) return getLen(id);
    if(tree[id].cval==0) return 0;
    int mid=(tree[id].left+tree[id].right)>>1;
    if(x<=mid)
    {
        if(mid-tree[id<<1].rval+1<=x) //x在左面且有向右能移动
        {
            return tree[id<<1].rval+tree[(id<<1)|1].lval;
        }
        return query(id<<1,x);//查询左区间
    }
    else
    {
        if(mid+1+tree[(id<<1)|1].lval-1>=x)
        {
            return tree[id<<1].rval+tree[(id<<1)|1].lval;
        }
        return query((id<<1)|1,x);//查询右区间
    }
}
char str[10];
int stc[maxn];
int stp;
int main()
{
    int n,ci;
    while(scanf("%d%d",&n,&ci)==2)
    {
        buildtree(1,1,n);
        stp=0;
        while(ci--)
        {
            scanf("%s",str);
            if(str[0]=='D')//摧毁
            {
                int x;scanf("%d",&x);
                stc[stp++]=x;
                update(1,x,x,0);
            }
            else if(str[0]=='R')//恢复上一次被摧毁的位置
            {
                if(stp==0) continue;
                int x=stc[--stp];
                update(1,x,x,1);
            }
            else //查询从x开始向左右能到达的最大区间
            {
                int x;scanf("%d",&x);
                int len=query(1,x);
                printf("%d\n",len);
            }
        }
    }
    return 0;
}

你可能感兴趣的:(poj 2892 Tunnel Warfare 查询从x开始向左右能到达的最大区间 线段树)