44MLE求调
查看原帖
44MLE求调
790748
2022tysc0776楼主2024/10/1 23:15
#include <bits/stdc++.h>
using namespace std;
const int Mo=1e9+7;
#define lc(x) trp[x].ls
#define rc(x) trp[x].rs
int n,op,x,cn,root;
struct Node{
	int ls,rs,key,pri,siz;
}trp[100010];
void split(int u,int x,int &L,int &R){//按权值分裂 
	if(u==0){L=0;R=0;return;}
	if(trp[u].key<=x){
		L=u;
		split(trp[u].rs,x,trp[u].rs,R);
	}else{
		R=u;
		split(trp[u].ls,x,L,trp[u].ls);
	}
	trp[u].siz=trp[lc(u)].siz+trp[rc(u)].siz+1;
}
int merge(int L,int R){
	if(L==0||R==0) return L+R;
	if(trp[L].pri>trp[R].pri){
		rc(L)=merge(rc(L),R);
		trp[L].siz=trp[lc(L)].siz+trp[rc(L)].siz+1;
		return L;
	}else{
		lc(R)=merge(L,lc(R));
		trp[R].siz=trp[lc(R)].siz+trp[rc(R)].siz+1;
		return R;
	}
}
void Newnode(int x){
	cn++;
	trp[cn].ls=trp[cn].rs=0;
	trp[cn].key=x;
	trp[cn].pri=1LL*rand()*rand()%Mo;
	trp[cn].siz=1;
}
void Insert(int x){
	int L,R;
	split(root,x,L,R);//L树上的权值全部<=x ,R树上的权值>=x
	Newnode(x);
	int aa=merge(L,cn);//注意合并顺序
	root=merge(aa,R);
}
void Erase(int x){
	int L,R,p;
	split(root,x,L,R);
	split(root,x-1,L,p);//弄出 u 的权值==x 的树p
	p=merge(lc(p),rc(p));//合并 p 的左子树和右子树
	root=merge(merge(L,p),R);
}
int Rank(int x){
	int L,R;
	split(root,x-1,L,R);
	int ans=trp[L].siz+1;
	root=merge(L,R);
	return ans;
}
int kth(int u,int x){
	if(trp[lc(u)].siz+1==x) return trp[u].key;
	if(x<=trp[lc(u)].siz) return kth(lc(u),x);
	else return kth(rc(u),x-trp[lc(u)].siz-1);
}
int precursor(int x){
	int L,R;
	split(root,x-1,L,R);
	int ans=kth(L,trp[L].siz);
	root=merge(L,R);
	return ans;
}
int succussor(int x){
	int L,R;
	split(root,x+1,L,R);
	int ans=kth(R,1);
	root=merge(L,R);
	return ans;
}
int main(){
	srand(time(0));
	scanf("%d",&n);
	while(n--){
		scanf("%d%d",&op,&x);
		if(op==1) Insert(x);
		else if(op==2) Erase(x);
		else if(op==3) printf("%d\n",Rank(x));
		else if(op==4) printf("%d\n",kth(root,x));
		else if(op==5) printf("%d\n",precursor(x));
		else printf("%d\n",succussor(x));
	}
	return 0;
} 
2024/10/1 23:15
加载中...