Splay 28pt 几乎全T求条
查看原帖
Splay 28pt 几乎全T求条
906856
A2_Zenith楼主2024/5/5 11:40

record

#include<bits/stdc++.h>
#define int long long
#define doub long double
using namespace std;
const int maxn=2e5+7;
struct node{
	int val;
	int cnt;
	int fa;
	int ch[2];
	int siz;
}d[maxn];
#define lc(p) d[p].ch[0]
#define rc(p) d[p].ch[1]
#define val(p) d[p].val
#define siz(p) d[p].siz
#define cnt(p) d[p].cnt
#define fa(p) d[p].fa
int rt,tot;
void clr(int p){val(p)=cnt(p)=fa(p)=lc(p)=rc(p)=siz(p)=0;}
void pu(int p){siz(p)=siz(lc(p))+siz(rc(p))+cnt(p);}
bool dir(int p){return p==rc(fa(p));}
int newnode(int v,int f){val(++tot)=v,fa(tot)=f,siz(tot)=cnt(tot)=1;return tot;}
void rot(int x){
	int y=fa(x),z=fa(y);
	bool c=dir(x);
	d[y].ch[c]=d[x].ch[c^1];
	fa(d[x].ch[c^1])=y;
	d[x].ch[c^1]=y;
	fa(y)=x;
	fa(x)=z;
	if(z)d[z].ch[y==d[z].ch[1]]=x;
	pu(y),pu(x);
}
void splay(int x,int ds){
	ds=fa(ds);
	while(fa(x)!=ds){
		int f=fa(x);
		if(fa(f))rot(dir(f)==dir(x)?f:x);
		rot(x);
	}
}
void ins(int v){
	if(!rt){
		rt=newnode(v,0);
		return;
	}
	int now=rt;
	while(1001){
		if(val(now)==v){
			cnt(now)++;
			splay(now,rt);
			return;
		}
		int nxt=(v<val(now)?0:1);
		if(!d[now].ch[nxt]){
			int p=newnode(v,now);
			d[now].ch[nxt]=p;
			splay(p,rt);
			rt=p;
			return;
		}
		now=d[now].ch[nxt];
	}
}
int find(int v){
	int now=rt;
	while(19491001){
		if(!now)return 0;
		if(val(now)==v){
			splay(now,rt);
			rt=now;
			return rt;
		}
		int nxt=(v<val(now)?0:1);
		now=d[now].ch[nxt];
	}
}
void del(int v){
	int p=find(v);
	if(!p)return;
	if(cnt(p)>1){
		cnt(p)--;
		pu(p);
		splay(p,rt);
		return;
	}
	if(!cnt(lc(p))&&!cnt(rc(p))){rt=0;return;}
	else if(!cnt(lc(p))){
		rt=rc(p);
		fa(rc(p))=0;
		return;
	}
	else{
		int L=lc(p);
		while(rc(L))L=rc(L);
		splay(L,lc(p));
		rc(L)=rc(p);
		fa(L)=0;
		fa(rc(p))=L;
		rt=L;
		return;
	}
}
int rk_e(int x){
	int ans=0,now=rt;
	while(19491001){
		if(x<val(now)){
			now=lc(now);
			continue;
		}
		ans+=siz(lc(now));
		if(x==val(now)){
			splay(now,rt);
			rt=now;
			return ans+1;
		}
		ans+=cnt(now);
		now=rc(now);
	}
}
int rk(int x){
	ins(x);
	int ans=rk_e(x);
	del(x);
	return ans;
}
int kth(int k){
	int now=rt;
	while(19491001){
		if(lc(now)&&k<=siz(lc(now))){
			now=lc(now);
			continue;
		}
		k-=(siz(lc(now))+cnt(now));
		if(k<=0){
			splay(now,rt);
			rt=now;
			return val(now);
		}
		now=rc(now);
	}
}
int pre_e(){
	int now=lc(rt);
	if(!now)return now;
	while(rc(now))now=rc(now);
	splay(now,rt);
	return val(now);
}
int pre(int v){
	ins(v);
	int ans=pre_e();
	del(v);
	return ans;
}
int nxt_e(){
	int now=rc(rt);
	if(!now)return now;
	while(lc(now))now=rc(now);
	splay(now,rt);
	return val(now);
}
int nxt(int v){
	ins(v);
	int ans=nxt_e();
	del(v);
	return ans;
}
int n;
signed main(){
	cin>>n;
	while(n--){
		int op,x;
		cin>>op>>x;
		if(op==1)ins(x);
		else if(op==2)del(x);
		else if(op==3)cout<<rk(x)<<endl;
		else if(op==4)cout<<kth(x)<<endl;
		else if(op==5)cout<<pre(x)<<endl;
		else if(op==6)cout<<nxt(x)<<endl;
	}
	return 0;
}
/*


*/
2024/5/5 11:40
加载中...