笛卡尔树入门

· · 个人记录

笛卡尔树入门

part -1: 闲话

感觉这个东西用处不是很大的说

part 0: 前置知识

## part 1: 概念 一种二叉树,每个结点维护两个值 $(k, w)$,其中 $k$ 要满足 $BST$ 的性质,而 $w$ 要满足堆的性质(大/小根堆视题目不同而不同)。由定义可以知晓,$Treap$ 也是一种笛卡尔树,只不过它的 $w$ 值是随机赋的。 事实上,如果笛卡尔树每个结点的 $k$ 各不相同, $w$ 也各不相同,那么这棵笛卡尔树的形态是唯一确定的,如图: ![](https://pic.imgdb.cn/item/64fb131d661c6c8e54b97b24.png) (图源自维基百科) 上图将数组下标当作 $k$,数组元素值当作 $w$,对于这样的笛卡尔树,则显然有一棵子树内的 $k$ 也就是下标是一段连续的区间。 那么可以发现,笛卡尔树可以用来解决 $RMQ$ 问题,区间 $[l, r]$ 的最大/最小值即为点 $(l, w_l)$, $(r, w_r)$ 在笛卡尔树上 $LCA$ 的 $w$ 值。 ## part 2: 建树 ### 构建方式:栈构建 我们考虑将所有元素按 $k$ 值排好序后一个个插入到笛卡尔树当中。那么显然每次新插入的节点的位置都应该在当前笛卡尔树的右链(右链:即从根节点出发一直跳右儿子,所经过的点形成的链)的末端,以保证键值 $k$ 满足 $BST$ 的性质。 以小根堆笛卡尔树为例,我们就要执行以下过程: 假设我们要插入 $v$ 点,从原右链末端开始跳父亲,一直跳到第一个满足 $w_u < w_v$ 的 $u$ 点,让 $v$ 点成为 $u$ 点的右儿子,而让 $u$ 点原本的右子树成为 $v$ 点的左子树,易发现笛卡尔树的性质没有被破坏。 读者可以通过下图模拟一下过程: ![](https://pic.imgdb.cn/item/64fb19ce661c6c8e54ba7b43.png) (图源自 OI-Wiki) 显然每个节点只会进出右链一次,可以用单调栈来维护现在右链上的点,复杂度就是 $O(N)$ 的。 #### 建树Code [P5854 【模板】笛卡尔树](https://www.luogu.com.cn/problem/P5854) ```cpp #include <bits/stdc++.h> #define INF 0x7fffffff using namespace std; typedef long long LL; const int N = 1e7 + 10; LL read() { LL x = 0, f = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') x = (x << 3) + (x << 1) + (LL)(ch - '0'), ch = getchar(); return x * f; } void write(LL n) { if (n < 0) putchar('-'), n = -n; if (n >= 10) write(n / 10); putchar(n % 10 + '0'); } inline void writee(LL n) { write(n); puts(""); } inline void writes(LL n) { write(n); putchar(' '); } int n, a[N], ch[N][2]; int st[N], top = 1; LL ansl, ansr; int main() { n = read(); st[1] = 1; for (int i = 1; i <= n; ++i) a[i] = read(); for (int i = 2; i <= n; ++i) { while (a[st[top]] > a[i] && top) --top; if (!top) ch[i][0] = st[top + 1]; else { ch[i][0] = ch[st[top]][1]; ch[st[top]][1] = i; } st[++top] = i; } for (int i = 1; i <= n; ++i) { ansl ^= (1ll * i * (ch[i][0] + 1)); ansr ^= (1ll * i * (ch[i][1] + 1)); } writes(ansl); write(ansr); return 0; } ``` ## part 3: 例题 ### [P1377 [TJOI2011] 树的序](https://www.luogu.com.cn/problem/P1377) 因为题目中的键值 $k$ 满足 $BST$ 的性质,而儿子总是在父亲之后插入,显然插入顺序满足小根堆性质。 将读入顺序当成 $w$, 而键值当成 $k$,容易发现对这样建出来的笛卡尔树做先序遍历就是答案。 #### Code ```cpp #include <bits/stdc++.h> #define INF 0x7fffffff using namespace std; typedef long long LL; typedef unsigned long long ULL; const int N = 1e5 + 10; LL read() { LL x = 0, f = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') x = (x << 3) + (x << 1) + (LL)(ch - '0'), ch = getchar(); return x * f; } void write(LL n) { if (n < 0) putchar('-'), n = -n; if (n >= 10) write(n / 10); putchar(n % 10 + '0'); } inline void writes(LL n) { write(n); putchar(' '); } int n, a[N], ch[N][2], rt = 1; int st[N], top = 1; void dfs(int p) { if (!p) return; writes(p); dfs(ch[p][0]); dfs(ch[p][1]); } int main() { n = read(); st[1] = 1; for (int i = 1; i <= n; ++i) a[read()] = i; for (int i = 2; i <= n; ++i) { while (a[st[top]] > a[i] && top) --top; if (!top) ch[i][0] = st[top + 1], rt = i; else { ch[i][0] = ch[st[top]][1]; ch[st[top]][1] = i; } st[++top] = i; } dfs(rt); return 0; } ``` ### [CF1748E Yet Another Array Counting Problem](https://www.luogu.com.cn/problem/CF1748E) 笛卡尔树 $DP

