可持久化fhq96pts求调WA on #20
查看原帖
可持久化fhq96pts求调WA on #20
943211
_mayiwei楼主2024/1/6 09:43
#include <bits/stdc++.h>
using namespace std;
int rnd(){
	return (rand()<<15)+rand();
}
const int N=30000005;
struct Node{
	int val,rnd,siz;
	int lson,rson;
}tr[N];
int tot;
void p(int u){
	if(u){
		p(tr[u].lson);
		cout<<tr[u].val<<" ";
		p(tr[u].rson);
	}
}
void print(int u){
	p(u);
	cout<<"\n";
}
int newNode(int x){
	tr[++tot]={x,rnd(),1,0,0};
	return tot;
}
int copyNode(int u){
	tr[++tot]=tr[u];
	return tot;
}
void pushup(int u){
	tr[u].siz=tr[tr[u].lson].siz+tr[tr[u].rson].siz+1;
}
void split(int u,int &x,int &y,int val){
	if(!u){
		x=y=0;
		return;
	}
	int v=copyNode(u);
	if(tr[u].val<=val){
		x=v;
		split(tr[v].rson,tr[v].rson,y,val);
	}else{
		y=v;
		split(tr[v].lson,x,tr[v].lson,val);
	}
	pushup(v);
}
int merge(int x,int y){
	if(!x||!y)return x|y;
	if(tr[x].rnd<tr[y].rnd){
		int p=copyNode(x);
		tr[p].rson=merge(tr[p].rson,y);
		pushup(p);
		return p;
	}else{
		int p=copyNode(y);
		tr[p].lson=merge(x,tr[p].lson);
		pushup(p);
		return p;
	}
}
int root[N],cnt;
void copyVer(int t){
	root[++cnt]=t;
}
void insert(int t,int val){
	int l,r;
	split(t,l,r,val);
	root[++cnt]=merge(merge(l,newNode(val)),r);
}
void del(int t,int val){
	int l,r,x,y;
	split(t,l,r,val);
	split(l,x,y,val-1);
	root[++cnt]=merge(merge(x,merge(tr[y].lson,tr[y].rson)),r);
}
int rnk(int t,int val){
	int l,r;
	split(t,l,r,val-1);
	return tr[l].siz+1;
}
int kth(int u,int k){
	int p=tr[tr[u].lson].siz+1;
	if(p==k)return tr[u].val;
	else if(p<k)return kth(tr[u].rson,k-p);
	else return kth(tr[u].lson,k);
}
int pre(int t,int val){
	int l,r;
	split(t,l,r,val);
	if(tr[l].siz==0)return -2147483647;
	return kth(l,tr[l].siz);
}
int nxt(int t,int val){
	int l,r;
	split(t,l,r,val);
	if(tr[r].siz==0)return 2147483647;
	return kth(r,1);
}
int n;
signed main(){
	srand(time(0));
//	ios::sync_with_stdio(false);
//	cin.tie(0);cout.tie(0);
	cin>>n;
	while(n--){
		int v,op,x;
		cin>>v>>op>>x;
		if(op==1){
			insert(root[v],x);
		}else if(op==2){
			del(root[v],x);
		}else if(op==3){
			cout<<rnk(root[v],x)<<"\n";
			copyVer(root[v]);
		}else if(op==4){
			cout<<kth(root[v],x)<<"\n";
			copyVer(root[v]);
		}else if(op==5){
			cout<<pre(root[v],x)<<"\n";
			copyVer(root[v]);
		}else{
			cout<<nxt(root[v],x)<<"\n";
			copyVer(root[v]); 
		}
	}
	return 0;
}
2024/1/6 09:43
加载中...