#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. UVA1523-Helicopter(暴力+全排列)

    题目链接 题意:有八个乘客坐在直升机上,求重心M最小值. 思路:依据题目所给的公式,我们能够知道要使得M最小.也就是要使得Mv和Mh的和最小,我们能够使用全排列,分别将每一个值放在各个位子上,然后更新 ...

  2. linux 和win7 双系统模式下 忘记win7 密码的修改方法

    首先登陆linux系统,在linux 系统下找到win7系统的安装盘(比如为C盘), (1)进入到C://windows/system32下 找到osk.exe 文件,并将其剪切到其他的地方(记住这个 ...

  3. hdoj--5526--欧拉回路(欧拉回路)

     欧拉回路 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Su ...

  4. Redis设置认证密码

    1.找到Redis里的redis.conf配置文件:搜素requirepass所在的行,格式为:requirepass password 2.redis-cli客户端登陆格式:redis-cli -a ...

  5. bzoj 1643: [Usaco2007 Oct]Bessie's Secret Pasture 贝茜的秘密草坪【枚举】

    4维枚举平方小于10000的数,相加等于n则ans++ #include<iostream> #include<cstdio> using namespace std; con ...

  6. crosswalk 初步使用

    这里简单的说下 crossWalk 的配置; 我在学习 crosswalk 的时候,看到一篇博客,他所记录的已经是很完整的教程了: 点击查看 本文就根据该博客和官网说明,进行部分修改,补充; 首先 c ...

  7. 《Maven实战》(许晓斌)导读(读书笔记&第二次读后感)

    第一章 Maven简介 Maven是构建工具,但同时还是jar包管理工具.项目信息管理工具 与Make.Ant比较,更为先进 第二章 Maven的安装和配置 Windows和Unix上安装都很简单,下 ...

  8. jsonp 监控简陋代码

    url: window.location.href Agent: navigator.userAgent var tkInfo = { VisitUrl: window.location.href, ...

  9. C++小项目-吃豆子游戏

    GMap.h #pragma once //保证头文件只被编译一次 #include "stdafx.h" #define MAP_LEN 19 //逻辑地图大小 (逻辑地图由行. ...

  10. 数据返回(数据共享,即从后端返回到前端调用,四种(requesst、ModelAndView、Model、Map))

    @Controller @RequestMapping("/view")//请求父路径 public class GoodsController { @RequestMapping ...