平衡树-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 ...
随机推荐
- The Triangle(DP-数塔问题)
题目链接:http://poj.org/problem?id=1163 7 3 8 8 1 0 2 7 4 4 4 5 2 6 5 (Figure 1) Figure 1 shows a number ...
- 利用NSE脚本检测域传送和证书透明度滥用
nslookup -type=NS <domain> <server> nmap -p 53 --script dns-zone-transfer --script-args ...
- Petrozavodsk Winter-2018. Jagiellonian U Contest
A. XOR 求出所有数的异或和$sum$,将所有数and上$sum$,然后求线性基,则选取$sum$的所有$1$对应的基最优. 时间复杂度$O(n\log x)$. #include<cstd ...
- Thinkphp3.2.3加载外部类并调用类里面的方法 获取token
例如:加载七牛上传类(thinkphp自带的) $qiniu = new \Think\Upload\Driver\Qiniu\QiniuStorage($setting['driverConfig' ...
- vue-cli模拟后台数据交互
作为一个前端入坑的妹子,在学习vue的道路上挣扎徘徊,由一开始的对vue一直蒙圈只知道双向数据绑定和一些'V-x'的指令,慢慢通过一个视频的学习渐渐入坑,对于我这个js基础不怎么好而且编程思维又不是很 ...
- 关于DTO的理解
转自大神loveis715博文:http://www.cnblogs.com/loveis715/p/4379656.html 在一个web服务的实现中,我们常常需要访问数据库,并将从数据库中取得的数 ...
- __x__(4)0905第二天__软件架构
软件架构 C/S 架构,客户端/服务器,用户通过客户端使用软件. 一般的应用软件都是 C/S 架构,如 QQ,360 等等. C 为 Client,用户电脑使用的软件. S 为 Server,服务器, ...
- ng7 设置http proxy
看文档 proxy.conf.json { "/api": { "target": "http://localhost:5000", &qu ...
- 壁虎书1 The Machine Learning Landscape
属性与特征: attribute: e.g., 'Mileage' feature: an attribute plus its value, e.g., 'Mileage = 15000' Note ...
- es7 async/await使用
先创建一个promise对象,里面执行一个异步函数 function fetchUser() { return new Promise((resolve, reject) => { fetch( ...