[题目链接] https://www.luogu.org/problemnew/show/P2596

平衡树,需支持五个操作:

1、 将某元素置顶:将元素旋到根,然后将左子树合并到该元素的后继

2、 将某元素置底:将元素旋到根,然后将右子树合并到该元素的前驱

3、 将某元素提前/滞后1位:直接与该元素的前驱/后继交换位置及信息

4、 询问指定元素排名:将元素旋到根,输出size-1

5、 询问指定排名元素:在树上find

必须维护每个点的位置,才能对它操作,不然找不到它

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int INF=1e9+7;
inline LL read(){
register LL x=0,f=1;register char c=getchar();
while(c<48||c>57){if(c=='-')f=-1;c=getchar();}
while(c>=48&&c<=57)x=(x<<3)+(x<<1)+(c&15),c=getchar();
return f*x;
} const int MAXN=80005; namespace splay{
int ch[MAXN][2],par[MAXN],num[MAXN],pos[MAXN],size[MAXN];
int Ncnt,rt; inline bool chk(int x){
return ch[par[x]][1]==x;
} inline void pushup(int x){
size[x]=size[ch[x][0]]+size[ch[x][1]]+1;
pos[num[ch[x][0]]]=ch[x][0],pos[num[ch[x][1]]]=ch[x][1];
} inline void rotate(int x){
int y=par[x],z=par[y],k=chk(x),w=ch[x][k^1];
ch[y][k]=w;par[w]=y;
ch[z][chk(y)]=x;par[x]=z;
ch[x][k^1]=y;par[y]=x;
pushup(y);pushup(x);
} inline void splay(int x,int goal=0){
while(par[x]!=goal){
int y=par[x],z=par[y];
if(z!=goal){
if(chk(x)==chk(y)) rotate(y);
else rotate(x);
}
rotate(x);
}
if(!goal) rt=x;
} inline void insert1(int val){
ch[rt][1]=++Ncnt,num[Ncnt]=val,par[Ncnt]=rt,size[Ncnt]=1;//按顺序插入,直接放在最右边(中序遍历保证正确性)
splay(Ncnt);
} inline void top(int x){//旋到根节点,把左儿子接到它的后继
splay(pos[x]);
if(!ch[rt][0]) return;
if(!ch[rt][1]) ch[rt][1]=ch[rt][0],ch[rt][0]=0;
else{
int suc=ch[rt][1];
while(ch[suc][0]) suc=ch[suc][0];
ch[suc][0]=ch[rt][0];
par[ch[rt][0]]=suc;
ch[rt][0]=0;
splay(ch[suc][0]);
}
} inline void bottom(int x){//旋到根节点,把右儿子接到它的前驱
splay(pos[x]);
if(!ch[rt][1]) return;
if(!ch[rt][0]) ch[rt][0]=ch[rt][1],ch[rt][1]=0;
else{
int pre=ch[rt][0];
while(ch[pre][1]) pre=ch[pre][1];
ch[pre][1]=ch[rt][1];
par[ch[rt][1]]=pre;
ch[rt][1]=0;
splay(ch[pre][1]);
}
} inline void insert2(int x,int t){
if(t==0) return;
splay(pos[x]);
if(t==1){
int suc=ch[rt][1];
while(ch[suc][0]) suc=ch[suc][0];
int ps=pos[x];
swap(pos[x],pos[num[suc]]);//交换信息
swap(num[ps],num[suc]);
}
if(t==-1){
int pre=ch[rt][0];
while(ch[pre][1]) pre=ch[pre][1];
int ps=pos[x];
swap(pos[x],pos[num[pre]]);
swap(num[ps],num[pre]);
}
} inline int kth(int k){
int cur=rt;
while(1){
if(k<=size[ch[cur][0]]) cur=ch[cur][0];
else if(k==size[ch[cur][0]]+1) return num[cur];
else k-=size[ch[cur][0]]+1,cur=ch[cur][1];
}
} inline int query(int x){
splay(pos[x]);
return size[ch[rt][0]];
}
}using namespace splay; int n,m; int main(){
n=read(),m=read();
for(int i=1;i<=n;i++) insert1(read());
for(int i=1;i<=m;i++){
char s[10];
scanf("%s",s);
switch (s[0]){
case 'T': top(read()); break;
case 'B': bottom(read()); break;
case 'I':{
int x=read(),y=read();
insert2(x,y);
break;
}
case 'Q': printf("%d\n",kth(read())); break;
case 'A': printf("%d\n",query(read())); break;
}
}
}

最新文章

  1. 帆布指纹识别(canvas fingerprinting)
  2. [IOS]Swift使用SVGKit的记录
  3. 洛谷P2735 电网 Electric Fences
  4. LoadRunner - 实战,转发
  5. Codeforces Round #327 (Div. 2) C. Median Smoothing 找规律
  6. Android_Intent_passValue(4)
  7. 字符编码笔记:ASCII,Unicode和UTF-8,附带 Little endian和Big endian的解释
  8. C#调用C++DLL传递结构体数组的终极解决方案
  9. poj2136---输出特殊图形
  10. java自己主动打开包装盒很容易导致两个误区
  11. 【转】php缓冲 output_buffering和ob_start
  12. 基于 HTML5 WebGL 的 3D 网络拓扑图
  13. spring Boot+spring Cloud实现微服务详细教程第二篇
  14. Android必知必会--GreenDao缓存
  15. C++std函数之transform
  16. js对内容进行编码(富文本编辑器使用居多)
  17. port bridge enable命令导致的环路
  18. JavaScript热身练习1
  19. ansible常用模块用法
  20. Unicode vs. UTF-8 etc.

热门文章

  1. hbase.client.RetriesExhaustedException: Can&#39;t get the locations hive关联Hbase查询报错
  2. [P3812][模板]线性基
  3. c#基础;初步学习循环语句
  4. ubuntu16安装pylearn2 出现错误提示importerror:no module named six.moves
  5. C语言访问网页
  6. Python程序设计2——列表和元组
  7. Java多线程并发学习-进阶大纲
  8. Mac 使用 NFS 连接 Centos 上的共享文件夹
  9. gRPC官方文档(异步基础: C++)
  10. UI控件相关宏定义