#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#define inf (int)(1e9+1000)
#define maxn (int)(1e5+1000)
using namespace std;
int fa[maxn],size[maxn],cnt[maxn],son[maxn][2],val[maxn];
int n,root,beh,fro,idx;
void maintain(int x){
size[x]=cnt[x];
if(son[x][0])size[x]+=size[son[x][0]];
if(son[x][1])size[x]+=size[son[x][1]];
return;
}
void clear(int x){
int y=fa[x];fa[x]=0;
if(!x)return;//test
if(son[y][0]==x)son[y][0]=0;
else son[y][1]=0;
return;
}
void rotate(int x){
int y=fa[x],z=fa[y],o=(son[y][1]==x); son[y][o]=son[x][o^1];
fa[son[x][o^1]]=y; son[x][o^1]=y;
fa[y]=x; son[z][son[z][1]==y]=x;
fa[x]=z; maintain(y);
maintain(x);
}
void splay(int x){
for(int y;(y=fa[x]);rotate(x)){
if(!fa[y])continue;
rotate(((son[fa[y]][0]==y)==(son[fa[x]][0]==x))?y:x);
}
root=x;
}
void insert(int x,int a){
int y=0;
while(x&&val[x]!=a){
y=x;
x=son[x][a>val[x]];
}
if(x){
size[x]++;cnt[x]++;
}
else{
x=++idx;
cnt[x]=1;
fa[x]=y;
son[y][a>val[y]]=x;
size[x]=1;
val[x]=a;
}
splay(x);
}
void del(int x){
splay(x);
if(cnt[x]>1){cnt[x]--;maintain(x);splay(x);return;}
root=son[x][1];int move=son[x][0];
clear(son[x][0]);clear(son[x][1]);
int y=0,now=root;
while(now){
y=now;size[y]+=size[move];
now=son[now][0];
}
fa[move]=y;son[y][0]=move;
splay(move);
return;
}
void pre(int x,int a){
if(!x)return;
while(x){
if(val[x]<a){
fro=x;
x=son[x][1];
}else{
x=son[x][0];
}
}
return;
}
void suc(int x,int a){
if(!x)return;
while(x){
if(val[x]>a){
beh=x;
x=son[x][0];
}
else{
x=son[x][1];
}
}
return;
}
int kth(int x,int a){
while(1){
if(a<=size[son[x][0]]){//test
x=son[x][0];
}
else{
a-=size[son[x][0]]+cnt[x];
if(a<=0)return x;
x=son[x][1];
}
}
return 0;
}
int rk(int x,int a){
int rank=0;
while(1){
if(a<val[x]){
x=son[x][0];
}
else{
rank+=size[son[x][0]];
if(a==val[x]){
splay(x);
return rank+1;
}
rank+=cnt[x];
x=son[x][1];
}
}
return rank;
}
int get(int a){
int x=root;
while(1){
if(val[x]==a)return x;
x=son[x][a>val[x]];
}
return 0;
}
int main(){
scanf("%d",&n);
insert(root,inf);insert(root,-inf);
for(int i=1;i<=n;i++){
int opt,x;scanf("%d%d",&opt,&x);
if(opt==1)insert(root,x);
else if(opt==2)del(get(x));
else if(opt==3)printf("%d\n",rk(root,x)-1);
else if(opt==4)printf("%d\n",val[kth(root,x+1)]);
else if(opt==5){pre(root,x);printf("%d\n",val[fro]);}
else if(opt==6){suc(root,x);printf("%d\n",val[beh]);}
}
}

