BZOJ3224普通平衡树

BZoj3224普通平衡树

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.每个数的数据范围:[-1e7,1e7]

一道裸平衡树,我写的是Treap,code如下


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstdlib>
using namespace std;
const int MAXN=1000001;
const int INF=1e9;
struct Treap
{
	int ch[2],key,tms,size,dat;
}treap[MAXN];
int root,tot;
int T,op,n;
inline int cmp(int x,int tar)
{
	if(tar==treap[x].dat) return -1;
	return (tar<treap[x].dat?0:1);
}
inline void maintain(int x)
{
	treap[x].size=treap[x].tms;
	if(treap[x].ch[0]) treap[x].size+=treap[treap[x].ch[0]].size;
	if(treap[x].ch[1]) treap[x].size+=treap[treap[x].ch[1]].size;
}
inline void rotate(int &x,int d)
{
	int p=treap[x].ch[d^1];
	treap[x].ch[d^1]=treap[p].ch[d];
	treap[p].ch[d]=x;
	maintain(x);
	maintain(p);
	x=p;
}
void ins(int &x,int tar)
{
	if(!x) 
	{
		tot++;
		treap[tot].key=rand();
		treap[tot].dat=tar;
		treap[tot].tms=1;
		treap[tot].size=1;
		x=tot;
		return;
	}
	int d=cmp(x,tar);
	if(d==-1) treap[x].tms++;
	else 
	{
		ins(treap[x].ch[d],tar);
		if(treap[treap[x].ch[d]].key>treap[x].key) rotate(x,d^1);
	}
	maintain(x);
}
void del(int &x,int tar)
{
	int d=cmp(x,tar);
	if(d==-1)
	{
		if(treap[x].tms>1) treap[x].tms--;
		else 
		{
			if(!treap[x].ch[0]&&!treap[x].ch[1]) x=0;
			else if(!treap[x].ch[0]&&treap[x].ch[1]) x=treap[x].ch[1];
			else if(treap[x].ch[0]&&!treap[x].ch[1]) x=treap[x].ch[0];
			else 
			{
				int t=(treap[treap[x].ch[0]].key>treap[treap[x].ch[1]].key?1:0);
				rotate(x,t);
				del(treap[x].ch[t],tar);
			}
		}
	}
	else del(treap[x].ch[d],tar);
	maintain(x);
}
inline int GetPre(int x,int tar)
{
	int con=-INF;
	while(x)
	{
		int d=cmp(x,tar);
		if(d==-1) x=treap[x].ch[0];
		else if(d) 
		{
			con=max(con,treap[x].dat);
			x=treap[x].ch[1];
		}
		else x=treap[x].ch[0];
	}
	return con;
}
inline int GetNext(int x,int tar)
{
	int con=INF;
	while(x)
	{
		int d=cmp(x,tar);
		if(d==-1) x=treap[x].ch[1];
		else if(d) x=treap[x].ch[1];
		else
		{
			con=min(con,treap[x].dat);
			x=treap[x].ch[0];
		}	
	}
	return con;
}
int GetRank(int x,int tar)
{
	int d=cmp(x,tar);
	if(d==-1) return treap[treap[x].ch[0]].size+1;
	else if(d) return treap[treap[x].ch[0]].size+treap[x].tms+GetRank(treap[x].ch[1],tar);
	else return GetRank(treap[x].ch[0],tar);
}
int GetKth(int x,int k)
{
	if(k<=treap[treap[x].ch[0]].size) return GetKth(treap[x].ch[0],k);
	k-=treap[treap[x].ch[0]].size+treap[x].tms;
	if(k<=0) return treap[x].dat;
	else return GetKth(treap[x].ch[1],k);
}
int main(int argc, char *argv[])
{
	scanf("%d",&T);
	for(int i=1;i<=T;i++)
	{
		scanf("%d%d",&op,&n);
		if(op==1) ins(root,n);
		else if(op==2) del(root,n);
		else if(op==3) printf("%d\n",GetRank(root,n));
		else if(op==4) printf("%d\n",GetKth(root,n));
		else if(op==5) printf("%d\n",GetPre(root,n));
		else if(op==6) printf("%d\n",GetNext(root,n));
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值