fhqTreap

Posted wendigo

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了fhqTreap相关的知识,希望对你有一定的参考价值。

咕咕。

  • 代码:
#include <bits/stdc++.h>
using namespace std;

//Start
typedef long long ll;
typedef double db;
#define mp(a,b) make_pair(a,b)
#define x(a) a.first
#define y(a) a.second
#define b(a) a.begin()
#define e(a) a.end()
#define sz(a) int((a).size())
#define pb(a) push_back(a)
const int inf=0x3f3f3f3f;
const ll INF=0x3f3f3f3f3f3f3f3f;

//Data
const int N=1e5;

//Fhqtreap
int top,cnt,ch[N+7][2],v[N+7],p[N+7],sz[N+7];
int low(int x){return sz[x]=1+sz[ch[x][0]]+sz[ch[x][1]];}
int wen(int x){return sz[++cnt]=1,v[cnt]=x,p[cnt]=rand(),cnt;}
int merge(int x,int y){
	if(!x||!y) return x^y;
	if(p[x]<p[y]) return ch[x][1]=merge(ch[x][1],y),low(x),x;
	return ch[y][0]=merge(x,ch[y][0]),low(y),y; 
} 
int split(int rt,int k,int&x,int&y){
	if(!rt) return x=y=0;
	if(v[rt]<=k) return x=rt,split(ch[rt][1],k,ch[rt][1],y),low(rt),0;
	return y=rt,split(ch[rt][0],k,x,ch[rt][0]),low(rt),0;
}
int kth(int x,int k){
	for(;k!=sz[ch[x][0]]+1;)
		if(k<=sz[ch[x][0]]) x=ch[x][0];
		else k-=sz[ch[x][0]]+1,x=ch[x][1];
	return x;
}

//Main
int main(){
	int n; scanf("%d",&n);
	for(int i=1;i<=n;i++){
		int o,a,x,y,z; scanf("%d%d",&o,&a);
		if(o==1) split(top,a,x,y),top=merge(merge(x,wen(a)),y);
		else if(o==2) split(top,a,x,z),split(x,a-1,x,y),
			y=merge(ch[y][0],ch[y][1]),top=merge(merge(x,y),z);
		else if(o==3) split(top,a-1,x,y),printf("%d
",sz[x]+1),top=merge(x,y);
		else if(o==4) printf("%d
",v[kth(top,a)]);
		else if(o==5) split(top,a-1,x,y),printf("%d
",v[kth(x,sz[x])]),top=merge(x,y);
		else if(o==6) split(top,a,x,y),printf("%d
",v[kth(y,1)]),top=merge(x,y);		
	}
	return 0;
}

以上是关于fhqTreap的主要内容,如果未能解决你的问题,请参考以下文章

fhqtreap入门

fhqTreap

fhqTreap

[模板]fhqTreap

$fhqTreap$

震惊!一蒟蒻竟然写出fhqTreap