题目一看就知道是一个平衡树,
但是这道题目并没有用 Treap 来写, 转而用 Splay 写了一下,也借鉴了以下板子
Splay 和 Treap 相比最大区别可能就是 多了一个 splay 操作,还有 Splay 不会像 Treap 那样只要加入节点就会立刻更新整个结构,而是根据询问,不断地把节点提升到根部,从而改变整个树结构内容,通过不断地均分复杂度,所以可能会跑的非常之快,所以相比Treap 加入一个处理一个, Splay 可能在加入节点时候不会有什么处理,主要处理过程在每个操作之中。
Splay 主要涉及到splay 操作,而且不需要通过 rand() 来维护堆性质的情况来优化整体结构,所以定义部分可能会有所区别
struct Splay
{
int v,s; //本题需要储存的信息,子树大小和当前节点权值
Splay *fa,*ch[2];// ch[0] 左儿子, ch[1] 右儿子 fa 是父节点
Splay() {}
Splay(int _v, Splay* _fa) : v(_v), s(1), fa(_fa)//构建新节点
{
ch[0] = ch[1] = NULL;
}
}*root, *pos[maxn];//pos 代表节点具体编号和位置
//以下是几个基本操作需要大量使用的,所以可以小小的封装一下
inline int size(Splay *k){return k == NULL? 0 : k->s;} // 直接通过size求得某个子树大小
inline int son(Splay *k){return k->fa == NULL ? 0 : k->fa->ch[1] == k;}//判断当前是哪个儿子
inline void pushup(Splay *k){ k->s = 1 + size(k->ch[1]) + size(k->ch[0]);}//更新大小
由于需要进行不断地把一个点变成根节点,我们自然需要知道它的父亲了,这样才能向上推
那么下边是我们的一个结合函数,可以理解成吧 A 变成 B 的子树,虽然有些板子可能没有,但是思路是一样的,这里写出来主要可以加深下理解。。。
inline void unite(Splay *_son, Splay *_fa, int which)//which 代表变成 fa 的哪个儿子
{
if(_son) _son->fa = _fa;
if(_fa) _fa->ch[which] = _son;
else root = _son;//如果没有父节点,那么当前儿子节点变成根节点
}
之后就是我们的旋转操作,由于存在左旋,右旋以及先左后右先右后左,连续左等等操作,我们直接写成一个函数,通过 01 判断是左旋还是右旋,这里我们可以归纳下 左旋右旋 相似点:(基本上只需要对三代进行判断)
①把自己连到爷爷上,方向和父亲相同
②把和自己方向相反的儿子连到自己的父亲上,且方向和自己相同
③把父亲连到自己上,方向和自己的方向相反
即:
反向子代我位,父代反向子位,我代父位
(注重理解,但强行记下来实际上也不难)
PS: 感谢巨巨博客中归纳,地址:https://blog.youkuaiyun.com/jtyj55454/article/details/84890936 尤其那句总结实在太强了
以下是代码,我们有了结合函数 所以旋转这一步的代码可读性就会好很多(当然如果足够熟练其实不需要结合函数)
inline void rotate(Splay *k)
{
Splay *p = k->fa, *q = p->fa;
int which = son(k);
unite(k, q, son(p));//参照步骤 1
unite(k->ch[which^1], p, which);//参照步骤 2
unite(p, k, which^1);// 参照步骤 3
pushup(p);
}
后边就是我们喜闻乐见的 splay 函数了,具体含义可以参照注释,整体思路就是把当前节点 k 移动到目标位置上去,一路通过旋转上位、
inline void splay(Splay *k, Splay *goal = NULL)//其实如果不引入 goal 默认为提升到根部
{
while(k->fa != goal)//如果没有到达目标点
{
Splay *p = k->fa, *q = p->fa; //观察三代人
q != goal ? rotate(son(k)^son(p) ? k : p) : rotate(k);
//这里可能复杂些,观察祖父是否是目标,如果祖父是目标,只要对当前节点旋转一次就好了,如果爷爷还不是目标,并且自己的方向和父亲的方向相同(都是各自父亲的左/右结点)那么就先选择父亲再旋转自己,否则连续旋转自己两遍 当然这里代码中没有写出来连续旋转,理论上双旋会跑的更快,以后有机会补上。。。
}
pushup(k);//更新
}
之后就是我们的插入操作,对于这道题,插入可能不太一样,但是基础模板插入思路很明了。
核心思路在于选左儿子里最小的当新根,或者右儿子里最大的当新根,保证数据结构最优
inline void insert(int v,int op)//这里的 op 代表题目中插入上边一个还是 下边一个 还是中间
{
if(op == 0)return;//放回的话自然没问题了
else if(op == -1)//放到原来的上一个 与前驱交换位置
{
splay(pos[v]);//提升到根部
Splay *k = root->ch[0];//看看左儿子存不存在
if(k == NULL)return;//如果不能再往上放了 那就不用搞了
while(k->ch[1])k=k->ch[1];//左边的肯定得放在
swap(pos[v], pos[k->v]);//交换位置
swap(root->v, k->v);//交换权值
}
else // 放到上一位
{
splay(pos[v]);//提根操作
Splay *k = root->ch[1];//看右儿子
if(k == NULL)return;
while(k->ch[0])k=k->ch[0];
swap(pos[v], pos[k->v]);
swap(root->v, k->v);
}
}
删除操作
inline void del(int x)
{
splay(pos[x]);//提上来
Splay *k = root;//附个临时值给根
if(k->ch[0] == NULL)如果是单链,翻转一下,根设为他的右儿子,直接消去原点
{
root = k->ch[1];
if(root) root->fa = NULL;
delete k;
}
else //如果另一种情况,把想要删除的点变成根,把左儿子众最大的提上来,和右儿子脸上,消去原点保证最优结构
{
Splay *tmp = root->ch[0];
while(tmp->ch[1]) tmp = tmp->ch[1];
splay(tmp, root);
root = tmp;
unite(k->ch[1], root, 1);
root->fa = NULL;
delete k;
}
}
建树操作
void build(Splay *&k = root, Splay *fa = NULL, int l = 1, int r = n)
{
if(l > r)return; // 根据中值建树
int mid = (l+r)>>1;
k = new Splay(a[mid], fa);
pos[a[mid]] = k;
build(k->ch[0], k, l, mid-1);
build(k->ch[1], k, mid+1, r);
pushup(k);
}
之后就是 top 和 bottom 操作了
inline void top(int x)//很简单,删了原点,找到最左,新建个节点,更新位置,提根
{
del(x);
Splay *k = root;
while(k->ch[0]) k = k->ch[0];
k->ch[0] = new Splay(x, k);
pos[x] = k->ch[0];
splay(k->ch[0]);
}
inline void bottom(int x)//同理了
{
del(x);
Splay *k = root;
while(k->ch[1])k=k->ch[1];
k->ch[1] = new Splay(x, k);
pos[x] = k->ch[1];
splay(k->ch[1]);
}
全部代码如下
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5+5;
inline int read()
{
register int x=0, w=1; register char ch = getchar();
while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
while(ch<='9'&&ch>='0'){x=x*10+ch-'0';ch=getchar();}
return w*x;
}
int n,m;
int a[maxn];
struct Splay
{
int v,s;
Splay *fa,*ch[2];
Splay() {}
Splay(int _v, Splay* _fa) : v(_v), s(1), fa(_fa)
{
ch[0] = ch[1] = NULL;
}
}*root, *pos[maxn];
inline int size(Splay *k){return k == NULL? 0 : k->s;}
inline int son(Splay *k){return k->fa == NULL ? 0 : k->fa->ch[1] == k;}
inline void pushup(Splay *k){ k->s = 1 + size(k->ch[1]) + size(k->ch[0]);}
inline void unite(Splay *_son, Splay *_fa, int which)
{
if(_son) _son->fa = _fa;
if(_fa) _fa->ch[which] = _son;
else root = _son;
}
inline void rotate(Splay *k)
{
Splay *p = k->fa, *q = p->fa;
int which = son(k);
unite(k, q, son(p));
unite(k->ch[which^1], p, which);
unite(p, k, which^1);
pushup(p);
}
inline void splay(Splay *k, Splay *goal = NULL)
{
while(k->fa != goal)
{
Splay *p = k->fa, *q = p->fa;
q != goal ? rotate(son(k)^son(p) ? k : p) : rotate(k);
}
pushup(k);
}
inline void del(int x)
{
splay(pos[x]);
Splay *k = root;
if(k->ch[0] == NULL)
{
root = k->ch[1];
if(root) root->fa = NULL;
delete k;
}
else
{
Splay *tmp = root->ch[0];
while(tmp->ch[1]) tmp = tmp->ch[1];
splay(tmp, root);
root = tmp;
unite(k->ch[1], root, 1);
root->fa = NULL;
delete k;
}
}
inline void top(int x)
{
del(x);
Splay *k = root;
while(k->ch[0]) k = k->ch[0];
k->ch[0] = new Splay(x, k);
pos[x] = k->ch[0];
splay(k->ch[0]);
}
inline void bottom(int x)
{
del(x);
Splay *k = root;
while(k->ch[1])k=k->ch[1];
k->ch[1] = new Splay(x, k);
pos[x] = k->ch[1];
splay(k->ch[1]);
}
inline void insert(int v,int op)
{
if(op == 0)return;
else if(op == -1)
{
splay(pos[v]);
Splay *k = root->ch[0];
if(k == NULL)return;
while(k->ch[1])k=k->ch[1];
swap(pos[v], pos[k->v]);
swap(root->v, k->v);
}
else
{
splay(pos[v]);
Splay *k = root->ch[1];
if(k == NULL)return;
while(k->ch[0])k=k->ch[0];
swap(pos[v], pos[k->v]);
swap(root->v, k->v);
}
}
inline int ask(int x)
{
splay(pos[x]);
return size(root->ch[0]);
}
inline int query(int x)
{
Splay *k = root;
while(true)
{
if(size(k -> ch[0]) < x && size(k -> ch[0]) + 1 >= x) return k -> v;
else if(size(k -> ch[0]) >= x) k = k -> ch[0];
else
{
x -= size(k -> ch[0]) + 1;
k = k -> ch[1];
}
}
}
void build(Splay *&k = root, Splay *fa = NULL, int l = 1, int r = n)
{
if(l > r)return;
int mid = (l+r)>>1;
k = new Splay(a[mid], fa);
pos[a[mid]] = k;
build(k->ch[0], k, l, mid-1);
build(k->ch[1], k, mid+1, r);
pushup(k);
}
int main()
{
n = read(), m=read();
for(int i=1;i<=n;i++) a[i] = read();
build();
while(m --)
{
char opt[10];
scanf("%s",opt);
if(*opt == 'T')top(read());
else if(*opt == 'B')bottom(read());
else if(*opt == 'I')
{
int a=read(), b=read();
insert(a, b);
}
else if(*opt == 'A')printf("%d\n",ask(read()));
else printf("%d\n",query(read()));
}
return 0;
}