无旋Treap【模板】P3369
2024-08-29 13:14:45
题目
详情见链接。
代码
#include<cstdio>
#include<iostream>
#define outd(x) printf("%d\n",x)
#define outld(x) printf("%lld\n", x)
#define ls(x) arr[x].child[0]
#define rs(x) arr[x].child[1]
inline int read_int()
{
char c;
int ret = , sgn = ;
do { c = getchar(); } while ((c < '' || c > '') && c != '-');
if (c == '-') sgn = -; else ret = c - '';
while ((c = getchar()) >= '' && c <= '') ret = ret * + (c - '');
return sgn * ret;
}
using namespace std; const int maxn = + ; struct node
{
int child[], size, value, key;
}arr[maxn];
int tot; //当前节点个数 inline void push_up(int index)
{
arr[index].size = arr[ls(index)].size + arr[rs(index)].size + ;
} void split(int root, int& x, int& y, int value) //x中的小于或等于value,y中的大于value
{
if (!root) { x = y = ; return; }
if (arr[root].value <= value) { x = root; split(rs(root), rs(x), y, value); }
else { y = root; split(ls(root), x, ls(y), value); }
push_up(root);
} void merge(int&root, int x, int y)
{
if (!x || !y) { root = x + y; return; } //其中一个为0,root等于另一个
if (arr[x].key < arr[y].key) { root = x; merge(rs(root), rs(x), y); }
else { root = y; merge(ls(root), x,ls(y)); }
push_up(root);
} inline void insert(int& root, int value)
{
int x = , y = , z = ++tot;
arr[z].value = value, arr[z].size = , arr[z].key = rand();
split(root, x, y, value);
merge(x, x, z);
merge(root, x, y);
} inline void erase(int& root, int value)
{
int x = , y = , z = ;
split(root, x, y, value);
split(x, x, z, value - ); //z中全是value
merge(z, ls(z), rs(z));
merge(x, x, z);
merge(root, x, y);
} inline int Kth_number(int root, int k)
{
while (arr[ls(root)].size + != k)
{
if (arr[ls(root)].size >= k) root = ls(root);
else { k -= (arr[ls(root)].size + ); root = rs(root); }
}
return arr[root].value;
} inline int Get_rank(int& root, int value)
{
int x = , y = ;
split(root, x, y, value - );
int res = arr[x].size + ;
merge(root, x, y);
return res;
} inline int Pre(int& root, int value)
{
int x = , y = ;
split(root, x, y, value - );
int res = Kth_number(x, arr[x].size);
merge(root, x, y); //merge回去
return res;
} inline int Suf(int& root, int value)
{
int x = , y = ;
split(root, x, y,value);
int res = Kth_number(y, );
merge(root, x, y);
return res;
} int n, op, root; int main()
{
srand();
n = read_int();
while (n--)
{
op = read_int();
if (op == ) insert(root, read_int());
if (op == ) erase(root, read_int());
if (op == ) outd(Get_rank(root, read_int()));
if (op == ) outd(Kth_number(root, read_int()));
if (op == ) outd(Pre(root, read_int()));
if (op == ) outd(Suf(root, read_int()));
} return ;
}
参考视频链接:https://www.bilibili.com/video/av46204315/?p=2
最新文章
- HDU 1528 贪心模拟/二分图
- python之路-Day11
- linuxMint下面的截图工具
- git 教程(4)--版本回退
- flex loaderInfo为null在creationComplete事件中
- Hbase0.98.4/Hadoop2.4.1整合小结【原创】
- UIImageView 动画 / UIImage 方向
- axure变量的使用
- SparkSQL On Yarn with Hive,操作和访问Hive表
- angular2 学习笔记 ( rxjs 流 )
- SpriteKit所有的类
- OpenCV-Python教程(10、直方图均衡化)
- C/C++宏定义中#与##区别 .
- solr学习-基础环境搭建(一)
- 【C#】C#获取文件夹下的所有文件
- FastAdmin 基本知识流程一栏
- <;Numerical Analysis>;(by Timothy Sauer) Notes
- python中的异常处理tryexcept
- Python自动化开发 - 模块与包
- 启动Tomcat 卡在 Initializing Spring FrameworkServlet &#39;SpringMVC&#39;