Splay求助,48pts
查看原帖
Splay求助,48pts
363495
我爱杨帆楼主2021/1/30 18:18
#include<bits/stdc++.h>
#define int long long
#define re register
#define inf 1e18
using namespace std;
const int sz=1e6+5;
inline int read()
{
	char ch;
	int r=0,f=1;
	ch=getchar();
	while((ch<'0'||ch>'9')&&ch!='-') ch=getchar();
	if(ch=='-') f=-1,ch=getchar();
	while(ch>='0'&&ch<='9') r=r*10+ch-'0',ch=getchar();
	return r*f;
}
int root,tot;
struct BST
{		
	struct node
	{
		int val,ch[2],ff,size,cnt;
	}t[sz];
	int identify(int x)
	{	
		return t[t[x].ff].ch[1]==x ? 1:0;
	}	
	void update(int x)
	{	
		t[x].size=t[t[x].ch[0]].size+t[t[x].ch[1]].size+t[x].cnt;
	}	
	void rotate(int x)
	{	
		int y=t[x].ff,z=t[t[x].ff].ff,kx=identify(x),ky=identify(y);
		t[x].ff=z,t[z].ch[ky]=x;
		t[y].ff=x;
		t[y].ch[kx]=t[x].ch[kx^1],t[t[x].ch[kx^1]].ff=y;t[x].ch[kx^1]=y;
		update(y);update(x);
	}	
	void splay(int x,int goal)
	{
		while(t[x].ff!=goal)		
		{					
			int y=t[x].ff,z=t[t[x].ff].ff;
			if(z!=goal) 	
				(t[z].ch[0]==y)^(t[y].ch[0]==x) ? rotate(x):rotate(y);
			rotate(x);		
		}	
		if(!goal) 		root=x; 
	}
	void find(int val)
	{	
		int u=root;
		if(!u) return;
		while(t[u].ch[t[u].val<val]&&val!=t[u].val) u=t[u].ch[t[u].val<val];
		splay(u,0);
	}	
	void cre(int val,int ff)
	{
		int u=++tot;
		t[ff].ch[t[ff].val<val]=u;
		t[u].ff=ff;
		t[u].size=t[u].cnt=1;
		t[u].val=val;
	}
	void insert(int val)
	{	
		int u=root,ff=0;
		while(t[u].val!=val&&u)
			ff=u,u=t[u].ch[t[u].val<val];
		if(t[u].val==val) {
			t[u].cnt++;
			splay(u,0);
			return;
		}	
		else 
		cre(val,ff);
		u=tot;
		splay(u,0);	
	}	
	int getnext(int x,int fla)
	{	
		find(x);
		int u=root;
		if(t[u].val>x&&fla) return u;
		if(t[u].val<x&&!fla) return u;
		u=t[u].ch[fla];
		while(t[u].ch[fla^1]) u=t[u].ch[fla^1];
		return u; 
	}	
	int dele(int x)
	{				
		int last=getnext(x,0);
		int next=getnext(x,1);
		splay(last,0);
		splay(next,last);
		if(t[t[next].ch[0]].cnt>1) t[t[next].ch[0]].cnt--;
		else t[next].ch[0]=0;
	}				
	int getrankbyval(int val,int now)
	{				
		if(!now) return 0;
		if(val==t[now].val) return t[t[now].ch[0]].size+1;
		if(val<t[now].val) return getrankbyval(val,t[now].ch[0]);
		return t[t[now].ch[0]].size+t[now].cnt+getrankbyval(val,t[now].ch[1]);
	}				
	int getvalbyrank(int ran,int now)
	{	
		if(!now) return inf;
		if(t[t[now].ch[0]].size>=ran) return getvalbyrank(ran,t[now].ch[0]);
		if(t[t[now].ch[0]].size+t[now].cnt>=ran) return t[now].val;
		return getvalbyrank(ran-t[t[now].ch[0]].size-t[now].cnt,t[now].ch[1]);  
	}	
}S;		
signed main()
{
	int n=read();
	S.insert(inf);S.insert(-inf);
	for(int i=1;i<=n;i++)
	{
		int op=read(),x=read();
		switch (op) 
		{
			case 1: 
				S.insert(x);
				break;
			case 2:
				S.dele(x);
				break;
			case 3:
				cout<<S.getrankbyval(x,root)-1<<endl;
				break;
			case 4:
				cout<<S.getvalbyrank(x+1,root)<<endl;
				break;
			case 5:
				cout<<S.t[S.getnext(x,0)].val<<endl;
				break;
			case 6:
				cout<<S.t[S.getnext(x,1)].val<<endl;
				break;				
		}
	}
}
/*
8
1 964673
1 915269
1 53283
3 964673
*/
2021/1/30 18:18
加载中...