欠了好久的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. Java多线程系列七——ExecutorService

    java.util.concurrent.ExecutorService接口提供了许多线程管理的方法 Method 说明 shutdown 拒绝接收新的任务,待已提交的任务执行后关闭,且宿主线程不阻塞 ...

  2. svn报错:privious operation has not finshed;run 'cleanup' if it was interrupted

    在更新svn的过程中,可能中途会取消,取消之后再次更新时可能提示,如下图: 下载sqlite3工具,进入此下载地址:https://www.sqlite.org/download.html 将sqli ...

  3. bzoj 1084: [SCOI2005]最大子矩阵【dp】

    分情况讨论,m=1的时候比较简单,设f[i][j]为到i选了j个矩形,前缀和转移一下就行了 m=2,设f[i][j][k]为1行前i个,2行前j个,一共选了k个,i!=j的时候各自转移同m=1,否则转 ...

  4. python之定时器Timer

    timer类 Timer(定时器)是Thread的派生类,用于在指定时间后调用一个方法. 构造方法: Timer(interval, function, args=[], kwargs={})  in ...

  5. java基本数据类型所占字节数

    JAVA基本数据类型所占字节数是多少?(32位系统) byte     1字节                short    2字节                int      4字节      ...

  6. Styles and Themens(1)详述

    Styles and Themes IN THIS DOCUMENT Defining Styles Inheritance Style Properties Applying Styles and ...

  7. JD商家后台管理的细节

    1: 宝贝主图和滚动图都是800px,只有刚好这么多时才能得到显示,否则不会显示. 2:宝贝描述图只支持750px, 只有这么多时才能得到显示, 刚开始不知道, 上传图片上去后, 发现始终无法显示, ...

  8. 大步小步法(BSGS) 学习笔记

    \(\\\) BSGS 用于求解关于 \(x\) 的方程: \[ a^x\equiv b\pmod p\ ,\ (p,a)=1 \] 一般求解的是模意义下的指数,也就是最小非负整数解. \(\\\) ...

  9. EditText自动弹出软键盘

    editText.requestFocus() editText.isFocusable = true editText.isFocusableInTouchMode = true val timer ...

  10. Android一句代码给Activity定制标题栏

    在此之前,使用过几种方法设置标题栏: 1.常规法:这个方法是最常用的了,哪个activity需要什么样的标题栏,就在对应的xml布局设计.缺点:此方法维护起来困难,没有将标题栏的共性抽取出来, 如果要 ...