bzoj 3224 splay模板题4

再刷水题我就废了。。。

 #include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#define lc(x) ch[x][0]
#define rc(x) ch[x][1]
#define inf 0x3f3f3f3f
#define N 200005
using namespace std;
int ch[N][];
int k[N];int cnt,root;
int size[N],fa[N];
void push_up(int x)
{
size[x]=size[ch[x][]]+size[ch[x][]]+;
}
void rotate(int p)
{
int q=fa[p],y=fa[q],x=(p==ch[q][]);
ch[q][x]=ch[p][x^];fa[ch[q][x]]=q;
ch[p][x^]=q;fa[q]=p;
fa[p]=y;
if(y)
{
if(q==ch[y][])ch[y][]=p;
else ch[y][]=p;
}
push_up(q);push_up(p);
return ;
}
void splay(int x)
{
for(int y;y=fa[x];rotate(x))
{
if(fa[y])
{
if((ch[y][]==x&&ch[fa[y]][]==y)||(ch[y][]==x&&ch[fa[y]][]==y))rotate(y);
else rotate(x);
}
}
root=x;
}
int pre(int v)
{
int x=root;int tmp=-inf;
while(ch[x][k[x]<v])
{
if(k[x]<v)tmp=k[x];
x=ch[x][k[x]<v];
}if(k[x]<v)tmp=k[x];
return tmp;
}
int suc(int v)
{
int x=root;int tmp=inf;
while(ch[x][k[x]<=v])
{
if(k[x]>v)tmp=k[x];
x=ch[x][k[x]<=v];
}if(k[x]>v)tmp=k[x];
return tmp;
}
int find(int z)
{
int x=root;
if(k[x]==z)return x;
while(ch[x][k[x]<z])
{
x=ch[x][k[x]<z];
if(k[x]==z)return x;
}
return ;
}
void insert(int z)
{
int x=root;size[x]++;
while(ch[x][k[x]<z])x=ch[x][k[x]<z],size[x]++;
cnt++;ch[x][k[x]<z]=cnt;k[cnt]=z;size[cnt]=;fa[cnt]=x;splay(cnt);
}
void del(int x)
{
splay(x);
if(!ch[x][])
{
root=ch[x][];fa[ch[x][]]=;
}
else if(!ch[x][])
{
root=ch[x][];fa[ch[x][]]=;
}
else
{
fa[ch[x][]]=;int tmp=ch[x][];
while(ch[tmp][])tmp=ch[tmp][];
splay(tmp);ch[tmp][]=ch[x][];fa[ch[x][]]=tmp;
push_up(tmp);
}
return ;
}
int fd(int kk,int x)
{
int l=ch[kk][];int r=ch[kk][];
if(size[l]+==x)return k[kk];
if(size[l]>=x)return fd(l,x);
return fd(r,x-size[l]-);
}
int pr(int z)
{
int ans=;
int x=root;
while(x)
{
if(k[x]<=z)
{
ans+=size[ch[x][]]+;
x=ch[x][];
}
else x=ch[x][];
}
return ans;
}
void yu()
{
root=;k[]=inf;cnt=;size[]=;
insert(-inf);
}
int main()
{
yu();
int n;
scanf("%d",&n);
for(int o=;o<=n;o++)
{
int t1,t2;
scanf("%d%d",&t1,&t2);
if(t1==)
{
insert(t2);
}
else if(t1==)
{
int x1=find(t2);
if(x1!=)del(x1);
}
else if(t1==)
{
printf("%d\n",pr(t2-));
}
else if(t1==)
{
printf("%d\n",fd(root,t2+));
}
else if(t1==)
{
printf("%d\n",pre(t2));
}
else
{
printf("%d\n",suc(t2));
}
}
return ;
}
上一篇:Maven的pom.xml标签详解


下一篇:js自动切换图片