P3369 【模板】普通平衡树(splay)
就是不用treap
splay板子,好好背吧TAT
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define N 100005
int n,cnt,rt,ans,q1,q2;
struct data{int v,ch[],fa,siz;}a[];
#define lc a[o].ch[0]
#define rc a[o].ch[1]
void up(int o){a[o].siz=a[lc].siz+a[rc].siz+;}
void turn(int x,int &k){
int y=a[x].fa,z=a[y].fa;
int l=(a[y].ch[]==x),r=l^;
if(y==k) k=x;
else a[z].ch[a[z].ch[]==y]=x;
a[a[x].ch[r]].fa=y; a[x].fa=z; a[y].fa=x;
a[y].ch[l]=a[x].ch[r]; a[x].ch[r]=y;
up(y); up(x);
}
void splay(int x,int &k){
for(;x!=k;turn(x,k)){
int y=a[x].fa,z=a[y].fa;
if(y!=k) turn(((a[y].ch[]==x)^(a[z].ch[]==y))?x:y,k);
}
}
int find(int o,int k){//查找排名为k的点的位置
while(k!=a[lc].siz+){
if(a[lc].siz+>=k) o=lc;
else k=k-a[lc].siz-,o=rc;
}return o;
}
int torank(int o,int k){//排名查询
int re=,id=;
while(o){
if(a[o].v==k) id=o;
if(a[o].v>=k) o=lc;
else re+=a[lc].siz+,o=rc;
}
if(id) splay(id,rt);//保证复杂度
return re;
}
void ins(int k){//添加
int o=rt,Fa=;
while(o) Fa=o,o=a[o].ch[a[o].v<=k];
o=++cnt; a[o].v=k; a[o].fa=Fa;
if(rt) a[Fa].ch[a[Fa].v<=k]=o,splay(o,rt);//保证复杂度
else rt=o;
}
void del(int k){//删除
int o=rt,tmp;
while(a[o].v!=k) o=a[o].ch[a[o].v<=k];
splay(o,rt);
if(lc&&rc){//分类讨论
tmp=find(rt,a[lc].siz);
splay(tmp,lc); o=rt;
a[lc].ch[]=rc; a[rc].fa=lc;
rt=lc; up(rt); a[rt].fa=;
}else if(lc) a[lc].fa=,rt=lc;
else if(rc) a[rc].fa=,rt=rc;
else rt=;
}
void pre(int o,int k){//前驱
if(!o) return;
if(ans<a[o].v&&a[o].v<k) ans=a[o].v;
pre(a[o].ch[a[o].v<k],k);
}
void last(int o,int k){//后缀
if(!o) return;
if(k<a[o].v&&a[o].v<ans) ans=a[o].v;
last(a[o].ch[a[o].v<=k],k);
}
int main(){
scanf("%d",&n);
while(n--){
scanf("%d%d",&q1,&q2);
if(q1==) ins(q2);
else if(q1==) del(q2);
else if(q1==) printf("%d\n",torank(rt,q2)+);//记得排名+1
else if(q1==) printf("%d\n",a[find(rt,q2)].v);
else if(q1==) ans=-1e9,pre(rt,q2),printf("%d\n",ans);
else ans=1e9,last(rt,q2),printf("%d\n",ans);
}return ;
}
P3369 【模板】普通平衡树(splay)的更多相关文章
- luoguP3391[模板]文艺平衡树(Splay) 题解
链接一下题目:luoguP3391[模板]文艺平衡树(Splay) 平衡树解析 这里的Splay维护的显然不再是权值排序 现在按照的是序列中的编号排序(不过在这道题目里面就是权值诶...) 那么,继续 ...
- 【洛谷P3369】普通平衡树——Splay学习笔记(一)
二叉搜索树(二叉排序树) 概念:一棵树,若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值: 若它的右子树不空,则右子树上所有结点的值均大于它的根结点的值: 它的左.右子树也分别为二叉搜索树 ...
- 洛谷.3369.[模板]普通平衡树(Splay)
题目链接 第一次写(2017.11.7): #include<cstdio> #include<cctype> using namespace std; const int N ...
- 洛谷.3391.[模板]文艺平衡树(Splay)
题目链接 //注意建树 #include<cstdio> #include<algorithm> const int N=1e5+5; //using std::swap; i ...
- 【阶梯报告】洛谷P3391【模板】文艺平衡树 splay
[阶梯报告]洛谷P3391[模板]文艺平衡树 splay 题目链接在这里[链接](https://www.luogu.org/problemnew/show/P3391)最近在学习splay,终于做对 ...
- 【模板篇】splay(填坑)+模板题(普通平衡树)
划着划着水一不小心NOIP还考的凑合了… 所以退役的打算要稍微搁置一下了… 要准备准备省选了…. 但是自己已经啥也不会了… 所以只能重新拾起来… 从splay开始吧… splay我以前扔了个板子来着, ...
- 【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 ...
- luoguP3369[模板]普通平衡树(Treap/SBT) 题解
链接一下题目:luoguP3369[模板]普通平衡树(Treap/SBT) 平衡树解析 #include<iostream> #include<cstdlib> #includ ...
- 平衡树——splay 一
splay 一种平衡树,同时也是二叉排序树,与treap不同,它不需要维护堆的性质,它由Daniel Sleator和Robert Tarjan(没错,tarjan,又是他)创造,伸展树是一种自调整二 ...
随机推荐
- 【LeetCode每天一题】Combination Sum(组合和)
Given a set of candidate numbers (candidates) (without duplicates) and a target number (target), fin ...
- js 对象的_proto_属性 和函数的prototype属性分析
bill是 Employee类型的实例,_proto_指向Employee.prototype Employee.prototype有一个constructor属性,指向Employee函数自身 fu ...
- 30-Python3 正则表达式
30-Python3 正则表达式 ''' re.match函数 ''' import re print(re.match('www','www.runoob.com').span()) print(r ...
- H5缩放效果的问题和缓存问题
https://segmentfault.com/q/1010000000305316 http://blog.csdn.net/hudashi/article/details/50963585 四. ...
- electron 前端开发桌面应用
electron是由Github开发,是一个用Html.css.JavaScript来构建桌面应用程序的开源库,可以打包为Mac.Windows.Linux系统下的应用. 快速开始 接下来,让代码来发 ...
- System.Web.UI.Page的页面基类
服务器端的page类 所有我们编写的页面都继承自page类,可见page类是非常重要的,page类提供了哪些功能,直接决定了我们的页面类可以继承什么功能,或者说,直接决定了我们的页面类功能的强大与否! ...
- 利用Tensorflow实现逻辑回归模型
官方mnist代码: #下载Mnist数据集 import tensorflow.examples.tutorials.mnist.input_data mnist = input_data.read ...
- 用xshell 连接docker Linux服务器
用xshell 连接docker Linux服务器
- iOS UI基础-18.0 UIView
设置边框 UIView *bgView = [[UIView alloc] init]; bgView.backgroundColor = [UIColor whiteColor]; self.bgV ...
- Error #2148
参考:https://blog.csdn.net/watersevenmmfx/article/details/52980804 chrome flash 安全沙箱冲突 SecurityError: ...