bzoj 3224: Tyvj 1728 普通平衡树【非旋treap】

就是非旋treap的板子

#include<iostream>
#include<cstdio>
#include<cstdlib>
using namespace std;
const int N=500005,mod=73939133,bs=997687623;
int q,tot,rt,st=123;
struct qwe
{
	int c[2],f,v,s,w;
}t[N];
int read()
{
	int r=0,f=1;
	char p=getchar();
	while(p>'9'||p<'0')
	{
		if(p=='-')
			f=-1;
		p=getchar();
	}
	while(p>='0'&&p<='9')
	{
		r=r*10+p-48;
		p=getchar();
	}
	return r*f;
}
int rand()
{
	return st=1ll*st*bs%mod;
}
void ud(int x)
{
	t[x].s=t[t[x].c[0]].s+t[t[x].c[1]].s+1;
}
int hb(int x,int y)
{
	if(!x||!y)
		return x+y;
	if(t[x].w<t[y].w)
	{
		t[x].c[1]=hb(t[x].c[1],y);
		ud(x);
		return x;
	}
	else
	{
		t[y].c[0]=hb(x,t[y].c[0]);
		ud(y);
		return y;
	}
}
void split(int x,int v,int &l,int &r)
{
	if(!x)
	{
		l=r=0;
		return;
	}
	if(t[x].v<=v)
		l=x,split(t[x].c[1],v,t[x].c[1],r);
	else
		r=x,split(t[x].c[0],v,l,t[x].c[0]);
	ud(x);
}
int kth(int x,int k)
{
	while(1)
	{
		if(k<=t[t[x].c[0]].s)
			x=t[x].c[0];
		else if(k==t[t[x].c[0]].s+1)
			return x;
		else
			k-=t[t[x].c[0]].s+1,x=t[x].c[1];
	}
}
void charu(int x)
{
	int a,b;
	split(rt,x,a,b);
	t[++tot].s=1,t[tot].v=x,t[tot].w=rand();
	rt=hb(hb(a,tot),b);
}
void shanchu(int x)
{
	int a,b,c;
	split(rt,x,a,c);
	split(a,x-1,a,b);
	rt=hb(hb(a,hb(t[b].c[0],t[b].c[1])),c);
}
void rank(int x)
{
	int a,b;
	split(rt,x-1,a,b);
	printf("%d\n",t[a].s+1);
	rt=hb(a,b);
}
void real(int x)
{
	printf("%d\n",t[kth(rt,x)].v);
}
void pre(int x)
{
	int a,b;
	split(rt,x-1,a,b);
	printf("%d\n",t[kth(a,t[a].s)].v);
	rt=hb(a,b);
}
void nxt(int x)
{
	int a,b;
	split(rt,x,a,b);
	printf("%d\n",t[kth(b,1)].v);
	rt=hb(a,b);
}
int main()
{
	q=read();
	while(q--)
	{
		int o=read();
		if(o==1)
			charu(read());
		else if(o==2)
			shanchu(read());
		else if(o==3)
			rank(read());
		else if(o==4)
			real(read());
		else if(o==5)
			pre(read());
		else
			nxt(read());
	}
	return 0;
}
posted @ 2018-12-05 10:58  lokiii  阅读(155)  评论(0编辑  收藏  举报