平衡树-Splay的更多相关文章

  1. hiho #1329 : 平衡树·Splay

    #1329 : 平衡树·Splay 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Ho:小Hi,上一次你跟我讲了Treap,我也实现了.但是我遇到了一个关键的问题. ...

  2. 【BZOJ3224】Tyvj 1728 普通平衡树 Splay

    Description 您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作:1. 插入x数2. 删除x数(若有多个相同的数,因只删除一个)3. 查询x数的排名(若有多个相同的数 ...

  3. BZOJ3224/洛谷P3391 - 普通平衡树(Splay)

    BZOJ链接 洛谷链接 题意简述 模板题啦~ 代码 //普通平衡树(Splay) #include <cstdio> int const N=1e5+10; int rt,ndCnt; i ...

  4. Hihocoder 1329 平衡树·Splay(平衡树)

    Hihocoder 1329 平衡树·Splay(平衡树) Description 小Ho:小Hi,上一次你跟我讲了Treap,我也实现了.但是我遇到了一个关键的问题. 小Hi:怎么了? 小Ho:小H ...

  5. 【阶梯报告】洛谷P3391【模板】文艺平衡树 splay

    [阶梯报告]洛谷P3391[模板]文艺平衡树 splay 题目链接在这里[链接](https://www.luogu.org/problemnew/show/P3391)最近在学习splay,终于做对 ...

  6. luoguP3391[模板]文艺平衡树(Splay) 题解

    链接一下题目:luoguP3391[模板]文艺平衡树(Splay) 平衡树解析 这里的Splay维护的显然不再是权值排序 现在按照的是序列中的编号排序(不过在这道题目里面就是权值诶...) 那么,继续 ...

  7. 平衡树——splay 三

    前文链接: 平衡树--splay 一 - yi_fan0305 - 博客园 (cnblogs.com) 平衡树--splay 二 - yi_fan0305 - 博客园 (cnblogs.com) 再补 ...

  8. 平衡树——splay 二

    上文传送门:平衡树--splay 一 - yi_fan0305 - 博客园 (cnblogs.com) OK,我们继续上文,来讲一些其他操作. 七.找排名为k的数 和treap的操作很像,都是通过比较 ...

  9. 平衡树——splay 一

    splay 一种平衡树,同时也是二叉排序树,与treap不同,它不需要维护堆的性质,它由Daniel Sleator和Robert Tarjan(没错,tarjan,又是他)创造,伸展树是一种自调整二 ...

  10. BZOJ3223: Tyvj 1729 文艺平衡树 [splay]

    3223: Tyvj 1729 文艺平衡树 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 3595  Solved: 2029[Submit][Sta ...

随机推荐

  1. RPC远程调用——Dubbo

    1.安装Zookeeper a.下载Zookeeper后解压 b.进入根目录下的conf文件夹,将zoo_sample.cfg改成bak文件,并复制一个修改为zoo.cfg,修改相关配置 # The ...

  2. jmeter基本组成原件介绍

    jmeter基本组成原件介绍 参考地址:https://wenku.baidu.com/view/d4986ca2aaea998fcc220ec1.html 从性能工具的原理划分: Jmeter工具和 ...

  3. JS _函数作用域及变量提升

    虽然看了多次js函数作用域及变量提升的理论知识,但也是一知半解~ 这几天做了几道js小题,对这部分进行了从新的理解,还是有所收获的~ 主要参考书籍: <你不知道的JavaScript(上卷)&g ...

  4. 【循环数组的最大字串和】Maximal-sum Subsequence

    [循环数组的最大字串和]Maximal-sum Subsequence PROBLEM 题目描述 给一个 N×N 的矩阵 M,可以取连续的一段数(必须是横着或者竖着或者斜着,这个矩阵是循环的,具体如下 ...

  5. GMA Round 1 三角形

    传送门 三角形 在△ABC中已知$sin2A+sin2B+sin2C=\frac{3\sqrt{3}}{2}$,求$cos\frac{A}{2}*cos\frac{B}{2}*cos\frac{C}{ ...

  6. Educational Codeforces Round 7

    622A - Infinite Sequence    20171123 暴力枚举\(n\)在哪个区间即可,时间复杂度为\(O(\sqrt{n})\) #include<stdlib.h> ...

  7. 查看webdriver API

    pydoc是Python自带的模块,主要用于从python模块中自动生成文档 生成web页面文档: cmd->python -m pydoc -p 8888 -p:在本机上启动服务 8888:端 ...

  8. 外网访问VMware虚拟机

      目的: 主机上安装了VMware,VMware上安装了Linux虚拟机(我安装的是Centos7).我想让虚拟机向外提供Web服务.本文记录如何让我的主机和外网用户可以访问VM虚拟机上的Web. ...

  9. 输入URL地址到页面加载完成 过程

    在浏览器的地址栏中输入URL地址"http://www.gacl.cn:8080/JavaWebDemo1/1.jsp"去访问服务器上的1.jsp这个web资源的过程 1.浏览器根 ...

  10. Yocto和Android编译命令的简化和自动完成的实现

    简化编译命令 无论是在Android编译系统中,还是在Yocto编译系统中,要编译一个目标,输入命令都有点费事. Yocto系统: source setup-environment $FOLDER b ...