#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. Round#534 div.2-C Grid game

    http://codeforces.com/contest/1104/problem/C 好厉害的题~ 只要把竖着的放在第一第二行,横着的放在第三/第四行就行. 哦吼,大半夜脑子迷糊地看英文的脑筋急转 ...

  2. SA:T1编写主函数法和T2Matlab自带的SA工具箱GUI法,两种方法实现对二元函数优化求解——Jason niu

    %SA:T1法利用Matlab编写主函数实现对定义域[-5,5]上的二元函数求最优解—Jason niu [x,y] = meshgrid(-5:0.1:5,-5:0.1:5); z = x.^2 + ...

  3. P1516 青蛙的约会

    P1516 青蛙的约会x+mt-p1L=y+nt-p2L(m-n)t+L(p2-p1)=y-x令p=p2-p1(m-n)t+Lp=y-x然后套扩欧就完事了 #include<iostream&g ...

  4. BZOJ.3566.[SHOI2014]概率充电器(概率DP 树形DP)

    BZOJ 洛谷 这里写的不错,虽然基本还是自己看转移... 每个点的贡献都是\(1\),所以直接求每个点通电的概率\(F_i\),答案就是\(\sum F_i\). 把\(F_x\)分成:父节点通电给 ...

  5. 新浪云SAE 关于部分函数不能使用的做法

    例如:file_put_contents("test.txt","Hello World. Testing!"); 可以这样写: file_put_conten ...

  6. angular.isString()

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  7. [LeetCode] Chalkboard XOR Game 黑板亦或游戏

    We are given non-negative integers nums[i] which are written on a chalkboard.  Alice and Bob take tu ...

  8. 变量类型-Dict

    教程:一:字典的创建        1:字典的介绍------>d = {key1:value1, key2:values2} (1)dictionary(字典) 是 Python 中最有用的数 ...

  9. keepalived + nginx 实现双机热备

    # docker run -itd --name centos_m1 centos # 进入容器 # docker exec -it centos_m1 /bin/bash # 安装nginx # r ...

  10. F#周报2019年第9期

    新闻 对于F#,Visual Studio 2019 RC有哪些更新 Visual Studio 2019 RC现在已经发布 C#版本与工具的升级 如何移植桌面应用程序到.NET Core 3.0 对 ...