二次联通门 : 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)的更多相关文章

  1. luoguP3369[模板]普通平衡树(Treap/SBT) 题解

    链接一下题目:luoguP3369[模板]普通平衡树(Treap/SBT) 平衡树解析 #include<iostream> #include<cstdlib> #includ ...

  2. 【模板】平衡树——Treap和Splay

    二叉搜索树($BST$):一棵带权二叉树,满足左子树的权值均小于根节点的权值,右子树的权值均大于根节点的权值.且左右子树也分别是二叉搜索树.(如下) $BST$的作用:维护一个有序数列,支持插入$x$ ...

  3. [luogu P3369]【模板】普通平衡树(Treap/SBT)

    [luogu P3369][模板]普通平衡树(Treap/SBT) 题目描述 您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作: 插入x数 删除x数(若有多个相同的数,因只删 ...

  4. 数组splay ------ luogu P3369 【模板】普通平衡树(Treap/SBT)

    二次联通门 : luogu P3369 [模板]普通平衡树(Treap/SBT) #include <cstdio> #define Max 100005 #define Inline _ ...

  5. 替罪羊树 ------ luogu P3369 【模板】普通平衡树(Treap/SBT)

    二次联通门 : luogu P3369 [模板]普通平衡树(Treap/SBT) 闲的没事,把各种平衡树都写写 比较比较... 下面是替罪羊树 #include <cstdio> #inc ...

  6. 红黑树 ------ luogu P3369 【模板】普通平衡树(Treap/SBT)

    二次联通门 : luogu P3369 [模板]普通平衡树(Treap/SBT) 近几天闲来无事...就把各种平衡树都写了一下... 下面是红黑树(Red Black Tree) 喜闻乐见拿到了luo ...

  7. 平衡树Treap模板与原理

    这次我们来讲一讲Treap(splay以后再更) 平衡树是一种排序二叉树(或二叉搜索树),所以排序二叉树可以迅速地判断两个值的大小,当然操作肯定不止那么多(不然我们还学什么). 而平衡树在排序二叉树的 ...

  8. 算法模板——平衡树Treap 2

    实现功能:同平衡树Treap 1(BZOJ3224 / tyvj1728) 这次的模板有了不少的改进,显然更加美观了,几乎每个部分都有了不少简化,尤其是删除部分,这个参照了hzwer神犇的写法,在此鸣 ...

  9. 2021.12.06 平衡树——Treap

    2021.12.06 平衡树--Treap https://www.luogu.com.cn/blog/HOJQVFNA/qian-xi-treap-ping-heng-shu 1.二叉搜索树 1.1 ...

随机推荐

  1. css之多行居中

    需求: 单行到多行文字居中. <div> <p>应该为数组中的每个子代分配一个唯一的键.表格dataSource和中的值columns应遵循此规则.默认情况下</p> ...

  2. Java冒泡排序与快速排序笔记

    public class Sort { public static void sort() { Scanner input = new Scanner(System.in); int sort[] = ...

  3. CORS讲解

    跨域资源共享(CORS) 是一种机制,它使用额外的 HTTP 头来告诉浏览器  让运行在一个 origin (domain) 上的Web应用被准许访问来自不同源服务器上的指定的资源.当一个资源从与该资 ...

  4. ①将SVN迁移到GitLab-单分支迁移

    将SVN上的代码迁移到GitLab上,实际原理是将所迁移的服务器上,拷贝SVN上的相关代码,在服务器上生成Git相关仓库,然后推送到GitLab仓库,并保存SVN相关的提交记录,分支,标签等信息. 一 ...

  5. Java中使用HttpPost上传文件以及HttpGet进行API请求(包含HttpPost上传文件)

    Java中使用HttpPost上传文件以及HttpGet进行API请求(包含HttpPost上传文件) 一.HttpPost上传文件 public static String getSuffix(fi ...

  6. UML回顾暨课程总结

    本文作为OO的最后一次博客作业,主要回顾了第四单元的架构设计和本学期的心路历程. 本单元架构设计 UML1 ​ 第一次作业的主要内容是解析mdj格式输入,记录特定数据并支持针对类.属性和方法等的查询功 ...

  7. android RecyclerView的Grid布局案例

    1.先创建activity_grid.xml 和 activity_grid_item.xml <?xml version="1.0" encoding="utf- ...

  8. mtd设备操作、jffs2

    安装mtd相关命令 手动安装mtd-utils,根据系统自行选择 mtd交叉编译:https://blog.csdn.net/zhangxuechao_/article/details/5212442 ...

  9. [C#(WinForm)]判断第一次启动程序

    来源:https://bbs.csdn.net/topics/220023353/(10楼) 在判断窗口上添加: string strIsFirstRun = "false"; b ...

  10. Nginx的proxy buffer参数总结

    1. proxy_buffering 语法:proxy_buffering on|off 默认值:proxy_buffering on 上下文:http,server,location 作用:该指令开 ...