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 ...
随机推荐
- GOF 的23种JAVA常用设计模式 学习笔记 持续更新中。。。。
前言: 设计模式,前人总结下留给后人更好的设计程序,为我们的程序代码提供一种思想与认知,如何去更好的写出优雅的代码,23种设计模式,是时候需要掌握它了. 1.工厂模式 大白话:比如你需要一辆汽车,你无 ...
- sql 分组后重复数据取时间最新的一条记录
1.取时间最新的记录 不分组有重复(多条CreateTime一样的都是最新记录) select * from test t where pid in ( select PId from Test t ...
- iOS静态库相关-封装lib
来源:http://blog.csdn.net/zsomsom/article/details/9163635 Library介绍 基本知识 在实际的编程过程中,通常会把一些公用函数制成函数库,供其它 ...
- aapt&adb笔记
aapt 查看安装包信息aapt list apk路径* aapt list xxx/app-debug.apk 查看apk文件信息并保存到本地(> 重定向符) * aapt list xx/a ...
- 删除Ubuntu的UEFI启动项
bcdedit 删除 千万不要手贱用diskpart之类的命令直接删除文件夹,大写的没,有,用! 感谢这个视频的up主,youtube看不到请翻墙.https://www.youtube.com/wa ...
- SqlServer中-char varchar nvarchar的区别
说说nvarchar和varchar的区别:的区别: varchar: 可变长度,存储ANSI字符,根据数据长度自动变化. nvarchar: 可变长度,存储Unicode字符,根据数据长度自动变化 ...
- c# ArrayList 类
- centos6/7添加系统服务
服务脚本必须存放在/etc/ini.d/目录下: chkconfig --add mongodb chkconfig --list mongodb 等级0表示:表示关机 ...
- 移动魔百和PTV-8098可以免拆直接安装第三方的软件
前言:江苏用户的福音啊!当地移动魔百和PTV-8098可以免拆直接安装第三方的软件,其他地区的PTV-8098未做测试,可以自己根据教程步骤进行测试: 准备工具:8G或4G的U盘(太大无法识别).电脑 ...
- excel将一个工作表根据条件拆分成多个sheet工作表与合并多个sheet工作表
本例介绍在excel中如何将一个工作表根据条件拆分成多个工作表. 注意:很多朋友反映sheets(i).delete这句代码出错,要注意下面第一个步骤,要拆分的数据工作表名称为“数据源”, 而不是你新 ...