Treap的几大基本操作:
- Kth(k):寻找第k小的数
- Rank(x):值x的“名次”,即比x小的节点个数+1
- pre(x),寻找比x小的数中最大的数
- next(x),寻找比x大的数中最小的数
Treap可以说是一个劣质版的set<int>,实质上是一个树堆,对于值来说是一颗排序二叉树,对于优先级来说是一个优先堆。
基本的作用就是寻找k小数,和k的,名次。
刘汝佳大神版的模板代码如下:
(参考于https://blog.csdn.net/ccsu_cat/article/details/81749042)
洛谷P3369
#include<cstdio>
#include<cstdlib>
#include<algorithm>
using namespace std;
const int inf=1e9;
struct Node *null;
struct Node
{
Node *ch[2];
int r; //随机优先级
int v; //值
int w; //多出来的w是记录相同的数的个数
int s; //名次,以当前节点为根节点的子树的节点个数
Node(int v):v(v){ch[0]=ch[1]=null;r=rand();s=w=1;}
bool operator<(const Node& rhs)const{
return r<rhs.r;
}
int cmp(i