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 ...
随机推荐
- DevExtreme学习笔记(一)treeView(搜索固定、节点展开和收缩)注意事项
var treeConfig1 = dxConfig.treeView(obj_Question.treeDataSource1); treeConfig1.selectionMode = 'sing ...
- ASP.NET Core 2.1 中的 HttpClientFactory (Part 3) 使用Handler实现传出请求中间件
原文:https://www.stevejgordon.co.uk/httpclientfactory-aspnetcore-outgoing-request-middleware-pipeline- ...
- 转 Json数据格式化
/// <summary> /// JSON字符串格式化 /// </summary> /// <param name="json"></ ...
- isolate 通信
main.dart import 'package:flutter/material.dart'; import 'package:flutter_isolate/flutter_isolate.da ...
- jvm调优相关
查找占用cpu过高的线程,并排查原因 1.查找jvm进程 (1)jps -l (2)ps -ef|grep java 这一步骤可以得到进程号,假如进程号为9527 2.查找该jvm进程中占用cpu比较 ...
- C++项目链接出错, error LNK2019: 无法解析的外部符号 __imp_xxxx_Allocate,该符号在函数 "xxxx" (xxxx) 中被引用
1 错误提示 error LNK2019: 无法解析的外部符号 __imp_FreeImage_Allocate,该符号在函数 "public: bool __cdecl colmap::B ...
- ToLua Timer机制
从一个Bug说起: 在内部试玩时发现有个任务的玩家跟随Npc逻辑挂了. telnet连接到出问题的设备上, 开始搞事情 这个跟随的逻辑是一个Timer驱动的. 这个Timer在主角创建时就会启动. 一 ...
- JavaScript中匿名函数this指向问题
this对象是在运行时基于函数执行环境绑定的,在全局函数中,this=window,在函数被作为某个对象的方法调用时,this等于这个对象. 但是匿名函数的执行环境是全局性的,所以匿名函数的this指 ...
- python实战项目
没有一个完整的项目开发过程,是不会对整个开发流程以及理论知识有牢固的认知的,对于怎样将所学的理论知识应用到实际开发中更是不得而知了! 以上就是我们在学习过程中必须要有项目实战开发经验的原因,其实无论项 ...
- unity4.3.4firedrillonline项目首次整合问题总结
零.资源导入后把所有资源模型拖到场景中去,并reset Transform,使场景展现原有样子. 一.资源导入之后发现项目场景是黑的,添加灯光之后场景中大部分仍然是黑的(并没有光照的效果) 可能原因: ...