| 比赛 | 
    清华集训2017模板练习 | 
    评测结果 | 
    AAAAAAAAAA | 
    | 题目名称 | 
    普通平衡树 | 
    最终得分 | 
    100 | 
    | 用户昵称 | 
    FoolMike | 
    运行时间 | 
    0.250 s  | 
    | 代码语言 | 
    C++ | 
    内存使用 | 
    1.84 MiB  | 
    | 提交时间 | 
    2017-12-02 20:47:22 | 
显示代码纯文本
#include<cstdio>
#include<iostream>
#include<cmath>
using namespace std;
struct leave
{
	int l,r,s,k;
}t[100010]={0};
const int maxl=99999999;
int top=0,n,e,i,x=0;
int size(int x) {return t[t[x].l].s+t[t[x].r].s+1;}
void l_rotate(int &x)
{
	int y=t[x].r;
	t[x].r=t[y].l;t[y].l=x;
	t[x].s=size(x);t[y].s=size(y);
	x=y;
	return;
}
void r_rotate(int &x)
{
	int y=t[x].l;
	t[x].l=t[y].r;t[y].r=x;
	t[x].s=size(x);t[y].s=size(y);
	x=y;
	return;
}
void Maintain(int &x,bool y)
{
	if (not y){
		if (t[t[t[x].l].l].s>t[t[x].r].s) r_rotate(x);
		else if (t[t[t[x].l].r].s>t[t[x].r].s){
			l_rotate(t[x].l);r_rotate(x);
		}
		else return;
	}
	else{
		if (t[t[t[x].r].r].s>t[t[x].l].s) l_rotate(x);
		else if (t[t[t[x].r].l].s>t[t[x].l].s){
			r_rotate(t[x].r);l_rotate(x);
		}
		else return;
	}
	Maintain(t[x].l,false);
	Maintain(t[x].r,true);
	Maintain(x,true);
	Maintain(x,false);
	return;
}
void insert(int &x,int key)
{
	if (x==0){
		top++;x=top;t[x].k=key;t[x].l=t[x].r=0;t[x].s=1;return;
	}
	t[x].s++;
	if (key>t[x].k) insert(t[x].r,key);else insert(t[x].l,key);
	Maintain(x,key>t[x].k);
	return;
}
int rank(int x,int key)
{
	int ans=maxl;
	if (x==0) return ans;
	if (key==t[x].k) ans=t[t[x].l].s+1;
	if (key<=t[x].k) ans=min(ans,rank(t[x].l,key));
	if (key>t[x].k) ans=min(ans,t[t[x].l].s+1+rank(t[x].r,key));
	return ans;
}
int select(int x,int key)
{
	if (key<t[t[x].l].s+1) return select(t[x].l,key);
	if (key==t[t[x].l].s+1) return t[x].k;
	if (key>t[t[x].l].s+1) return select(t[x].r,key-t[t[x].l].s-1);
}
int pred(int x,int key)
{
	int ans=-maxl;
	if (x==0) return ans;
	if (t[x].k<key) ans=t[x].k;
	if (t[x].k<key) ans=max(ans,pred(t[x].r,key));
	if (t[x].k>=key) return pred(t[x].l,key);
	return ans;
}
int succ(int x,int key)
{
	int ans=maxl;
	if (x==0) return ans;
	if (t[x].k>key) ans=t[x].k;
	if (t[x].k>key) ans=min(ans,succ(t[x].l,key));
	if (t[x].k<=key) return succ(t[x].r,key);
	return ans;
}
void del(int &x,int key)
{
	t[x].s--;
	if (key>t[x].k) del(t[x].r,key);
	if (key<t[x].k) del(t[x].l,key);
	if (key==t[x].k){
		if (t[x].l!=0&&t[x].r==0){
			x=t[x].l;return;
		}
		if (t[x].l==0&&t[x].r!=0){
			x=t[x].r;return;
		}
		if (t[x].l==0&&t[x].r==0){
			x=0;return;
		}
		if (t[x].l!=0&&t[x].r!=0){
			int y=select(t[x].r,1);
			t[x].k=y;
			del(t[x].r,y);
		}
	}
	return;
}
void bianli(int x,int c)
{
	if (x==0) return;
	for (int i=1;i<=c;i++) printf(" ");
	printf("%d\n",t[x].k);
	bianli(t[x].l,c+2);
	bianli(t[x].r,c+2);
	return;
}
int main()
{
	freopen("phs.in","r",stdin);
	freopen("phs.out","w",stdout);
	scanf("%d",&n);
	for (;n>0;n--){
		scanf("%d%d",&e,&i);
		if (e==1) insert(x,i);
		if (e==2) del(x,i);
		if (e==3) printf("%d\n",rank(x,i));
		if (e==4) printf("%d\n",select(x,i));
		if (e==5) printf("%d\n",pred(x,i));
		if (e==6) printf("%d\n",succ(x,i));
	}
	return 0;
}