FHQ treap求助
查看原帖
FHQ treap求助
57900
inging楼主2020/7/22 09:17
#include <bits/stdc++.h>
using namespace std;
int n,t,s[100005],k,g,kk,tree[100005],son[100005][2],heap[100005];
int seed=19260817;
void fen(int now,int &a /*A树*/ ,int &b /*B树*/ ,int val){
	if(now==0){
		a=0;
		b=0;
		return ;
	}
	if(tree[now]<=val){
		a=now;
		fen(son[now][1],son[now][1],b,val);
	}
	else {
		b=now;
		fen(son[now][0],a,son[now][0],val);
	}
	s[now]=s[son[now][0]]+s[son[now][1]]+1;
}
void he(int &now,int a,int b){
	if(a==0||b==0){
		now=a+b;
		return ;
	}
	if(heap[a]<heap[b]){
		now=a;
		he(son[now][1],son[a][1],b);
	}
	else {
		now=b;
		he(son[now][0],a,son[b][0]);
	}
	s[now]=s[son[now][0]]+s[son[now][1]]+1;
}
int rank(int aa){
	int x=0,y=0;
	fen(g,x,y,aa-1);
	int fd=s[x]+1;
	he(g,x,y);
	return fd;	
}
int find(int now,int kkk){
	for(;;){
		if(s[son[now][0]]+1==kkk){
			return tree[now];
		}
		if(s[son[now][0]]>=kkk) now=son[now][0];
		else kkk-=(s[son[now][0]]+1),now=son[now][1];
	}
}
int main(){
	g=0;
	
	cin>>kk;
	for(;kk--;){
		scanf("%d%d",&t,&k);
		if(t==1){
			int x=0,y=0;
			n++;
			s[n]=1;
			tree[n]=k;
			seed=int(seed*4827ll)%2147483637;
			heap[n]=seed;
			fen(g,x,y,k);
			int o=n;
			he(x,x,o);
			he(g,x,y);
		}
		if(t==2){
			int x=0,y=0,o=0;
			fen(g,x,y,k);
			fen(x,x,o,k-1);
			he(o,son[o][0],son[o][1]);
			he(x,x,o);
			he(g,x,y);
		} 
		if(t==3){
			cout<<rank(k)<<endl;
		}
		if(t==4){
			
			cout<<find(g,k)<<endl;
		}
		if(t==5){
			int a=0,b=0;
			fen(g,a,b,k-1);
			printf("%d\n",find(a,s[a]));
			he(g,a,b);
		}
		if(t==6){
			int a=0,b=0;
			fen(g,a,b,k+1);
			printf("%d\n",find(b,1));
			he(g,a,b);
		}
	}
	return 0;
}
卡在68分orz
2020/7/22 09:17
加载中...