bzoj3224普通平衡树

本文介绍了一种伸展树数据结构的实现方法,该结构能够高效地进行元素的插入、删除、查找等操作,并提供了具体的应用实例及代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一道伸展树裸题。一直没做,完善代码的时候顺便刷掉吧…….

Description

您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作:
1. 插入x数
2. 删除x数(若有多个相同的数,因只删除一个)
3. 查询x数的排名(若有多个相同的数,因输出最小的排名)
4. 查询排名为x的数
5. 求x的前驱(前驱定义为小于x,且最大的数)
6. 求x的后继(后继定义为大于x,且最小的数)

Input

第一行为n,表示操作的个数,下面n行每行有两个数opt和x,opt表示操作的序号(1<=opt<=6)

Output

对于操作3,4,5,6每行输出一个数,表示对应答案

Sample Input

10
1 106465
4 1
1 317721
1 460929
1 644985
1 84185
1 89851
6 81968
1 492737
5 493598

Sample Output

106465
84185
492737

HINT

1.n的数据范围:n<=100000

2.每个数的数据范围:[-2e9,2e9]

//制作人:陈保良
#include<cstdio>
using namespace std;
int read()
{
	int x=0,f=1;char ch=getchar();
	while(ch<'0' || ch>'9'){if(ch=='-')f=-1;ch=getchar();}
	while(ch>='0' && ch<='9'){x=x*10+ch-'0';ch=getchar();}
	return x*f;
}
int root;
struct trnode
{
	int d,c,n,f,son[2];
}tr[110000];int trlen;
void update(int x)
{
	int lc=tr[x].son[0],rc=tr[x].son[1];
	tr[x].c=tr[lc].c+tr[rc].c+tr[x].n;
}
void add(int d,int f)
{
	trlen++;int x=trlen;
	tr[x].d=d;tr[x].c=tr[x].n=1;
	tr[x].f=f;if(d<tr[f].d)tr[f].son[0]=x;else tr[f].son[1]=x;
	tr[x].son[0]=tr[x].son[1]=0;
}
void Rotate(int x,int w)
{
	int f=tr[x].f,ff=tr[f].f;
	int r,R;
	r=tr[x].son[w],R=f;
	tr[R].son[1-w]=r;
	if(r!=0)tr[r].f=R;
	r=x,R=ff;
	if(tr[R].son[0]==f)tr[R].son[0]=r;else tr[R].son[1]=r;
	tr[r].f=R;
	r=f,R=x;
	tr[R].son[w]=r;
	tr[r].f=R;
	update(f);
	update(x);
}
void splay(int x,int rt)
{	
	while(tr[x].f!=rt)
	{
		int f=tr[x].f,ff=tr[f].f;
		if(ff==rt)
		{
			if(tr[f].son[0]==x)Rotate(x,1);
			else Rotate(x,0);
		}
		else
		{
			if(tr[ff].son[0]==f && tr[f].son[0]==x){Rotate(f,1);Rotate(x,1);}
			else if(tr[ff].son[1]==f && tr[f].son[1]==x){Rotate(f,0);Rotate(x,0);}
			else if(tr[ff].son[0]==f && tr[f].son[1]==x){Rotate(x,0);Rotate(x,1);}
			else{Rotate(x,1);Rotate(x,0);}
		}
	}
	if(rt==0)root=x;
}
int findip(int d)
{
	int x=root;
	while(d!=tr[x].d)
	{
		int lc=tr[x].son[0],rc=tr[x].son[1];
		if(d<tr[x].d)
		{
			if(lc==0)break;
			x=lc;
		}
		else
		{
			if(rc==0)break;
			x=rc;
		}
	}
	return x;
}
void ins(int d)
{
	if(root==0)
	{
		add(d,0);
		root=trlen;
		return ;
	}
	int x=findip(d);
	if(d==tr[x].d)
	{
		tr[x].n++;
		update(x);
		splay(x,0);
	}
	else
	{
		add(d,x);
		update(x);
		splay(trlen,0);
	}
}
void del(int d)
{
	int x=findip(d);
	if(tr[x].d!=d)return ;
	splay(x,0);
	if(tr[x].n>1){tr[x].n--;update(x);return ;}
	if(tr[x].son[0]==0 && tr[x].son[1]==0){root=trlen=0;}
	else if(tr[x].son[0]==0){root=tr[x].son[1];tr[root].f=0;}
	else if(tr[x].son[1]==0){root=tr[x].son[0];tr[root].f=0;}
	else
	{
		int p=tr[x].son[0];while(tr[p].son[1]!=0)p=tr[p].son[1];
		splay(p,x);
		int r=tr[x].son[1],R=p;
		tr[R].son[1]=r;
		tr[r].f=R;
		root=R;tr[root].f=0;
		update(R);
	}
}
int findpaiming(int d)
{
	int x=findip(d);splay(x,0);
	return tr[tr[x].son[0]].c+1;
}
int findshuzi(int k)
{
	int x=root;
	while(1)
	{
		int lc=tr[x].son[0],rc=tr[x].son[1];
		if(k<=tr[lc].c)x=lc;
		else if(k>tr[lc].c+tr[x].n){k-=tr[lc].c+tr[x].n;x=rc;}
		else break;
	}
	splay(x,0);
	return tr[x].d;
}
int findqianqu(int d)
{
	int x=findip(d);splay(x,0);
	if(tr[x].d>=d && tr[x].son[0]!=0)
	{
		x=tr[x].son[0];
		while(tr[x].son[1]!=0)x=tr[x].son[1];
	}
	if(tr[x].d>=d)x=0;
	return tr[x].d;
}
int findhouji(int d)
{
	int x=findip(d);splay(x,0);
	if(tr[x].d<=d && tr[x].son[1]!=0)
	{
		x=tr[x].son[1];
		while(tr[x].son[0]!=0)x=tr[x].son[0];
	}
	if(tr[x].d<=d)x=0;
	return tr[x].d;
}
int main()
{
	int n,x,y;
	n=read();
	for(int i=1;i<=n;i++)
	{
		x=read();y=read();
		if(x==1)ins(y);
		else if(x==2)del(y);
		else if(x==3)printf("%d\n",findpaiming(y));
		else if(x==4)
		{
			if(y<=tr[root].c)printf("%d\n",findshuzi(y));
			else printf("-1\n");
		}
		else if(x==5)printf("%d\n",findqianqu(y));
		else printf("%d\n",findhouji(y));
	}
	return 0;
}
祝1A

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值