int key[100005];
int fa[100005];
int ch[100005][2];
int root,tot,t1,t2;
bool dir(int x)
{
return x==ch[fa[x]][1];
}
void rotate(int x)
{
bool b=dir(x);
int y=fa[x];
int z=fa[y],a=ch[x][!b];
if(z==0)
root=x;
else
ch[z][dir(y)]=x;
fa[x]=z;
ch[x][!b]=y;
fa[y]=x;
ch[y][b]=a;
if(a) fa[a]=y;
}
void splay(int x,int i)
{
while(fa[x]!=i)
{
int y=fa[x];
int z=fa[y];
if(z==i)
rotate(x);
else
{
bool b=dir(x),c=dir(y);
if(b^c)
{
rotate(x);
rotate(x);
}
else
{
rotate(y);
rotate(x);
}
}
}
}
void insert(int &k,int x,int last)
{
if(k==0)
{
k=++tot;
key[k]=x;
fa[k]=last;
splay(k,0);
return ;
}
if(x<key[k])
insert(ch[k][0],x,k);
else
insert(ch[k][1],x,k);
}
void find_pre(int i,int x)
{
if(i==0)
return ;
if(key[i]<x)
{
t1=i;
find_pre(ch[i][1],x);
}
else
find_pre(ch[i][0],x);
}
void find_nxt(int i,int x)
{
if(i==0)
return ;
if(key[i]>x)
{
t2=i;
find_nxt(ch[i][0],x);
}
else
find_nxt(ch[i][1],x);
}
void del(int x)
{
splay(x,0);
if(!ch[x][0])
{
root=ch[x][1];
fa[ch[x][1]]=0;
return ;
}
if(!ch[x][1])
{
root=ch[x][0];
fa[ch[x][0]]=0;
return ;
}
find_pre(root,key[x]);
splay(t1,root);
ch[t1][1]=ch[root][1];
fa[ch[root][1]]=t1;
root=t1; fa[t1]=0;
}
应该没有大问题(坑)
最新推荐文章于 2023-07-20 08:00:00 发布