可持久化 FHQ-Treap求调
查看原帖
可持久化 FHQ-Treap求调
744895
liaoxingrui楼主2024/7/1 16:34
#include<bits/stdc++.h>
#define tmpl(x) tree[x].l
#define tmpr(x) tree[x].r
#define size(x) tree[x].siz
#define vall(x) tree[x].val
using namespace std;
const int N=5e5+5;
const int ans=1<<31-1;
int n,rot,val,opt,tot;
int root[N];
struct node{
	int l,r,siz,val,next;
}tree[N<<5];
inline void pushup(int node){
	tree[node].siz=size(tmpl(node))+size(tmpr(node))+1;
}
inline void split(int node,int val,int &x,int &y){
	if(!node){
		x=0;
		y=0;
		return;
	}
	tot++;
	if(vall(node)<=val){
		x=tot;
		tree[x]=tree[node];
		split(tmpr(x),val,tmpr(x),y);
		pushup(x);
	}
	else{
		y=tot;
		tree[y]=tree[node];
		split(tmpl(y),val,x,tmpl(y));
		pushup(y);
	}
}
inline int merge(int x,int y){
	if(!x||!y)
		return x+y;
	tot++;
	int rot=tot;
	if(tree[x].next<=tree[y].next){
		tree[rot]=tree[x];
		tree[rot].r=merge(tmpr(rot),y);
	}
	else{
		tree[rot]=tree[y];
		tree[rot].l=merge(x,tmpl(rot));
	}
	pushup(rot);
	return rot;
}
inline void insert(int &root,int val){
	int l,r;
	split(root,val,l,r);
	tot++;
	tree[tot].siz=1;
	tree[tot].val=val;
	tree[tot].next=rand()%1000000007;
	root=merge(l,merge(tot,r));
}
inline void erase(int &root,int val){
	int x,y,z;
	split(root,val,x,y);
	split(x,val-1,x,z);
	z=merge(tmpl(z),tmpr(z));
	root=merge(merge(x,z),y);
}
inline int rank(int &root,int val){
	int l,r;
	split(root,val-1,l,r);
	return tree[l].siz+1;
}
inline int find(int node,int val){
	if(val==tmpl(node)+1)
		return vall(node);
	if(val<=tmpl(node)+1)
		return find(tmpl(node),val);
	return find(tmpr(node),val-size(tmpl(node))-1);
}
inline int query_head(int &root,int val){
	int l,r;
	split(root,val-1,l,r);
	if(!l)
		return -ans;
	return find(l,size(l));
}
inline int query_next(int &root,int val){
	int l,r;
	split(root,val+1,l,r);
	if(!r)
		return ans;
	return find(r,1);
}
int main(){
	srand(time(0));
	cin>>n;
	for(int i=1;i<=n;i++){
		cin>>rot>>opt>>val;
		root[i]=root[rot];
		if(opt==1)
			insert(root[i],val);
		else if(opt==2)
			erase(root[i],val);
		else{
			if(opt==3)
				cout<<rank(root[i],val);
			else if(opt==4)
				cout<<find(root[i],val);
			else if(opt==5)
				cout<<query_head(root[i],val);
			else
				cout<<query_next(root[i],val);
			root[i]=root[rot];
			putchar('\n');
		}
	}
	return 0;
}
2024/7/1 16:34
加载中...