欠了好久的Splay,以后就它了。

默写真不容易,过几天估计就忘了..

整个Splay真的精妙,不拖泥带水那种..

前驱后继之所以不能用rk转到根,是因为这个数不一定存在。。

kth中<=老忘记=

#include<iostream>
#include<cstdio>
#define check(x) (x==ch[fa[x]][1])
#define clear(x) val[x]=cnt[x]=siz[x]=ch[x][0]=ch[x][1]=fa[x]=0
#define pushup(x) siz[x]=siz[ch[x][0]]+siz[ch[x][1]]+cnt[x] using namespace std; const int MAXN=100005; int val[MAXN],cnt[MAXN],siz[MAXN];
int ch[MAXN][2],fa[MAXN];
int tot,root;
inline int newnode(int x){cnt[++tot]++;siz[tot]=1;val[tot]=x;return tot;}
void rotate(int x){
int y=fa[x],z=fa[fa[x]];
bool ck=check(x);
fa[ch[x][ck^1]]=y;ch[y][ck]=ch[x][ck^1];
ch[x][ck^1]=y;fa[y]=x;fa[x]=z;
if(z) ch[z][ch[z][1]==y]=x;
pushup(y);pushup(x);
}
void splay(int x){
for(int f=fa[x];f;rotate(x),f=fa[x])
if(fa[f]) rotate(check(f)==check(x)?f:x);
root=x;
}
void insert(int x){
if(!root){root=newnode(x);return;}
int cur=root,f=0;
while(1){
if(val[cur]==x){cnt[cur]++;pushup(cur);pushup(f);splay(cur);return;}
f=cur;cur=ch[cur][x>val[cur]];
if(!cur){cur=newnode(x);fa[cur]=f;ch[f][x>val[f]]=cur;pushup(f);splay(cur);return;}
}
}
int rk(int x){
int cur=root,ret=0;
while(1){
if(x<val[cur]) cur=ch[cur][0];
else{
ret+=siz[ch[cur][0]];
if(val[cur]==x) return splay(cur),ret+1;//
ret+=cnt[cur];cur=ch[cur][1];
}
}
}
int kth(int x){
int cur=root;
while(1){
if(ch[cur][0]&&x<=siz[ch[cur][0]]) cur=ch[cur][0];
else{
x-=siz[ch[cur][0]]+cnt[cur];
if(x<=0) return cur;
cur=ch[cur][1];
}
}
}
int prev(){
int cur=ch[root][0];
while(ch[cur][1]) cur=ch[cur][1];
return cur;
}
int nxtv(){
int cur=ch[root][1];
while(ch[cur][0]) cur=ch[cur][0];
return cur;
}
void del(int x){
rk(x);
if(cnt[root]>1){cnt[root]--;pushup(root);return;}
if(!ch[root][0]&&!ch[root][1]){clear(root);root=0;return;}
if(!ch[root][0]){int sav=root;root=ch[sav][1];fa[root]=0;clear(sav);return;}
if(!ch[root][1]){int sav=root;root=ch[sav][0];fa[root]=0;clear(sav);return;}
int sav=root;
splay(prev());
ch[root][1]=ch[sav][1];
fa[ch[sav][1]]=root;
clear(sav);
pushup(root);
} int n; inline int rd(){
int ret=0,f=1;char c;
while(c=getchar(),!isdigit(c))f=c=='-'?-1:1;
while(isdigit(c))ret=ret*10+c-'0',c=getchar();
return ret*f;
} int main(){
n=rd();
int x,y;
for(int i=1;i<=n;i++){
x=rd();y=rd();
switch(x){
case 1:{insert(y);break;}
case 2:{del(y);break;}
case 3:{printf("%d\n",rk(y));break;}
case 4:{printf("%d\n",val[kth(y)]);break;}
case 5:{insert(y);printf("%d\n",val[prev()]);del(y);break;}
case 6:{insert(y);printf("%d\n",val[nxtv()]);del(y);break;}
}
}
return 0;
}

