FHQ Treap 36tps求调
查看原帖
FHQ Treap 36tps求调
666741
_wakeup楼主2023/6/6 18:09
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<ctime>
#include<cstdlib>
#define ll long long
using namespace std;
int n,opt,t,root,cnt=0;
struct node{
	int l,r,key,size,val;
}fhq[1000100];
int update(int x)
{
	fhq[x].size=fhq[fhq[x].l].size+fhq[fhq[x].r].size+1;
}
void split(int now,int dat,int &x,int &y)
{
	if(now==0){x=y=0;return;}
	if(dat<fhq[now].val)
	{
		y=now;
		split(fhq[now].l,dat,x,fhq[now].l);
	}
	else
	{
		x=now;
		split(fhq[now].r,dat,fhq[now].r,y);
	}
	update(now);
}
int merge(int x,int y)
{
	if(x==0||y==0)return x+y;
	if(fhq[x].key>fhq[y].key)
	{
		fhq[y].l=merge(x,fhq[y].l);
		update(y);
		return y;
	}
	else
	{
		fhq[x].r=merge(fhq[x].r,y);
		update(x);
		return x;
	}
}
int add(int x)
{
	fhq[++cnt].val=x;
	fhq[cnt].size=1;
	fhq[cnt].key=rand();
	return cnt;
}
int x,y,z;
void ins(int val)
{
	z=add(val);
	split(root,val,x,y);
	root=merge(merge(x,z),y);
}
void del(int val)
{
	split(root,val,x,z);
	split(x,val-1,x,y);
	y=merge(fhq[y].l,fhq[y].r);
	root=merge(merge(x,y),z); 
}
void get_rk(int val)
{
	split(root,val-1,x,y);
	cout<<fhq[x].size+1<<endl;
	root=merge(x,y);
}
int get_val(int dat)
{
	int now=root;
	while(now>0)
	{
		if(fhq[fhq[now].l].size+1==dat)break;
		else if(fhq[fhq[now].l].size>=dat)now=fhq[now].l;
		else
		{
			dat-=fhq[fhq[now].l].size+1;
			now=fhq[now].l;
		}
	}
	return fhq[now].val;
}
void pre(int val)
{
	split(root,val-1,x,y);
	int now=x;
	while(fhq[now].r>0)now=fhq[now].r;
	cout<<fhq[now].val<<endl;
	root=merge(x,y);
}
void next(int val)
{
	split(root,val,x,y);
	int now=y;
	while(fhq[now].l>0)now=fhq[now].l;
	cout<<fhq[now].val<<endl;
	root=merge(x,y);
}
int main()
{
	cin>>n;
	for(int i=1;i<=n;i++)
	{
		cin>>opt>>t;
		if(opt==1)ins(t);
		if(opt==2)del(t);
		if(opt==3)get_rk(t);
		if(opt==4)cout<<get_val(t)<<endl;
		if(opt==5)pre(t);
		if(opt==6)next(t);
	}
	return 0;
}
2023/6/6 18:09
加载中...