#include<iostream>
#include<cstdio>
#define cin(x) scanf("%d",&x)
using namespace std;
int ch[][],key[],
cnt[],size[],sz,rt,f[];
bool get(int x)
{
return ch[f[x]][]==x;
}
void clear(int x)
{
f[x]=cnt[x]=ch[x][]=ch[x][]=size[x]=key[x]=;
}
void pushup(int x)
{
if(x)
{
size[x]=cnt[x];
if(ch[x][])size[x]+=size[ch[x][]];
if(ch[x][])size[x]+=size[ch[x][]];
}
}
void rotate(int x)
{
int old=f[x],oldf=f[old],which=get(x);
ch[old][which]=ch[x][which^];f[ch[old][which]]=old;
ch[x][which^]=old;f[old]=x;
f[x]=oldf;
if(oldf)ch[oldf][ch[oldf][]==old]=x;
pushup(old),pushup(x);
}
void splay(int x)
{
for(int fa;fa=f[x];rotate(x))
if(f[fa])
rotate(get(x)==get(fa)?fa:x);
rt=x;
}
void insert(int x)
{
if(rt==)
{
sz++;key[sz]=x;rt=sz;
cnt[sz]=size[sz]=;
f[sz]=ch[sz][]=ch[sz][]=;
return;
}
int now=rt,fa=;
while()
{
if(x==key[now])
{
cnt[now]++;
pushup(now);
pushup(fa);
splay(now);
return;
}
fa=now;
now=ch[now][x>key[now]];
if(now==)
{
sz++;
size[sz]=cnt[sz]=;
ch[sz][]=ch[sz][]=;
ch[fa][x>key[fa]]=sz;
f[sz]=fa;
key[sz]=x;
pushup(fa);
splay(sz);
return;
}
}
}
int rnk(int x)
{
int now=rt,ans=;
while()
{
if(ch[now][] && x<key[now])now=ch[now][];
else
{
ans+=size[ch[now][]];
if(x==key[now])
{
splay(now);
return ans+;
}
ans+=cnt[now];
now=ch[now][];
}
}
}
int kth(int x)
{
int now=rt;
while()
{
if(ch[now][] && x<=size[ch[now][]])now=ch[now][];
else
{
int temp=size[ch[now][]]+cnt[now];
if(temp>=x)
return key[now];
x-=temp;now=ch[now][];
}
}
}
int pre()
{
int now=ch[rt][];
while(ch[now][])now=ch[now][];
return now;
}
int next()
{
int now=ch[rt][];
while(ch[now][])now=ch[now][];
return now;
}
void del(int x)
{
rnk(x);
if(cnt[rt]>){cnt[rt]--;pushup(rt);return;}
if(!ch[rt][] && !ch[rt][]){clear(rt);rt=;return;}
if(!ch[rt][])
{
int old=rt;rt=ch[rt][];f[rt]=;clear(old);return;
}
else if(!ch[rt][])
{
int old=rt;rt=ch[rt][];f[rt]=;clear(old);return;
}
int oldrt=rt,leftbig=pre();
splay(leftbig);//leftbig无儿子,所以oldrt无左二子
ch[rt][]=ch[oldrt][];
f[ch[oldrt][]]=rt;
clear(oldrt);
pushup(rt); }
signed main()
{
// freopen("input5.in","r",stdin); int n,opt,x;
cin(n);
while(n--)
{
cin(opt);cin(x);
if(opt==)insert(x);
if(opt==)del(x);
if(opt==)printf("%ld\n",rnk(x));
if(opt==)printf("%ld\n",kth(x));
if(opt==){insert(x);printf("%ld\n",key[pre()]);del(x);}
if(opt==){insert(x);printf("%ld\n",key[next()]);del(x);}
}
}

模板—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

    欠了好久的Splay,以后就它了. 默写真不容易,过几天估计就忘了.. 整个Splay真的精妙,不拖泥带水那种.. 前驱后继之所以不能用rk转到根,是因为这个数不一定存在.. kth中<=老忘记 ...

  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. web 开发之js---js 实现网页中播放wav的一种方法(flash播放器)

    http://blog.csdn.net/whumr1/article/details/6948160

  2. STM32学习之路-感觉自己走到了一个天大的坑里了!

    先前兴致勃勃的来弄16位并口驱动LCD.本以为就须要改下LCD IC的初始化即可了,没想到弄了这么多天最终发现自己走进了一个深坑了 T T 原因是我的开发板是奋斗V5的, 它确实有MCU外扩IO口, ...

  3. Codeforces Round #233 (Div. 2)D. Painting The Wall 概率DP

                                                                                   D. Painting The Wall ...

  4. the largest value you actually can transmit between the client and server is determined by the amount of available memory and the size of the communications buffers.

    the largest value you actually can transmit between the client and server is determined by the amoun ...

  5. ZOJ 3609 Modular Inverse(扩展欧几里德)

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4712 The modular modular multiplicat ...

  6. 纯css3实现美化复选框和手风琴效果(详细)

    关键技术点和原理: 原理就是把 checkbox或 radio 给隐藏掉   ,然后给选框 绑定一个label标签. 然后用label标签作为容器,在里面放一个:before或一个after 用bef ...

  7. 假如Java对象是个人······

    假如Java对象是个人,那意味着它也具备了我们人所有的东西,头,身体,大长腿. 头 头就是我们的对象头(Header).根据JAVA虚拟机规范,我们的对象头分为两部分,分别是存储对象自身的运行时数据和 ...

  8. bzoj3561

    3561: DZY Loves Math VI Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 240  Solved: 163[Submit][Sta ...

  9. .NET Runtime version 2.0.50727.8762 - 执行引擎错误(7969097A) (80131506)

    VS2010调试IIS发布的web工程提示:无法连接到 Visual Studio 开发服务器 .NET Runtime version 2.0.50727.8762 - 执行引擎错误(7969097 ...

  10. Yii2笔记一

    环境LNMP,通过Composer安装 安装Composer(已经安装请跳过) curl -s http://getcomposer.org/installer | php #php可执行文件所在位置 ...