#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. 配置ubuntu的超管账号密码

    1 在搭建ubuntu 之后 只有自己的账号 2 root 用户是没有配置好的 需要重新配置 3 passwd root 4 输入新密码即可

  2. Windows上为Apache配置HTTPS

    Windows上为Apache配置HTTPS   转 https://www.cnblogs.com/tianzijiaozi/p/7582671.html   1. 安装OpenSSL: Windo ...

  3. Android 控件

    1. WebView <uses-permission android:name="android.permission.INTERNET" /> WebView wv ...

  4. Android第四次作业

    一.团队成员 成员1:刘宇莹 学号:1600802122 班级:计算机164 博客链接:刘宇莹 成员2:孟鑫菲 学号:1600802092 班级:计算机163 博客链接:孟鑫菲 二.团队项目apk 拍 ...

  5. I Think I Need a Houseboat POJ - 1005

    I Think I Need a Houseboat POJ - 1005 解题思路:水题 #include <iostream> #include <cstdio> #inc ...

  6. Dev_GridView自定义表格

    #region 自定义表格 //初始化测斜分析数据表 BandedGridView view = advBandedGridView1 as BandedGridView; view.BeginUpd ...

  7. shell - shift

    Shell编程中Shift的用法 位置参数可以用shift命令左移.比如 shift 3表示原来的$4现在变成$1,原来的$5现在变成$2等等,原来的$1.$2.$3丢弃,$0不移动.不带参数的shi ...

  8. swust oj 981

    统计利用二叉树存储的森林中树的棵数 1000(ms) 10000(kb) 2919 / 5436 普通树及其构成的森林均可转换成相应的二叉树,反之亦然.故而可以根据相应的转换方法去统计某一二叉树对应的 ...

  9. js文档就绪函数

    $(function(){ //执行的内容 }): $().ready(function(){ //执行的内容 }); $(document).ready(function(){ //执行的内容 }) ...

  10. 换目标啦,初识PHP

    一.初识PHP脚步程序 1.PHP开始标记 <?php 2.PHP结束标记 ?> <?php?> 3.我们的页面最终是通过html,css,js来展示出一个炫丽的界面 4.PH ...