其他分享
首页 > 其他分享> > 【fhq-treap】poj2892 Tunnel Warfare

【fhq-treap】poj2892 Tunnel Warfare

作者:互联网

Tunnel Warfare
Time Limit: 1000MS   Memory Limit: 131072K
Total Submissions: 10758   Accepted: 4448

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:

  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


用fhq-treap维护被摧毁的节点,然后查找前驱和后继来计算
对于操作"R",用一个栈来记录要删除的节点即可

代码
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
using namespace std;
const int maxn=5e4+5;
int n,m,tot,root;
struct fhq_treap
{
    int l,r,v,siz,rnd;
}fhq[maxn];
int newnode(int val)
{
    ++tot;
    fhq[tot].v=val; fhq[tot].rnd=rand();
    fhq[tot].siz=1;
    return tot;
} 
void update(int now)
{
    fhq[now].siz=fhq[fhq[now].l].siz+fhq[fhq[now].r].siz+1;
}
void split(int now,int k,int &x,int &y)
{
    if(!now) x=y=0;
    else
    {
        if(fhq[now].v<=k)
        {
            x=now;
            split(fhq[now].r,k,fhq[now].r,y);
        }
        else
        {
            y=now;
            split(fhq[now].l,k,x,fhq[now].l);
        }
        update(now);
    }
}
int merge(int x,int y)
{
    if(!x || !y) return x+y;
    if(fhq[x].rnd<=fhq[y].rnd)
    {
        fhq[x].r=merge(fhq[x].r,y);
        update(x);
        return x;
    }
    else
    {
        fhq[y].l=merge(x,fhq[y].l);
        update(y);
        return y;
    }
}
int st[maxn],top;
void ins(int val)
{
    int x,y;
    split(root,val,x,y);
    root=merge(merge(x,newnode(val)),y);
}
void del(int val)
{
    int x,y,z;
    split(root,val,x,z);
    split(x,val-1,x,y);
    y=merge(fhq[y].l,fhq[y].r);
    root=merge(merge(x,y),z);
}
int nxt(int val)
{
    int x,y;
    split(root,val,x,y);
    int now=y;
    while(fhq[now].l) now=fhq[now].l;
    int ans=fhq[now].v;
    root=merge(x,y);
    return ans;
}
int pre(int val)
{
    int x,y;
    split(root,val-1,x,y);
    int now=x;
    while(fhq[now].r) now=fhq[now].r;
    int ans=fhq[now].v;
    root=merge(x,y);
    return ans;
}
void query(int val)
{
    if(nxt(val-1)==val)
    {
        printf("%d\n",0);
        return;
    }
    int aa=nxt(val);
    int bb=pre(val);
    printf("%d\n",aa-bb-1);
}
int main()
{
    freopen("a.in","r",stdin);
    freopen("a.out","w",stdout);
    scanf("%d%d",&n,&m);
    ins(0),ins(n+1);
    for(int i=1;i<=m;i++)
    {
        char op[3];
        int x;
        scanf("%s%d",op,&x);
        if(op[0]=='D') ins(x),st[++top]=x;
        if(op[0]=='Q') query(x);
        if(op[0]=='R') del(st[top--]);
    }
    return 0;    
} 

 



标签:Tunnel,was,villages,tot,treap,int,now,fhq
来源: https://www.cnblogs.com/andylnx/p/14128650.html