poj2892 treap

博客围绕抗战时期华北平原隧道战场景展开,介绍了村庄隧道连接情况及入侵者攻击、八路军查询连接状态等事件。输入包含村庄和事件数量,有村庄破坏、查询连接村庄数、重建村庄三种事件。解题使用treap算法,初始化空树,按不同事件操作树。

Tunnel Warfare
Time Limit: 1000MS Memory Limit: 131072K
Total Submissions: 7205 Accepted: 2957

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

此题为了练习treap,初始化空树,对每次D X,将x插入平衡树,对Q X,则寻找距离x最近和最远的值,R则删除最近插入的数。

#include<cstdio>
#include<iostream>
#include<cstring>
#include<ctime>
#include<cstdlib>
#include<algorithm>
#define Maxn 50010
using namespace std;

int maxx,minn;
struct treap{
    int l,r,num,w,rnd;
}tree[Maxn];
int sz; //当前已用节点数
int root;
void rturn(int &t){ //右旋
    int k=tree[t].l;
    tree[t].l=tree[k].r;
    tree[k].r=t;
    t=k;
}
void lturn(int &t){ //左旋
    int k=tree[t].r;
    tree[t].r=tree[k].l;
    tree[k].l=t;
    t=k;
}
//第一次插入调用insert(root,0),root=0
//调用结束后root=1,之后可以正常插入
void insert(int &t,int x){ //t传引用
    if(t==0){ //到叶子节点,新建节点
        t=++sz;
        tree[t].num=x;
        tree[t].w=1;
        tree[t].rnd=rand();
        return;
    }
    if(x==tree[t].num){ //已存在该元素
        //tree[t].w++; //权值加1
        return;
    }
    else if(x<tree[t].num){ //往左子树插入
        insert(tree[t].l,x);
        if(tree[tree[t].l].rnd<tree[t].rnd)
            rturn(t); //t会改变
    }
    else{
        insert(tree[t].r,x);
        if(tree[tree[t].r].rnd<tree[t].rnd)
            lturn(t); //t会改变
    }
}
void del(int &t,int x){
    if(t==0) return;
    if(tree[t].num==x){
        /*
        if(tree[t].w>1){
            tree[t].w--;
            return;
        }*/
        if(tree[t].l*tree[t].r==0) //到叶节点
            t=tree[t].l+tree[t].r;
        else if(tree[tree[t].l].rnd>=tree[tree[t].r].rnd){
            lturn(t);
            del(tree[t].l,x);
        }
        else{
            rturn(t);
            del(tree[t].r,x);
        }
    }
    else if(tree[t].num<x)
        del(tree[t].r,x);
    else del(tree[t].l,x);
}
void find(int &t,int x){
    if(t==0) return;
    if(tree[t].num>=x) maxx=min(maxx,tree[t].num);
    if(tree[t].num<=x) minn=max(minn,tree[t].num);
    if(tree[t].num<x) find(tree[t].r,x);
    else find(tree[t].l,x);
}
char s[2];
int st[Maxn],top;
int vis[Maxn];
int main()
{
    int n,m,a;
    //srand((unsigned)time(NULL));
    while(~scanf("%d%d",&n,&m)){
        memset(tree,0,sizeof tree);
        memset(vis,0,sizeof vis);
        sz=root=top=0;
        for(int i=0;i<m;i++){
            scanf("%s",s);
            if(s[0]=='D'){
                scanf("%d",&a);
                insert(root,a);
                vis[a]=1;
                st[top++]=a;
            }
            else if(s[0]=='R'){
                top--;
                if(vis[st[top]]){
                    del(root,st[top]);
                    vis[st[top]]=0;
                }
            }
            else{
                scanf("%d",&a);
                minn=0,maxx=n+1;
                find(root,a);
                if(minn==maxx) puts("0");
                else printf("%d\n",maxx-minn-1);
            }
        }
    }
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值