平衡树-Splay
#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的更多相关文章
- hiho #1329 : 平衡树·Splay
#1329 : 平衡树·Splay 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Ho:小Hi,上一次你跟我讲了Treap,我也实现了.但是我遇到了一个关键的问题. ...
- 【BZOJ3224】Tyvj 1728 普通平衡树 Splay
Description 您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作:1. 插入x数2. 删除x数(若有多个相同的数,因只删除一个)3. 查询x数的排名(若有多个相同的数 ...
- BZOJ3224/洛谷P3391 - 普通平衡树(Splay)
BZOJ链接 洛谷链接 题意简述 模板题啦~ 代码 //普通平衡树(Splay) #include <cstdio> int const N=1e5+10; int rt,ndCnt; i ...
- Hihocoder 1329 平衡树·Splay(平衡树)
Hihocoder 1329 平衡树·Splay(平衡树) Description 小Ho:小Hi,上一次你跟我讲了Treap,我也实现了.但是我遇到了一个关键的问题. 小Hi:怎么了? 小Ho:小H ...
- 【阶梯报告】洛谷P3391【模板】文艺平衡树 splay
[阶梯报告]洛谷P3391[模板]文艺平衡树 splay 题目链接在这里[链接](https://www.luogu.org/problemnew/show/P3391)最近在学习splay,终于做对 ...
- luoguP3391[模板]文艺平衡树(Splay) 题解
链接一下题目:luoguP3391[模板]文艺平衡树(Splay) 平衡树解析 这里的Splay维护的显然不再是权值排序 现在按照的是序列中的编号排序(不过在这道题目里面就是权值诶...) 那么,继续 ...
- 平衡树——splay 三
前文链接: 平衡树--splay 一 - yi_fan0305 - 博客园 (cnblogs.com) 平衡树--splay 二 - yi_fan0305 - 博客园 (cnblogs.com) 再补 ...
- 平衡树——splay 二
上文传送门:平衡树--splay 一 - yi_fan0305 - 博客园 (cnblogs.com) OK,我们继续上文,来讲一些其他操作. 七.找排名为k的数 和treap的操作很像,都是通过比较 ...
- 平衡树——splay 一
splay 一种平衡树,同时也是二叉排序树,与treap不同,它不需要维护堆的性质,它由Daniel Sleator和Robert Tarjan(没错,tarjan,又是他)创造,伸展树是一种自调整二 ...
- BZOJ3223: Tyvj 1729 文艺平衡树 [splay]
3223: Tyvj 1729 文艺平衡树 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 3595 Solved: 2029[Submit][Sta ...
随机推荐
- Intellij IDEA导入eclipse项目配置jdk、tomcat到浏览器正常访问
转发自:博客园---Lindp(大佬写的甚好) 以下是转发的正文 intellij idea中文资料网上比较少,对于eclipse的项目如何导入intellij idea也没有完整的说明,本人在这里整 ...
- Faster数据库研习,一
什么是Faster Faster 是一个很屌的嵌入式KeyValue 数据库项目 我简单的把 微软官网怎么吹的给大家翻译一下: Faster:一个为状态管理而生的嵌入式并发KeyValue ...
- Docker操作笔记(三)数据管理
数据管理 一.数据卷 数据卷 是一个可供一个或多个容器使用的特殊目录,它绕过 UFS,可以提供很多有用的特性: 数据卷 可以在容器之间共享和重用 对 数据卷 的修改会立马生效 对 数据卷 的更新,不会 ...
- eclipse 中 导入git项目无法导入的问题
研发在git上打了一个分支,需要重新导入分支项目.此时发现与之前相同模式导入失败,不起作用. 解决: 需要在Git Repositories中对应项目下找到.project 文件并进行修改,修改项目名 ...
- Jupyter notbook& REVEAL.JS& nbconvert 使用jupyter notebook制作slides
使用Jupyter notebook作为slide主要有两个方面: 在运行notebook 的时候可以幻灯片播放 这样幻灯片就有了notebook可交互的功能,而notebook就有了幻灯片全屏容易分 ...
- SQLyog 最新版本12.5-64bit 完美破解,亲测可用!
声明:本文只是提供一个网络上找到的针对12.5版本的注册码使用方式做一个说明,不建议企业用户破解,有条件的还是希望大家购买原版.当然个人学习用的但又不想购买原版的,这里只是提供个途径,请勿用做商业用途 ...
- PHP 可以获取客户端哪些访问信息---来自网页转载
php是一种弱类型的程序语言,但是最web的 在程序语言中有系统全局函数: $_SERVER <?php echo "".$_SERVER['PHP_SELF'];#当前正在 ...
- 5m21d缓冲区溢出学习笔记
mysql链接字符串函数 concat(str1,str2) concat_ws(separator,str1,str2....) group_concat(str1,str2....) mysql的 ...
- SQL之NULL值的几种处理方式
1.创建测试表: drop table if exists tab_null_operator; create table tab_null_operator as select 1 as id,'c ...
- 洛谷P4778 Counting swaps 数论
正解:数论 解题报告: 传送门! 首先考虑最终的状态是固定的,所以可以知道初始状态的每个数要去哪个地方,就可以考虑给每个数$a$连一条边,指向一个数$b$,表示$a$最后要移至$b$所在的位置 显然每 ...