社区讨论

Splay #3 AC 其他 WA or TLE

P3369【模板】普通平衡树参与者 2已保存回复 2

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
2 条
当前快照
1 份
快照标识符
@lz1avgwc
此快照首次捕获于
2024/07/25 21:19
2 年前
此快照最后确认于
2024/07/25 22:48
2 年前
查看原帖
CPP
#include<bits/stdc++.h>
#define int long long
using namespace std;
long long T;
int tot,root;
struct node{
	int val,father,cnt,size;
	int ch[2];
}tree[100005];
inline void rotate(int x){
	int y=tree[x].father;
	int z=tree[y].father,k;
	if(tree[y].ch[0]==x){
		k=0;
	}
	else{
		k=1;
	}
	if(tree[z].ch[1]==y){
		tree[z].ch[1]=x;
	}
	else{
		tree[z].ch[0]=x;
	}
	tree[x].father=z;
	tree[y].ch[k]=tree[x].ch[k^1];
	tree[tree[x].ch[k^1]].father=y;
	tree[x].ch[k^1]=y;
	tree[y].father=x;
	return;
}
inline void splay(int x,int to){
	while(tree[x].father!=to){
		int y=tree[x].father;
		int z=tree[y].father;
		if(z!=to){
			if(tree[z].ch[0]==y){
				if(tree[y].ch[0]==x){
					rotate(y);
				}
				else{
					rotate(x);
				}
			}
			else{
				if(tree[y].ch[0]==x){
					rotate(x);
				}
				else{
					rotate(y);
				}
			}
		}
		rotate(x);
	}
	if(to==0){
		root=x;
	}
	return;
}
inline void insert(int x){
	int u=root,father=0;
	while(u&&tree[u].val!=x){
		father=u;
		if(x>tree[u].val){
			u=tree[u].ch[1];
		}
		else{
			u=tree[u].ch[0];
		}
	}
	if(u){
		tree[u].cnt++;
	}
	else{
		u=++tot;
		if(father){
			if(x>tree[father].val){
				tree[father].ch[1]=u;
			}
			else{
				tree[father].ch[0]=u;
			}
		}
		tree[u].ch[0]=tree[u].ch[1]=0;
		tree[tot].father=father;
		tree[tot].val=x;
		tree[tot].cnt=1;
		tree[tot].size=1;
	}
	splay(u,0);
	return;
}
inline void find(int x){
	int u=root;
	if(!u){
		return;
	}
	if(x>tree[u].val){
		while(tree[u].ch[1]&&x!=tree[u].val){
			u=tree[u].ch[1];
		}
	}
	else{
		while(tree[u].ch[0]&&x!=tree[u].val){
			u=tree[u].ch[0];
		}
	}
	splay(u,0);
	return;
}
inline long long nxt(int x,int father){
	find(x);
	int u=root;
	if(tree[u].val>x&&father){
		return u;
	}
	if(tree[u].val<x&&!father){
		return u;
	}
	u=tree[u].ch[father];
	while(tree[u].ch[father^1]){
		u=tree[u].ch[father^1];
	}
	return u;
}
inline void dlt(int x){
	int lst=nxt(x,0);
	int neext=nxt(x,1);
	splay(neext,0);
	splay(neext,lst);
	int del=tree[lst].ch[0];
	if(tree[del].cnt>1){
		tree[del].cnt--;
		splay(del,0);
	}
	else{
		tree[neext].ch[0]=0;
	}
	return;
}
inline long long kth(int x){
	int u=root;
	if(tree[u].size<x){
		return 0;
	}
	while(1){
		int y=tree[u].ch[0];
		if(x>tree[y].size+tree[u].cnt){
			x-=tree[y].size+tree[u].cnt;
			u=tree[u].ch[1];
		}
		else{
			if(tree[y].size>=x){
				u=y;
			}
			else{
				return tree[u].val;
			}
		}
	}
}
inline long long rnk(int x){
	int u=0,father=root;
	while(1){
		if(x<tree[father].val){
			father=tree[father].ch[0];
		}
		else{
			u+=tree[tree[father].ch[0]].size;
			if(!father){
				return u+1;
			}
			if(x==tree[father].val){
				splay(father,0);
				return u+1;
			}
			u+=tree[father].cnt;
			father=tree[father].ch[1];
		}
	}
//	return;
}
signed main(){
	cin>>T;
	while(T--){
		int opt,x;
		cin>>opt>>x;
		if(opt==1){
			insert(x);
		}
		if(opt==2){
			dlt(x);
		}
		if(opt==3){
			cout<<rnk(x)<<endl;
		}
		if(opt==4){
			cout<<kth(x)<<endl;
		}
		if(opt==5){
			cout<<tree[nxt(x,0)].val<<endl;
//			cout<<nxt(x,0)<<endl;
		}
		if(opt==6){
			cout<<tree[nxt(x,1)].val<<endl;
		}
	}
}
感觉自己 rnk(x) 出锅了,有无人帮忙调调/qd/qd

回复

2 条回复,欢迎继续交流。

正在加载回复...