splay好板子
找到一份比较好的板子,链接https://blog.csdn.net/crazy_ac/article/details/8034190
#include<cstdio>
#include<cstdlib>
const int inf = ~0u>>;
#define L ch[x][0]
#define R ch[x][1]
#define KT (ch[ ch[rt][1] ][0])
const int maxn = ;
int lim;
struct SplayTree {
int sz[maxn];
int ch[maxn][];
int pre[maxn];
int rt,top;
inline void up(int x){
sz[x] = cnt[x] + sz[ L ] + sz[ R ];
}
inline void Rotate(int x,int f){
int y=pre[x];
ch[y][!f] = ch[x][f];
pre[ ch[x][f] ] = y;
pre[x] = pre[y];
if(pre[x]) ch[ pre[y] ][ ch[pre[y]][] == y ] =x;
ch[x][f] = y;
pre[y] = x;
up(y);
}
inline void Splay(int x,int goal){//将x旋转到goal的下面
while(pre[x] != goal){
if(pre[pre[x]] == goal) Rotate(x , ch[pre[x]][] == x);
else {
int y=pre[x],z=pre[y];
int f = (ch[z][]==y);
if(ch[y][f] == x) Rotate(x,!f),Rotate(x,f);
else Rotate(y,f),Rotate(x,f);
}
}
up(x);
if(goal==) rt=x;
}
inline void RTO(int k,int goal){//将第k位数旋转到goal的下面
int x=rt;
while(sz[ L ] != k-) {
if(k < sz[ L ]+) x=L;
else {
k-=(sz[ L ]+);
x = R;
}
}
Splay(x,goal);
}
inline void vist(int x){
if(x){
printf("结点%2d : 左儿子 %2d 右儿子 %2d val:%2d sz=%d cnt:%d\n",x,L,R,val[x],sz[x],cnt[x]);
vist(L);
vist(R);
}
}
void debug() {
puts("");
vist(rt);
puts("");
}
inline void Newnode(int &x,int c,int f){
x=++top;
L = R = ;
pre[x] = f;
sz[x]=; cnt[x]=;
val[x] = c;
}
inline void init(){
ch[][]=ch[][]=pre[]=sz[]=;
rt=top=; cnt[]=;
}
inline void Insert(int &x,int key,int f){
if(!x) {
Newnode(x,key,f);
Splay(x,);//注意插入完成后splay
return ;
}
if(key==val[x]){
cnt[x]++;
sz[x]++;
Splay(x,);//注意插入完成后splay
return ;
}else if(key<val[x]) {
Insert(L,key,x);
} else {
Insert(R,key,x);
}
up(x);
}
void Del_root(){//删除根节点
int t=rt;
if(ch[rt][]) {
rt=ch[rt][];
RTO(,);
ch[rt][]=ch[t][];
if(ch[rt][]) pre[ch[rt][]]=rt;
}
else rt=ch[rt][];
pre[rt]=;
up(rt);
}
void findpre(int x,int key,int &ans){//找前驱节点
if(!x) return ;
if(val[x] <= key){
ans=x;
findpre(R,key,ans);
} else
findpre(L,key,ans);
}
void findsucc(int x,int key,int &ans){//找后继节点
if(!x) return ;
if(val[x]>=key) {
ans=x;
findsucc(L,key,ans);
} else
findsucc(R,key,ans);
}
inline int find_kth(int x,int k){ //第k小的数
if(k<sz[L]+) {
return find_kth(L,k);
}else if(k > sz[ L ] + cnt[x] )
return find_kth(R,k-sz[L]-cnt[x]);
else{
Splay(x,);
return val[x];
}
}
int find(int x,int key){
if(!x) return ;
else if(key < val[x]) return find(L,key);
else if(key > val[x]) return find(R,key);
else return x;
}
int getmin(int x){
while(L) x=L; return val[x];
}
int getmax(int x){
while(R) x=R; return val[x];
}
//确定key的排名
int getrank(int x,int key,int cur){//cur:当前已知比要求元素(key)小的数的个数
if(key == val[x])
return sz[L] + cur + ;
else if(key < val[x])
getrank(L,key,cur);
else
getrank(R,key,cur+sz[L]+cnt[rt]);
}
int get_lt(int x,int key){//小于key的数的个数 lt:less than
if(!x) return ;
if(val[x]>=key) return get_lt(L,key);
return cnt[x]+sz[L]+get_lt(R,key);
}
int get_mt(int x,int key){//大于key的数的个数 mt:more than
if(!x) return ;
if(val[x]<=key) return get_mt(R,key) ;
return cnt[x]+sz[R]+get_mt(L,key);
}
void del(int &x,int f){//删除小于lim的所有的数所在的节点
if(!x) return ;
if(val[x]>=lim){
del(L,x);
} else {
x=R;
pre[x]=f;
if(f==) rt=x;
del(x,f);
}
if(x) up(x);
}
inline void update(){
del(rt,);
}
int get_mt(int key) {
return get_mt(rt,key);
}
int get_lt(int key) {
return get_lt(rt,key);
}
void insert(int key) {
Insert(rt,key,);
}
void Delete(int key) {
int node=find(rt,key);
Splay(node,);
cnt[rt]--;
if(!cnt[rt])Del_root();
}
int kth(int k) {
return find_kth(rt,k);
}
int cnt[maxn];
int val[maxn];
int lim;
}spt;
splay好板子的更多相关文章
- 个人整理的数组splay板子,指针的写的太丑了就不放了。。
splay的板子.. 由于被LCT榨干了..所以昨天去学了数组版的splay,现在整理一下板子.. 以BZOJ3224和3223为例题..暂时只有这些,序列的话等有时间把维修序列给弄上来!! BZOJ ...
- 在平衡树的海洋中畅游(三)——Splay
Preface 由于我怕学习了Splay之后不直接写blog第二天就忘了,所以强行加了一波优先级. 论谁是天下最秀平衡树,我Splay第一个不服.维护平衡只靠旋转. 一言不合转死你 由于平衡树我也介绍 ...
- 洛谷P3369 【模板】普通平衡树(Splay)
题面 传送门 题解 鉴于最近的码力实在是弱到了一个境界--回来重新打一下Splay的板子--竟然整整调了一个上午-- //minamoto #include<bits/stdc++.h> ...
- 浅谈算法——splay
BST(二叉查找树)是个有意思的东西,种类巨TM多,然后我们今天不讲其他的,我们今天就讲splay 首先,如果你不知道Splay是啥,你也得知道BST是啥 如上图就是一棵优美的BST,它对于每个点保证 ...
- 写在SDOI2016Round1前的To Do List
理性的整理了一下自己的不足. 计算几何啥都不会,字符串类DP毫无练习,数据结构写的不熟,数论推不出式子,网络流建模常建残: 需要达成的任务: 一.网络流: 熟练网络流的板子(之前一直仰慕zkw费用流, ...
- 枪战Maf[POI2008]
题目描述 有n个人,每个人手里有一把手枪.一开始所有人都选定一个人瞄准(有可能瞄准自己).然后他们按某个顺序开枪,且任意时刻只有一个人开枪.因此,对于不同的开枪顺序,最后死的人也不同. 输入 输入n人 ...
- 洛谷P3369 普通平衡树
刚学平衡树,分别用了Splay和fhq-treap交了一遍. 这是Splay的板子,貌似比较短? Splay #include <iostream> #include <cstdio ...
- Link Cut Tree学习笔记
从这里开始 动态树问题和Link Cut Tree 一些定义 access操作 换根操作 link和cut操作 时间复杂度证明 Link Cut Tree维护链上信息 Link Cut Tree维护子 ...
- Luogu1801_黑匣子_KEY
题目传送门 借这道题练一下Treap和Splay的板子. code: #include <cstdio> #include <cstdlib> using namespace ...
随机推荐
- 函数和常用模块【day05】:迭代器(六)
本节内容 1.简书 2.可迭代对象 3.迭代器 4.rang方法 5.总结 一.简述 我们经常使用for循环去遍历一些序列数据,但是我们有的时间发现for循环的效率很低,而且很占用了大量的硬件资源,但 ...
- Python基础【day01】:python 2和3区别(四)
许多Python初学者都会问:我应该学习哪个版本的Python.对于这个问题,我的回答通常是“先选择一个最适合你的Python教程,教程中使用哪个版本的Python,你就用那个版本.等学得差不多了,再 ...
- centos7环境下安装mysql5.6-----解压安装包的方法
参考连接:https://blog.csdn.net/qq_17776287/article/details/53536761 linux下有很多服务,如何查看服务是否启动,以mysql为例子 使用命 ...
- 在tomcat集群环境下redis实现分布式锁
上篇介绍了redis在集群环境下如何解决session共享的问题.今天来讲一下如何解决分布式锁的问题 什么是分布式锁? 分布式锁就是在多个服务器中,都来争夺某一资源.这时候我们肯定需要一把锁是不是 , ...
- js 锚点定位【转】
锚点定义 <a name="firstAnchor">&nsbp;</a> a标签锚点使用 <a href="#firstAncho ...
- HDU - 5340 Three Palindromes(manacher算法)
http://acm.hdu.edu.cn/showproblem.php?pid=5340 题意 判断是否能将字符串S分成三段非空回文串 分析 manacher预处理出前缀和后缀回文的位置, 枚举第 ...
- Hibernate非主键一对多关联。
Unit表 id,code User表 id,ucode ...@ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name="ucode" ...
- C# ffmpeg工具将视频转为SWF格式
1.下载ffmpeg工具 using System; using System.Collections; using System.Configuration; using System.Data; ...
- 理解django框架中的MTV与MVC模式
1.Models:一个抽象层,用来构建和操作你的web应用中的数据,模型是你的数据的唯一的.权威的信息源.它包含你所储存数据的必要字段和行为.通常,每个模型对应数据库中唯一的一张表. from dja ...
- UE4的AI学习(1)——基本概念
AI学习当中,不学习行为树基本概念就不能明白具体实例中的操作意义,但是没有经过具体实例实验,又觉得基本概念抽象难以理解.建议先泛读(1)(2)后再对具体的细节进行死磕,能较深的理解行为树的具体概念.第 ...