最近真的是花了很大的力气来学习平衡树的内容,因为之前在弄偏序的时候提到了平衡树的内容,真的是开了一个大坑啊。
先说说treap吧,网上的资料很多,描述的也很详细,我就不细说了,这里简单的谈谈我的看法,treap就是在二叉树的基础上加了一个随机域来保证堆的性质
这样可以把树的高度维护到期望的logn级别,其他的就是一棵二叉排序树,并没有什么特别的。
我实现了指针版和数组版两种,但是说实话,在写题的时候还是数组版的实用,无论是调试还是时间和代码量,数组都要显得优越很多。下面附上数组版的代码实现
struct Treaps
{
const static int maxn = 1e6 + 3e5;
int L[maxn], R[maxn], v[maxn], p[maxn], A[maxn], C[maxn], tot;
void clear(){ A[0] = L[0] = R[0] = C[0] = 0; tot = 1; }
int Node(int V, int P){ L[tot] = R[tot] = 0; v[tot] = V; p[tot] = P; A[tot] = C[tot] = 1; return tot++; }
void Count(int x){ C[x] = A[x] + C[L[x]] + C[R[x]]; }
void rotate_right(int &x)
{
int y = L[x]; L[x] = R[y]; R[y] = x; C[y] = C[x]; Count(x); x = y;
}
void rotate_left(int &x)
{
int y = R[x]; R[x] &#