fhq treap ------ luogu P3369 【模板】普通平衡树(Treap/SBT)
二次联通门 : LibreOJ #104. 普通平衡树
#include <cstdio>
#include <iostream>
#include <algorithm>
const int BUF = ;
char Buf[BUF], *buf = Buf; inline void read (int &now)
{
bool temp = false;
for (now = ; !isdigit (*buf); ++ buf)
if (*buf == '-') temp = true;
for (; isdigit (*buf); now = now * + *buf - '', ++ buf);
if (temp) now = -now;
} struct T_D
{
T_D *L, *R;
int key, r, s;
inline void Updata ()
{
s = + (L ? L->s : ) + (R ? R->s : );
}
}; #define Max 1231231
struct D
{
T_D *x, *y; D () {}
D (T_D *_x, T_D *_y) : x (_x), y (_y) {}
};
class Fhq_Treap
{
private : T_D poor[Max], *Ta, *Root; inline T_D *New (int _x)
{
T_D *now = ++ Ta;
now->r = rand (), now->key = _x;
now->s = , now->L = now->R = NULL;
return now;
} D Split (T_D *now, int k)
{
if (now == NULL) return D (NULL, NULL);
D res;
if ((now->L ? now->L->s : ) >= k)
{
res = Split (now->L, k);
now->L = res.y, now->Updata ();
res.y = now;
}
else
{
res = Split (now->R, k - (now->L ? now->L->s : ) - );
now->R = res.x, now->Updata ();
res.x = now;
}
return res;
} T_D *Merge (T_D *A, T_D *B)
{
if (A == NULL) return B;
if (B == NULL) return A;
if (A->r < B->r)
{
A->R = Merge (A->R, B);
A->Updata (); return A;
}
else
{
B->L = Merge (A, B->L);
B->Updata (); return B;
}
} int Get_rank (T_D *now, int k)
{
if (now == NULL) return ;
return k <= now->key ? Get_rank (now->L, k) : (Get_rank (now->R, k) + (now->L ? now->L->s : ) + );
} public : Fhq_Treap () { Ta = poor; }
inline int Get_rank (int k)
{
return Get_rank (Root, k) + ;
} int Find_kth (int k)
{
D x = Split (Root, k - );
D y = Split (x.y, );
T_D *res = y.x;
Root = Merge (Merge (x.x, res), y.y);
return res->key;
} void Insert (int key)
{
int k = Get_rank (Root, key);
D x = Split (Root, k);
T_D *now = New (key);
Root = Merge (Merge (x.x, now), x.y);
} void Delete (int key)
{
int k = Get_rank (Root, key);
D x = Split (Root, k);
D y = Split (x.y, );
Root = Merge (x.x, y.y);
} int Find_Pre (int key)
{
int k = Get_rank (Root, key);
D x = Split (Root, k - );
D y = Split (x.y, );
T_D *res = y.x;
Root = Merge (Merge (x.x, res), y.y);
return res->key;
} int Find_Suc (int key)
{
int k = Get_rank (Root, key + );
D x = Split (Root, k);
D y = Split (x.y, );
T_D *res = y.x;
Root = Merge (Merge (x.x, res), y.y);
return res->key;
}
}; Fhq_Treap Tree;
int Main ()
{
fread (buf, , BUF, stdin);
int N, M; register int i;
read (N); int x, type;
for (i = ; i <= N; ++ i)
{
read (type), read (x);
if (type == )
Tree.Insert (x);
else if (type == )
Tree.Delete (x);
else if (type == )
printf ("%d\n", Tree.Get_rank (x));
else if (type == )
printf ("%d\n", Tree.Find_kth (x));
else if (type == )
printf ("%d\n", Tree.Find_Pre (x));
else printf ("%d\n", Tree.Find_Suc (x));
}
return ;
} int ZlycerQan = Main ();
int main (int argc, char *argv[]) {;}
fhq treap ------ luogu P3369 【模板】普通平衡树(Treap/SBT)的更多相关文章
- luoguP3369[模板]普通平衡树(Treap/SBT) 题解
链接一下题目:luoguP3369[模板]普通平衡树(Treap/SBT) 平衡树解析 #include<iostream> #include<cstdlib> #includ ...
- 【模板】平衡树——Treap和Splay
二叉搜索树($BST$):一棵带权二叉树,满足左子树的权值均小于根节点的权值,右子树的权值均大于根节点的权值.且左右子树也分别是二叉搜索树.(如下) $BST$的作用:维护一个有序数列,支持插入$x$ ...
- [luogu P3369]【模板】普通平衡树(Treap/SBT)
[luogu P3369][模板]普通平衡树(Treap/SBT) 题目描述 您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作: 插入x数 删除x数(若有多个相同的数,因只删 ...
- 数组splay ------ luogu P3369 【模板】普通平衡树(Treap/SBT)
二次联通门 : luogu P3369 [模板]普通平衡树(Treap/SBT) #include <cstdio> #define Max 100005 #define Inline _ ...
- 替罪羊树 ------ luogu P3369 【模板】普通平衡树(Treap/SBT)
二次联通门 : luogu P3369 [模板]普通平衡树(Treap/SBT) 闲的没事,把各种平衡树都写写 比较比较... 下面是替罪羊树 #include <cstdio> #inc ...
- 红黑树 ------ luogu P3369 【模板】普通平衡树(Treap/SBT)
二次联通门 : luogu P3369 [模板]普通平衡树(Treap/SBT) 近几天闲来无事...就把各种平衡树都写了一下... 下面是红黑树(Red Black Tree) 喜闻乐见拿到了luo ...
- 平衡树Treap模板与原理
这次我们来讲一讲Treap(splay以后再更) 平衡树是一种排序二叉树(或二叉搜索树),所以排序二叉树可以迅速地判断两个值的大小,当然操作肯定不止那么多(不然我们还学什么). 而平衡树在排序二叉树的 ...
- 算法模板——平衡树Treap 2
实现功能:同平衡树Treap 1(BZOJ3224 / tyvj1728) 这次的模板有了不少的改进,显然更加美观了,几乎每个部分都有了不少简化,尤其是删除部分,这个参照了hzwer神犇的写法,在此鸣 ...
- 2021.12.06 平衡树——Treap
2021.12.06 平衡树--Treap https://www.luogu.com.cn/blog/HOJQVFNA/qian-xi-treap-ping-heng-shu 1.二叉搜索树 1.1 ...
随机推荐
- LOJ2026 JLOI/SHOI2016 成绩比较 组合、容斥
传送门 感觉自己越来越愚钝了qwq 先考虑从\(n-1\)个人里安排恰好\(k\)个人被碾压,然后再考虑如何分配分数,两者乘起来得到答案. 对于第一部分,可以考虑容斥:设\(f_i\)表示\(i\)个 ...
- CCF 2016-12-1 中间数
CCF 2016-12-1 中间数 题目 问题描述 在一个整数序列a1, a2, -, an中,如果存在某个数,大于它的整数数量等于小于它的整数数量,则称其为中间数.在一个序列中,可能存在多个下标不相 ...
- sql 作业创建
转载:https://jingyan.baidu.com/article/adc81513be3423f722bf7351.html
- 14-4 ADO.NET简介2
14-4-1Connection连接.关闭数据库 1.为了访问数据库,就要提供数据库连接类,在C#中,是通过Connection类来实现的.它有 四种类型的连接方式: SQLConnection AD ...
- css3可拖动的魔方3d
css3可拖动的魔方3d 主要用到知识点: css3 3d转换 原生js鼠标拖动事件 display:grid 布局 实现的功能 3d魔方 可点击,可拖动 直接看效果 html: <div cl ...
- CSS是什么
css是层叠样式表(英文全称:Cascading Style Sheets)是一种用来表现HTML(标准通用标记语言的一个应用)或XML(标准通用标记语言的一个子集)等文件样式的计算机语言. CSS不 ...
- ECSHOP(3.0.0升级3.6.0)帮助教程
说明: 本文档只针对于未做过二开的ECSHOP3.0 用户 1.准备材料 先确保正在使用的ECShop系统版本为ecshop3.0.0并且代码没有经过二次开发,然后下载最新的ECShop3.6.0安装 ...
- python 提取目录中特定类型的文件
python使用‘os’和‘re’模块提取目录中特定类型的文件,这两个模都是安装python自带的,所以不需要安装. 思路: 使用os库lilstdir获取文件夹中的所有文件名,然后带上文件夹路径组合 ...
- [堆栈]Linux 中的各种栈:进程栈 线程栈 内核栈 中断栈
转自:https://blog.csdn.net/yangkuanqaz85988/article/details/52403726 问题1:不同线程/进程拥有着不同的栈,那系统所有的中断用的是同一个 ...
- k8s安装之grafana.yaml
这个作展示,够用. 为了使用nginx统一管理, 这里将grafana放在子目录下. - name: GF_SERVER_ROOT_URL value: "%(protocol)s://% ...