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 ...
随机推荐
- Charles4.2.8抓包(http+https)
Charles 和 Fiddler 一样都是http抓包工具. 之前用 Fiddler 抓个别 ios 手机 https 报文时总卡在哪里返不回任何数据,后来怀疑是 Fiddler 问题,就考虑使用 ...
- python中的可哈希与不可哈希
什么是可哈希(hashable)? 简要的说可哈希的数据类型,即不可变的数据结构(字符串str.元组tuple.对象集objects). 哈希有啥作用? 它是一个将大体量数据转化为很小数据的过程,甚至 ...
- 在KubeSphere中部署Kubeapps
1. 情况说明 使用一台VMWare Workstation虚拟机,4核8G内存,50G磁盘 已安装KubeSphere 2.1 版本,已经按照官方文档的入门必读,示例一创建好相应的账号信息等 Kub ...
- Spark 系列(十二)—— Spark SQL JOIN 操作
一. 数据准备 本文主要介绍 Spark SQL 的多表连接,需要预先准备测试数据.分别创建员工和部门的 Datafame,并注册为临时视图,代码如下: val spark = SparkSessio ...
- Java JDK1.8源码学习之路 1 Object
写在最前 对于一个合格的后端程序员来说,现行的流行框架早已经能胜任基本的企业开发,Springboot 任何的框架都把重复的工作更佳简单/优化的解决掉,但是完全陷入在这样的温水里面, 好比温水煮青蛙, ...
- C# vb .net实现灰度化特效滤镜
在.net中,如何简单快捷地实现Photoshop滤镜组中的灰度化呢?答案是调用SharpImage!专业图像特效滤镜和合成类库.下面开始演示关键代码,您也可以在文末下载全部源码: 设置授权 第一步: ...
- Thomas Brinkhoff 基于路网的移动对象生成器的使用[第二版]
Thomas Brinkhoff 基于路网的移动对象生成器的使用 Thomas Brinkhoff 基于路网的移动对象生成器的使用 相关操作的说明 相关文件的说明 运行 导入eclipse后运行时选择 ...
- D1-JavaScript
下面的代码,我想要打印出hey jack,结果却打印出hey rose,为什么? function greet(person) { if (person == {name: 'jack'}) { co ...
- JAVA基础之ServletContext应用
创建一个登陆的界面,并且统计次数! 导入jar包; 1. driver=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/java0603?u ...
- Referenced file contains errors (xml文件第一行小红叉错误)
转自:http://www.manongjc.com/article/30401.html 在eclipse中开发网页时,经常会遇到写xml文件时第一行无缘无故报错.在最左面的行数上面报出一个小红叉, ...