POJ 2892 Tunnel Warfare(线段树单点更新区间合并)

Posted Blackops

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了POJ 2892 Tunnel Warfare(线段树单点更新区间合并)相关的知识,希望对你有一定的参考价值。

Tunnel Warfare
Time Limit: 1000MS   Memory Limit: 131072K
Total Submissions: 7876   Accepted: 3259

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

 

 

 

题目链接:POJ 2892

线段树的区间合并操作相比其他区间操作稍微难理解,但是还是可以写的一下的,主要就是对pushup和query的修改

做法就是seg数据里记录当前区间从左边即seg::l 开始向右连续连通长度,记为L,从右边seg::r 开始的向左连续连通长度,记为R。用seg::M表示是区间中最大的连续连通长度,显然M应该有三种情况,要么为L要么为R要么为左子树的R+右子树的L,最后的情况是必然会要比较的,但是前两种不一定会出现,比如左子树的L并没有达到父节点的mid即左子树的r,因此中间断开了,父节点的L仍时保持为左子树的L,父节点的R也相同道理,然后三者取一个最大的就得到了父节点的M,然后是L和R如何更新就看左子树的L和右子树的R是否完全覆盖了子区间,若完全覆盖则父节点的L与R可以拓展延长……

还有就是查询的时候加一个剪枝条件不然容易TLE,若当前区间已经完全覆盖或者最大连通长度M为0显然是不需要再递归查询的直接返回即可。

代码:

#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<sstream>
#include<cstring>
#include<bitset>
#include<cstdio>
#include<string>
#include<deque>
#include<stack>
#include<cmath>
#include<queue>
#include<set>
#include<map>
using namespace std;
#define INF 0x3f3f3f3f
#define CLR(x,y) memset(x,y,sizeof(x))
#define LC(x) (x<<1)
#define RC(x) ((x<<1)+1)
#define MID(x,y) ((x+y)>>1)
typedef pair<int,int> pii;
typedef long long LL;
const double PI=acos(-1.0);
const int N=50010;
struct seg
{
    int l,mid,r;
    int L,R,M;
    int len()
    {
        return r-l+1;
    }
};
seg T[N<<2];
void pushup(int k)
{
    T[k].L=T[LC(k)].L;//暂时保持
    T[k].R=T[RC(k)].R;//暂时保持

    if(T[k].L==T[LC(k)].len())//可以向右拓展
        T[k].L+=T[RC(k)].L;//加上向右可拓展的长度
    if(T[k].R==T[RC(k)].len())//可以向左拓展
        T[k].R+=T[LC(k)].R;//加上向左可拓展的长度

    T[k].M=max<int>(T[LC(k)].R+T[RC(k)].L,max<int>(T[LC(k)].M,T[RC(k)].M));//更新M
}
void build(int k,int l,int r)
{
    T[k].l=l;
    T[k].r=r;
    T[k].mid=MID(l,r);
    if(l==r)
    {
        T[k].L=1;
        T[k].R=1;
        T[k].M=1;
        return ;
    }
    build(LC(k),l,T[k].mid);
    build(RC(k),T[k].mid+1,r);
    pushup(k);//记得pushup
}
void update(int k,int x,int val)
{
    if(T[k].l==T[k].r&&T[k].l==x)
    {
        T[k].M=T[k].L=T[k].R=val;
        return ;
    }
    if(x<=T[k].mid)
        update(LC(k),x,val);
    else
        update(RC(k),x,val);
    pushup(k);
}
int query(int k,int x)
{
    if(T[k].l==T[k].r||T[k].M==0||T[k].len()==T[k].M)
        return T[k].M;
    else
    {
        if(x<=T[k].mid)//在左子树
        {
            if(x>=T[k].mid-T[LC(k)].R+1)//若在左子树的R范围内则说明可以继续向右子树连通
                return query(LC(k),x)+query(RC(k),T[RC(k)].l);
            else
                return query(LC(k),x);
        }
        else//在右子树
        {
            if(x<=(T[k].mid+1)+T[RC(k)].L-1)//若在右子树的L范围内说明可以继续向左子树连通
                return query(RC(k),x)+query(LC(k),T[LC(k)].r);
            else
                return query(RC(k),x);
        }
    }
}
int main(void)
{
    int n,m,x,r;
    char ops[4];
    while (~scanf("%d%d",&n,&m))
    {
        build(1,1,n);
        stack<int>Q;
        while (m--)
        {
            scanf("%s%d",ops,&x);
            if(ops[0]==‘D‘)
            {
                update(1,x,0);
                Q.push(x);
            }
            else if(ops[0]==‘R‘)
            {
                update(1,Q.top(),1);
                Q.pop();
            }
            else
                printf("%d\n",query(1,x));
        }
    }
    return 0;
}

以上是关于POJ 2892 Tunnel Warfare(线段树单点更新区间合并)的主要内容,如果未能解决你的问题,请参考以下文章

poj2892 Tunnel Warfare

POJ2892 Tunnel Warfare

POJ2892 Tunnel Warfare

POJ 2892 Tunnel Warfare(线段树单点更新区间合并)

POJ2892 Tunnel Warfare 题解

hdu 1540/POJ 2892 Tunnel Warfare 线段树区间合并