找到一份比较好的板子,链接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好板子的更多相关文章

  1. 个人整理的数组splay板子,指针的写的太丑了就不放了。。

    splay的板子.. 由于被LCT榨干了..所以昨天去学了数组版的splay,现在整理一下板子.. 以BZOJ3224和3223为例题..暂时只有这些,序列的话等有时间把维修序列给弄上来!! BZOJ ...

  2. 在平衡树的海洋中畅游(三)——Splay

    Preface 由于我怕学习了Splay之后不直接写blog第二天就忘了,所以强行加了一波优先级. 论谁是天下最秀平衡树,我Splay第一个不服.维护平衡只靠旋转. 一言不合转死你 由于平衡树我也介绍 ...

  3. 洛谷P3369 【模板】普通平衡树(Splay)

    题面 传送门 题解 鉴于最近的码力实在是弱到了一个境界--回来重新打一下Splay的板子--竟然整整调了一个上午-- //minamoto #include<bits/stdc++.h> ...

  4. 浅谈算法——splay

    BST(二叉查找树)是个有意思的东西,种类巨TM多,然后我们今天不讲其他的,我们今天就讲splay 首先,如果你不知道Splay是啥,你也得知道BST是啥 如上图就是一棵优美的BST,它对于每个点保证 ...

  5. 写在SDOI2016Round1前的To Do List

    理性的整理了一下自己的不足. 计算几何啥都不会,字符串类DP毫无练习,数据结构写的不熟,数论推不出式子,网络流建模常建残: 需要达成的任务: 一.网络流: 熟练网络流的板子(之前一直仰慕zkw费用流, ...

  6. 枪战Maf[POI2008]

    题目描述 有n个人,每个人手里有一把手枪.一开始所有人都选定一个人瞄准(有可能瞄准自己).然后他们按某个顺序开枪,且任意时刻只有一个人开枪.因此,对于不同的开枪顺序,最后死的人也不同. 输入 输入n人 ...

  7. 洛谷P3369 普通平衡树

    刚学平衡树,分别用了Splay和fhq-treap交了一遍. 这是Splay的板子,貌似比较短? Splay #include <iostream> #include <cstdio ...

  8. Link Cut Tree学习笔记

    从这里开始 动态树问题和Link Cut Tree 一些定义 access操作 换根操作 link和cut操作 时间复杂度证明 Link Cut Tree维护链上信息 Link Cut Tree维护子 ...

  9. Luogu1801_黑匣子_KEY

    题目传送门 借这道题练一下Treap和Splay的板子. code: #include <cstdio> #include <cstdlib> using namespace ...

随机推荐

  1. 整理备忘一波liunx命令(持续更新)

    # 分区挂载 查看当前目录下的文件大小 du --max-depth= -h # 文件操作 # 编辑操作 #liunx 字体设置 苹果方字体下载安装 # 网洛端口 netstat命令各个参数说明如下: ...

  2. JavaSE学习总结(五)——封装,继承,多态很简单

    java面向对象的三大特性是:封装.继承与多态,是面向对象编程的核心. 一.封装 简单说封装就是将同一类事物的特性与功能包装在一起,对外暴露调用的接口. 封装:封装也称信息隐藏,是指利用抽象数据类型把 ...

  3. python---自定义分页类

    # coding:utf8 # __author: Administrator # date: 2018/3/7 0007 # /usr/bin/env python import tornado.w ...

  4. __attribute__的一些相关属性

    __attribute__((format()))  这个format有3个参数. int my(NSString *str,NSString *str1,NSArray*str2,...) __at ...

  5. VUE2.0 饿了吗视频学习笔记(二):新版本添加路由和显示Header

    https://gitee.com/1981633/vue_study.git 源码下载地址,随笔记动态更新中 webpack.dev.conf.js中添加两段代码 'use strict' cons ...

  6. SNMP收集

    http://velep.com/archives/416.html     协议基本格式

  7. android ContentObserver内容观察者基本使用

    package com.example.observertest; import android.content.ContentResolver; import android.database.Co ...

  8. POJ 1061 青蛙的约会 (扩展欧几里得算法)

    题目链接 Description 两只青蛙在网上相识了,它们聊得很开心,于是觉得很有必要见一面.它们很高兴地发现它们住在同一条纬度线上,于是它们约定各自朝西跳,直到碰面为止.可是它们出发之前忘记了一件 ...

  9. 用C++的 new 代替 C 的 malloc 进行内存分配

    例子: (int*)malloc(100*sizeof(int)) 是先取得int类型的字节宽度,然后乘100计算后得到400,然后调用malloc,并将400传递给函数,分配400字节的内存空间,但 ...

  10. 『PyTorch』第五弹_深入理解autograd_上:Variable属性方法

    在PyTorch中计算图的特点可总结如下: autograd根据用户对variable的操作构建其计算图.对变量的操作抽象为Function. 对于那些不是任何函数(Function)的输出,由用户创 ...