注意确保操作合法性,否则可能陷入死循环

以点权作为排序依据

struct Splay{
#define ls p[u].son[0]
#define rs p[u].son[1]
#define maxn 100000 int root, cnt;
struct Node{
int val, fa, size, sum;
int son[2];
}p[maxn]; inline void destroy(int u){
p[u].val = ls = rs = p[u].fa = p[u].sum = p[u].size = 0;
} inline int identify(int u){
return p[p[u].fa].son[1] == u;
} inline void update(int u){
if(u) p[u].sum = p[ls].sum + p[rs].sum + p[u].size;
} void rotate(int u){
int f = p[u].fa, gf = p[f].fa, sta = identify(u), sta_f = identify(f);
p[f].son[sta] = p[u].son[sta ^ 1];
p[p[f].son[sta]].fa = f;
p[u].son[sta^1] = f, p[f].fa = u, p[u].fa = gf;
p[gf].son[sta_f] = u;
update(f);
} void splay(int u, int goal){
for(int f; (f = p[u].fa) && (f != goal); rotate(u))
if(p[f].fa != goal) rotate(identify(u) == identify(f) ? f : u);
if(!goal) root = u;
update(u);
} void insert(int u){ // 函数结束后权值为u的节点变为根节点
if(!root){
p[++cnt].val = u;
p[cnt].size = p[cnt].sum = 1;
root = cnt;
return ;
}
int now = root, f = 0;
while(true){
if(u == p[now].val){
++p[now].size;
splay(now, 0);
return ;
}
f = now, now = p[now].son[p[now].val < u];
if(!now){
p[++cnt].val = u;
p[cnt].size = p[cnt].sum = 1;
p[cnt].fa = f, p[f].son[p[f].val < u] = cnt;
++p[f].sum;
splay(cnt, 0);
return ;
}
}
} int find_val(int rank){
int now = root;
while(true){
if(p[now].son[0] && rank <= p[p[now].son[0]].sum)
now = p[now].son[0];
else{
int temp = p[p[now].son[0]].sum + p[now].size;
if(rank <= temp) return p[now].val;
now = p[now].son[1], rank -= temp;
}
}
} int find_rank(int u){ // 函数结束后权值为u的节点是根节点
int now = root, rank = 0;
while(true){
if(u < p[now].val) now = p[now].son[0];
else{
rank += p[p[now].son[0]].sum;
if(u == p[now].val){
splay(now, 0);
return rank + 1;
}
rank += p[now].size, now = p[now].son[1];
}
}
} int find_pre(int x){ // 返回x前驱节点编号
insert(x);
int now = p[root].son[0];
while(p[now].son[1]) now = p[now].son[1];
delete_val(x);
return now;
} int find_suffix(int x){ // 返回x后继节点编号
insert(x);
int now = p[root].son[1];
while(p[now].son[0]) now = p[now].son[0];
delete_val(x);
return now;
} void delete_val(int u){
find_rank(u); // 将权值为u的节点旋转到根节点
if(p[root].size > 1){
--p[root].size, --p[root].sum;
return ;
}
if(!p[root].son[0] && !p[root].son[1]){
destroy(root), root = 0;
return ;
}
int old_root = root;
if(!p[root].son[0]){
root = p[root].son[1];
p[root].fa = 0;
destroy(old_root);
return ;
}
if(!p[root].son[1]){
root = p[root].son[0];
p[root].fa = 0;
destroy(old_root);
return ;
}
int left_max = find_pre(u);
splay(left_max, 0);
p[root].son[1] = p[old_root].son[1];
p[p[old_root].son[1]].fa = root;
destroy(old_root);
update(root);
}
}splay;

以序列下标作为排序依据,常用于需要增删序列的操作(区间平衡树)

