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 (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.
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;
}