平衡树【洛谷P3369】的一些正解
PurpleWonder · · 个人记录
//fhq_treap
//代码长度:1861 运行时间:393ms
#include<cstdio>
#include<cstdlib>
using namespace std;
struct node{
int ch[2],f,size,val,key;
}t[100010];
int gs,n,rt,com,zhi;
inline void update(int x){
t[x].size=t[t[x].ch[0]].size+t[t[x].ch[1]].size+1;
}
//生成随机数(然而这个生成法不是最靠谱的)
inline int rd(){
return (1ll*rand()*rand())%2147483647;
}
//合并两个根分别为x与y的树
int merge(int x,int y){
if((!x) || (!y))return x|y;
int now=0;
if(t[y].key>t[x].key){
now=x,t[now].ch[1]=merge(t[x].ch[1],y);
}
else now=y,t[now].ch[0]=merge(x,t[y].ch[0]);
update(now);
return now;
}
//按照zhi分开一棵树
void split(int root,int zhi,int &x,int &y){
if(!root){
x=y=0;return;
}
else if(t[root].val<=zhi){
x=root,split(t[root].ch[1],zhi,t[x].ch[1],y);
}
else y=root,split(t[root].ch[0],zhi,x,t[y].ch[0]);
update(root);
}
int insert(int zhi){
register int x,y;//x与y分别记录左右子树的根节点,方便一会合并
split(rt,zhi,x,y);
gs++;
t[gs].key=rd();
t[gs].size=1;
t[gs].val=zhi;
rt=merge(merge(x,gs),y);//将合并后的根记录下来,方便下次操作
}
int del(int zhi){
register int x,y,z;//分别记录三棵子树的根节点
split(rt,zhi,x,y);
split(x,zhi-1,x,z);
z=merge(t[z].ch[0],t[z].ch[1]);
rt=merge(merge(x,z),y);
}
void k_th(int zhi,int root){
int dq=root;
while(t[t[dq].ch[0]].size!=zhi-1){
if(t[t[dq].ch[0]].size<zhi){
zhi-=t[t[dq].ch[0]].size+1;
dq=t[dq].ch[1];
}
else{
dq=t[dq].ch[0];
}
}
printf("%d\n",t[dq].val);
}
void rank(int zhi){
int x,y;
split(rt,zhi-1,x,y);
printf("%d\n",t[x].size+1);
merge(x,y);
}
void pre(int zhi){
int x,y;
split(rt,zhi-1,x,y);
k_th(t[x].size,x);
merge(x,y);
}
void next(int zhi){
int x,y;
split(rt,zhi,x,y);
k_th(1,y);
merge(x,y);
}
int main(){
scanf("%d",&n);
srand(20180825);
for(int i=1;i<=n;i++){
scanf("%d %d",&com,&zhi);
if(com==1)insert(zhi);
else if(com==2)del(zhi);
else if(com==3)rank(zhi);
else if(com==4)k_th(zhi,rt);
else if(com==5)pre(zhi);
else if(com==6)next(zhi);
}
}
//splay
//代码长度:2562 运行时间:696ms
#include<iostream>
using namespace std;
struct node{
int ch[2],f,zhi,s,size;
}t[100010];
int n,gs,root,x,y;
inline void update(int x){
t[x].size=t[t[x].ch[0]].size+t[t[x].ch[1]].size+t[x].s;
}
inline void rotate(int x){
int y=t[x].f;
int z=t[y].f;
int c=t[y].ch[1]==x;
t[z].ch[t[z].ch[1]==y]=x;
t[x].f=z;
t[y].ch[c]=t[x].ch[c^1];
t[t[x].ch[c^1]].f=y;
t[x].ch[c^1]=y;
t[y].f=x;
update(y);
update(x);
}
inline void splay(int x,int goal){
while(t[x].f!=goal){
int y=t[x].f;
int z=t[y].f;
if(z!=goal){
if((t[z].ch[0]==y)^(t[y].ch[0]==x))rotate(x);
else rotate(y);
}
rotate(x);
}
if(goal==0)root=x;
}
inline void insert(int x){
int a=root,ff=0;
while(a && x!=t[a].zhi){
ff=a;
a=t[a].ch[t[a].zhi<x];
}
if(a){
t[a].s++;
splay(a,0);
}
else{
gs++;
t[gs].zhi=x;
t[gs].f=ff;
t[gs].s=t[gs].size=1;
t[ff].ch[t[ff].zhi<x]=gs;
t[gs].ch[0]=t[gs].ch[1]=0;
splay(gs,0);
}
}
inline void find(int x){
int a=root;
if(!a)return;
while(t[a].ch[t[a].zhi<x] && x!=t[a].zhi)a=t[a].ch[t[a].zhi<x];
splay(a,0);
}
//查询x的前驱/后继(flag=0/flag=1)
inline int next(int x,int flag){
find(x);
if((x<t[root].zhi && flag)||(x>t[root].zhi &&!flag))return root;
int a=t[root].ch[flag];
while(t[a].ch[flag^1])a=t[a].ch[flag^1];
return a;
}
inline void del(int x){
int y=next(x,0);
int z=next(x,1);
splay(y,0);
splay(z,y);
if(t[t[z].ch[0]].s>1){
t[t[z].ch[0]].s--;
splay(t[z].ch[0],0);
}
else t[z].ch[0]=0;
}
inline int rank(int x){
int a=root;
while(true){
if(x>t[a].s+t[t[a].ch[0]].size){
x-=t[a].s+t[t[a].ch[0]].size;
a=t[a].ch[1];
}
else if(x<=t[t[a].ch[0]].size)a=t[a].ch[0];
else break;
}
return t[a].zhi;
}
int main(){
ios::sync_with_stdio(false);
cin>>n;
insert(-2147483647);
insert(2147483647);
//插入俩数防止出现奇怪的问题,但是不插好像也行,只是询问排名可能需要+1-1什么的
for(int i=1;i<=n;i++){
cin>>x>>y;
switch(x){
case 1:insert(y);break;
case 2:del(y);break;
case 3:find(y),cout<<t[t[root].ch[0]].size<<"\n";break;
case 4:cout<<rank(y+1)<<"\n";break;
case 5:cout<<t[next(y,0)].zhi<<"\n";break;
case 6:cout<<t[next(y,1)].zhi<<"\n";break;
}
}
return 0;
}