struct Splay{
#define ls p[u].son[0]
#define rs p[u].son[1]
#define maxn N
static const int inf = 1e9; // 因为有虚点直接初始化了
int root = 1, top = 0, temp = 5e5 + 45;
int id[N], c[N], cnt[N]; struct Node{
int fa, size, len, reset, sum; // size是子树大小,len是除去虚点的子树大小
int tag, val, l_max, r_max, mid_max;
int son[2];
Node() { reset = inf; }
}p[maxn]; inline int identify(int u){
return p[p[u].fa].son[1] == u;
} // 空间回收
void destroy(int u){
if(!u) return ;
if(ls) destroy(ls);
if(rs) destroy(rs);
p[u] = p[temp];
id[++top] = u;
} inline void update(int u){
p[u].size = p[ls].size + p[rs].size + 1;
p[u].len = p[ls].len + p[rs].len + (u > 2); // 判断u > 2是为了除去虚点的影响
p[u].sum = p[ls].sum + p[rs].sum + p[u].val;
p[u].l_max = max(p[ls].l_max, p[ls].sum + p[u].val + p[rs].l_max);
p[u].r_max = max(p[rs].r_max, p[rs].sum + p[u].val + p[ls].r_max);
p[u].mid_max = max(p[u].val + p[ls].r_max + p[rs].l_max, max(p[ls].mid_max, p[rs].mid_max));
} void change(int u, int val){
p[u].val = p[u].reset = val;
p[u].sum = p[u].val * p[u].len;
p[u].l_max = p[u].r_max = max(0, p[u].sum);
p[u].mid_max = max(val, p[u].sum);
} inline void pushdown(int u){
if(p[u].reset != inf){
if(ls) change(ls, p[u].reset);
if(rs) change(rs, p[u].reset);
p[u].reset = inf, p[u].tag = 0;
}
if(p[u].tag){
if(ls) p[ls].tag ^= 1, swap(p[ls].son[0], p[ls].son[1]), swap(p[ls].l_max, p[ls].r_max);
if(rs) p[rs].tag ^= 1, swap(p[rs].son[0], p[rs].son[1]), swap(p[rs].l_max, p[rs].r_max);
p[u].tag = 0;
}
} void rotate(int u){
int f = p[u].fa, gf = p[f].fa, sta = identify(u), sta_f = identify(f);
p[f].son[sta] = p[u].son[sta ^ 1];
p[p[f].son[sta]].fa = f;
p[u].son[sta^1] = f, p[f].fa = u, p[u].fa = gf;
p[gf].son[sta_f] = u;
update(f);
} void splay(int u, int goal){
for(int f; (f = p[u].fa) && (f != goal); rotate(u)){
if(p[f].fa != goal) rotate(identify(u) == identify(f) ? f : u);
}
if(!goal) root = u;
update(u);
} int find_Kth(int k){
int u = root;
while(1){
pushdown(u);
if(p[ls].size + 1 == k) return u;
if(p[ls].size >= k) u = ls;
else k -= p[ls].size + 1, u = rs;
}
} int build(int l, int r, int fa){
if(l > r) return 0;
int mid = (l + r) >> 1, now = cnt[mid];
if(l == r){
p[now].val = c[l];
p[now].fa = fa;
update(now);
p[now].mid_max = p[now].val;
return now;
}
p[now].fa = fa, p[now].val = c[mid];
p[now].son[0] = build(l, mid - 1, now);
p[now].son[1] = build(mid + 1, r, now);
update(now);
return now;
} // 在第u个位置后面插入值插入tot个数
void insert(int u, int tot){
for(int i = 1; i <= tot; ++i) scanf("%d", &c[i]), cnt[i] = id[top--];
int rt = build(1, tot, 0);
int L = find_Kth(u), R = find_Kth(u + 1);
splay(L, 0), splay(R, L);
p[rt].fa = R, p[R].son[0] = rt;
splay(rt, 0);
} // 区间删除
void delete_range(int pos, int tot){
int L = find_Kth(pos), R = find_Kth(pos + tot + 1);
splay(L, 0), splay(R, L);
destroy(p[R].son[0]);
p[R].son[0] = 0;
update(R), update(L);
} // 区间修改
void modify_range(int pos, int tot, int set){
int L = find_Kth(pos), R = find_Kth(pos + tot + 1);
splay(L, 0), splay(R, L);
int u = p[R].son[0];
p[u].reset = p[u].val = set;
p[u].sum = p[u].len * set;
p[u].l_max = p[u].r_max = max(0, p[u].sum);
p[u].mid_max = max(p[u].sum, set);
update(R), update(L);
} // 翻转区间
void reverse(int pos, int tot){
int L = find_Kth(pos), R = find_Kth(pos + tot + 1);
splay(L, 0), splay(R, L);
int u = p[R].son[0];
p[u].tag ^= 1;
swap(ls, rs);
swap(p[u].l_max, p[u].r_max);
update(R), update(L);
} int get_sum(int pos, int tot){
int L = find_Kth(pos), R = find_Kth(pos + tot + 1);
splay(L, 0), splay(R, L);
return p[p[R].son[0]].sum;
} // 非空子段最大值
int Max(){
return p[root].mid_max;
} // 插入虚点,之后操作要注意下标
void init(){
for(int i = 3; i <= N - 45; ++i) id[++top] = i;
p[1].son[1] = 2, p[2].fa = 1;
p[1].size = 2, p[2].size = 1;
// 虚点赋值负无穷,消除影响,根据不同题而定
p[0].mid_max = p[1].mid_max = p[2].mid_max = p[0].val = p[1].val = p[2].val = -inf;
}
}splay;

