欠了好久的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. bzoj 3609: [Heoi2014]人人尽说江南好【博弈论】

    参考:https://blog.csdn.net/Izumi_Hanako/article/details/80189596 胜负和操作次数有关,先手胜为奇,所以先手期望奇数后手期望偶数,最后一定能达 ...

  2. Ubuntu 18.04 关闭蓝牙开机启动

    sudo gedit /etc/rc.local 然后,加入下面一行 rfkill block bluetooth

  3. saltstack实战笔记

    #运维管理工具 ansible #支持ssh,不需要客户端 saltstack #也是只是ssh,不需要客户端的 安装架构是,master /minion 安装salt的依赖模块 python zer ...

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

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

  5. Cenos7 切换单用户模式

    CentOS 7在进入单用户的时候和6.x做了很多改变,下面让我们来看看如何进入单用户. 1.重启服务器,在选择内核界面使用上下箭头移动 2.选择内核并按“e” 3.修改参数 将rhgb quiet ...

  6. sed练习第一节

    ed语法和基本命令 employee.txt文件内容如下: 101,John Doe,CEO 102,Jason Smith,IT Manager 103,Raj Reddy,Sysadmin 104 ...

  7. 安装SNMP

    http://songknight.blog.51cto.com/2599480/655337

  8. outlook 通讯录分类--2017年1月16日--对联系人分类管理

    outlook功能多,复杂,导致打开界面就晕,通讯录分类 问:在Outlook 中,随着联系人数量的增多,亲朋好友.同事.客户的信息混杂在一起,每次发邮件都要用很长时间才能从联系人列表中找到需要的人. ...

  9. 开发一个 Web App 必须了解的那些事

    在过去的一年里,我在从头开始开发我的第一个重要的Web应用.经验教会了很多以前不知道的东西,特别是在安全性和用户体验方面. 值得一提的是,我上一次尝试构建的任何合理复杂性是在2005年.所以,在安全防 ...

  10. 外文翻译 《How we decide》赛场上的四分卫 第二节

    本书导言翻译 本章第一节 "决定是如何做出来的",关于意识最神秘的问题之一.尽管我们时刻做着决定,但是我们没有感觉到大脑内部的一系列有关进程.NFL球探挑选候选球员的评分表中,决策 ...