[模板] Splay的更多相关文章

  1. 算法模板——splay区间反转 2

    实现功能:同splay区间反转 1(基于BZOJ3223 文艺平衡树) 这次改用了一个全新的模板(HansBug:琢磨了我大半天啊有木有),大大简化了程序,同时对于splay的功能也有所完善 这里面没 ...

  2. 算法模板——splay区间反转 1

    实现的功能:将序列区间反转,并维护 详见BZOJ3223 var i,j,k,l,m,n,head,a1,a2:longint; s1:ansistring; a,b,c,d,fat,lef,rig: ...

  3. 【luogu P3369 普通平衡树(Treap/SBT)】 模板 Splay

    题目链接:https://www.luogu.org/problemnew/show/P3369 #include <cstdio> #include <algorithm> ...

  4. 洛谷 P3391 模板Splay

    #include<bits/stdc++.h> using namespace std; #define maxn 200000 int read() { ,w=; ;ch=getchar ...

  5. 模板—splay

    #include<iostream> #include<cstdio> #define cin(x) scanf("%d",&x) using na ...

  6. 模板——Splay

    $Splay$ #include <bits/stdc++.h> #define inf (int)1e9 using namespace std; const int N=1e5+100 ...

  7. Splay 伸展树

    废话不说,有篇论文可供参考:杨思雨:<伸展树的基本操作与应用> Splay的好处可以快速分裂和合并. ===============================14.07.26更新== ...

  8. [NOI2003][bzoj1507] 文本编辑器 editor [splay]

    其实看明白了就是一道水题 毕竟模板 splay敲一发,插入一个串的时候先把它构建成一棵平衡树,再挂到原来的splay上面去即可 没别的了,上代码 #include<iostream> #i ...

  9. splay最终模板

    来自wjmzbmr的splay模板 #include<cstdio> #include<iostream> #include<algorithm> using na ...

随机推荐

  1. 关于ArcGis for javascript的引用天地图

    1. 在引用天地图时, 我们要自定义一个相关的比例尺转换类 const tileInfoObj = { rows: 256, cols: 256, compressionQuality: 0, ori ...

  2. asp.net web.config配置节说明(转发)

    原文地址:http://www.cnblogs.com/qingyuan/articles/1501644.html web.config 文件查找规则:      (1)如果在当前页面所在目录下存在 ...

  3. 使用root用户登录到AWS EC2服务器,上传文件到/var/www目录

    关键词 1.aws ec2中上传文件到/var/www目录(使用filezilla) 2.使用root用户登录aws ec2实例 上一篇随笔中记录了在aws ec2实例中部署apache服务器的过程, ...

  4. 通过实现ServletContextListener接口创建数据库连接池(C3P0方式)

    使用Listener步骤 1. 定义Listener实现类 2. 在web.xml中配置(或使用Annotation) 使用C3P0方式创建数据库连接池需要添加的jar包 1.c3p0-0.9.5.j ...

  5. JavaScript实现对象的深度克隆及typeof和instanceof【简洁】【分享】

    JavaScript实现对象的深度克隆 代码实现如下: <!DOCTYPE html> <html lang="en"> <head> < ...

  6. 洛谷P4116 Qtree3(树剖+线段树)

    传送门 LCT秒天秒地 树剖比较裸的题了 用线段树记录一下区间的最左边的黑点的编号(因为同一条链上肯定是最左边的深度最小,到根节点距离最近) 然后记得树剖的时候肯定是越后面的答案越优,因为深度越浅 / ...

  7. [USACO 2012 Jan Silver] Delivery Route【拆点】

    传送门:http://www.usaco.org/index.php?page=viewproblem2&cpid=106 这道题还真是完全没思路,真的不知道怎么做,但是看了题解后恍然大悟. ...

  8. 文件输入输出C++操作

    基于C++的文件操作 在C++中,有一个stream这个类,所有的I/O都以这个"流"类为基础的,包括我们要认识的文件I/O,stream这个类有两个重要的运算符: 1.插入器(& ...

  9. 输入一个秒数,要求转换为XX小时XX分XX秒的格式输出出来;

    package arithmetic; import java.util.Scanner; import org.junit.Test; public class Test02 { /** * 输入一 ...

  10. AngularJs调用NET MVC 控制器中的函数进行后台操作

    题目中提到的控制器指的是.NET  MVC的控制器,不是angularjs的控制器. 首先看主页面的代码: <!DOCTYPE html> <html> <head> ...