【封装】Splay的更多相关文章

  1. [学习笔记] Splay Tree 从入门到放弃

    前几天由于出行计划没有更博QwQ (其实是因为调试死活调不出来了TAT我好菜啊) 伸展树 伸展树(英语:Splay Tree)是一种二叉查找树,它能在O(log n)内完成插入.查找和删除操作.它是由 ...

  2. BZOJ 3196 Tyvj 1730 二逼平衡树:线段树套splay

    传送门 题意 给你一个长度为 $ n $ 有序数列 $ a $ ,进行 $ m $ 次操作,操作有如下几种: 查询 $ k $ 在区间 $ [l,r] $ 内的排名 查询区间 $ [l,r] $ 内排 ...

  3. AVL树、splay树(伸展树)和红黑树比较

    AVL树.splay树(伸展树)和红黑树比较 一.AVL树: 优点:查找.插入和删除,最坏复杂度均为O(logN).实现操作简单 如过是随机插入或者删除,其理论上可以得到O(logN)的复杂度,但是实 ...

  4. [C#] 简单的 Helper 封装 -- RegularExpressionHelper

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  5. iOS开发之App间账号共享与SDK封装

    上篇博客<iOS逆向工程之KeyChain与Snoop-it>中已经提到了,App间的数据共享可以使用KeyChian来实现.本篇博客就实战一下呢.开门见山,本篇博客会封装一个登录用的SD ...

  6. Ajax实现原理,代码封装

    都知道实现页面的异步操作需要使用Ajax,那么Ajax到是怎么实现异步操作的呢? 首先需要认识一个对象 --> XMLHttpRequest 对象 --> Ajax的核心.它有许多的属性和 ...

  7. 用C语言封装OC对象(耐心阅读,非常重要)

    用C语言封装OC对象(耐心阅读,非常重要) 本文的主要内容来自这里 前言 做iOS开发的朋友,对OC肯定非常了解,那么大家有没有想过OC中NSInteger,NSObject,NSString这些对象 ...

  8. 【知识必备】RxJava+Retrofit二次封装最佳结合体验,打造懒人封装框架~

    一.写在前面 相信各位看官对retrofit和rxjava已经耳熟能详了,最近一直在学习retrofit+rxjava的各种封装姿势,也结合自己的理解,一步一步的做起来. 骚年,如果你还没有掌握ret ...

  9. 对百度WebUploader开源上传控件的二次封装,精简前端代码(两句代码搞定上传)

    前言 首先声明一下,我这个是对WebUploader开源上传控件的二次封装,底层还是WebUploader实现的,只是为了更简洁的使用他而已. 下面先介绍一下WebUploader 简介: WebUp ...

  10. 封装集合(Encapsulate Collection)

    封装就是将相关的方法或者属性抽象成为一个对象. 封装的意义: 对外隐藏内部实现,接口不变,内部实现自由修改. 只返回需要的数据和方法. 提供一种方式防止数据被修改. 更好的代码复用. 当一个类的属性类 ...

随机推荐

  1. Dokcer学习之旅(1)——运行一个简单的容器

    基本概念 镜像 我们都知道,操作系统分为 内核 和 用户空间.对于 Linux 而言,内核启动后,会挂载 root 文件系统为其提供用户空间支持.而 Docker 镜像(Image),就相当于是一个 ...

  2. Blazor前后端框架Known-V1.2.12

    V1.2.12 Known是基于C#和Blazor开发的前后端分离快速开发框架,开箱即用,跨平台,一处代码,多处运行. Gitee: https://gitee.com/known/Known Git ...

  3. ETL之apache hop系列3-hop Server环境部署与客户端发布管道工作流

    前言 该文档主要是apache hop 2.5的 Windows 10和Linux docker环境部署和客户端发布工作流和管道的相关内容 不使用Docker直接使用应用程序包,下载压缩包文件后,需要 ...

  4. C#中的ConcurrentExclusiveSchedulerPair类

    为什么使用ConcurrentExclusiveSchedulerPair? 现实生活中的例子是一个停车场的入口和出口,多辆车可以同时进入和离开停车场,但是只有一个车辆可以进入或离开一次. 这时候就需 ...

  5. 白盒AES和SM4实现的差分故障分析

    DFA攻击背景介绍 传统的密码安全性分析环境被称为黑盒攻击环境,攻击者只能访问密码系统的输入与输出,但随着密码系统部署环境的多样化,该分析模型已经不能够反映实际应用中攻击者的能力.2002年,Chow ...

  6. PanGu-Coder2:从排序中学习,激发大模型潜力

    本文分享自华为云社区<PanGu-Coder2:从排序中学习,激发大模型潜力>,作者: 华为云软件分析Lab . 2022年7月,华为云PaaS技术创新Lab联合华为诺亚方舟语音语义实验室 ...

  7. HTML一键打包APK工具最新版1.9.2更新(附下载地址)

    HMTL网址打包APK,可以把本地HTML项目, Egret游戏,网页游戏,或者网站打包为一个安卓应用APK文件,无需编写任何代码,也无需配置安卓开发环境,支持在最新的安卓设备上安装运行. 打包软件会 ...

  8. 聊聊数据库事务内嵌TCP连接

    最近再看项目代码,发现很多的service里面,喜欢在事务内部再去调用HTTP请求,简单分析下此种方式的利弊与解决策略. 概述 在数据库内部嵌套TCP连接(一般是HTTP调用或是RPC远程调用). @ ...

  9. 一文弄懂TypeScript中的混合

    1.前言 由于TypeScrip中的类不支持多继承,所以引入了混合(Mixin)的特性,可以间接实现继承的效果. 2.正文 // 声明一个汽车类Vehicle,它有drive方法 class Vehi ...

  10. 「Semigroup と Monoid と Functional と」

    一个被国内 oi 环境弱化至几乎不存在的概念,不过我觉得还是有学一学的必要.因为我没学过代数结构所以大部分内容都在开黄腔,欲喷从轻. Semigroup 的定义是,\(\forall a,b\in\m ...