建一棵大根堆笛卡尔树,则有区间 [l, r] 的“最左端最大值位置”就是 l 和 r 在笛卡尔树上的 LCA。且若两个序列所有“最左端最大值位置”都相同,这两个序列建出的笛卡尔树必然相同。

由 \sum N \times M \le 10^6 发现可以枚举每一个点取什么值,所以不妨设 f_u,_ i 为当 u 点取 i 时,其子树内的答案,于是有:

f_u,_i = \sum_{j = 1}^{i - 1} f_{ch[u][0]},_ j \times \sum_{j = 1}^{i} f_{ch[u][1]},_ j

Code

#include <bits/stdc++.h>
#define INF 1e18
#define RE register

using namespace std;
typedef long long LL;
const int N = 1e6 + 10;
const int MOD = 1e9 + 7;


LL read()
{
    LL x = 0, f = 1; char ch = getchar();
    while (ch < '0' || ch > '9')
    {
        if (ch == '-')
            f = -1;
        ch = getchar();
    }
    while (ch >= '0' && ch <= '9')
        x = (x << 3) + (x << 1) + (LL)(ch - '0'), ch = getchar();
    return x * f;
}

void write(LL n)
{
    if (n < 0)
        putchar('-'),
        n = -n;
    if (n >= 10)
        write(n / 10);
    putchar(n % 10 + '0');
}

inline void writee(LL n)
{
    write(n); puts("");
}


int t, n, m, p[N];
vector <vector <LL> > f;
int st[N], top, ch[N][2];

void dfs(int p)
{
    for (int i = 1; i <= m; ++i)
        f[p][i] = 1;
    if (ch[p][0])
        dfs(ch[p][0]);
    if (ch[p][1])
        dfs(ch[p][1]);
    if (ch[p][0])
        for (int i = 1; i <= m; ++i)
            f[p][i] = (f[p][i] * f[ch[p][0]][i - 1]) % MOD;
    if (ch[p][1])
        for (int i = 1; i <= m; ++i)
            f[p][i] = (f[p][i] * f[ch[p][1]][i]) % MOD;
    for (int i = 2; i <= m; ++i)
        f[p][i] = (f[p][i] + f[p][i - 1]) % MOD;
}


int main()
{
    t = read();
    for (int _ = 1; _ <= t; ++_)
    {
        memset(st, 0, sizeof st);
        memset(ch, 0, sizeof ch);
        top = 1; st[1] = 1;

        n = read(); m = read();
        for (int i = 1; i <= n; ++i)
            p[i] = read();
        for (int i = 2; i <= n; ++i)
        {
            while (p[st[top]] < p[i] && top) --top;
            if (!top)
                ch[i][0] = st[top + 1];
            else
            {
                ch[i][0] = ch[st[top]][1];
                ch[st[top]][1] = i;
            }
            st[++top] = i;
        }
        f.resize(n + 1);
        for (int i = 1; i <= n; ++i)
            f[i].resize(m + 1);
        dfs(st[1]);
        writee(f[st[1]][m]);
    }
    return 0;
}

逃(