笛卡尔树入门
part -1: 闲话
感觉这个东西用处不是很大的说
part 0: 前置知识
, , 单调栈
part 1: 概念
一种二叉树,每个结点维护两个值 ,其中 要满足 的性质,而 要满足堆的性质(大/小根堆视题目不同而不同)。由定义可以知晓, 也是一种笛卡尔树,只不过它的 值是随机赋的。
事实上,如果笛卡尔树每个结点的 各不相同, 也各不相同,那么这棵笛卡尔树的形态是唯一确定的,如图:
(图源自维基百科)
上图将数组下标当作 ,数组元素值当作 ,对于这样的笛卡尔树,则显然有一棵子树内的 也就是下标是一段连续的区间。
那么可以发现,笛卡尔树可以用来解决 问题,区间 的最大/最小值即为点 , 在笛卡尔树上 的 值。
part 2: 建树
构建方式:栈构建
我们考虑将所有元素按 值排好序后一个个插入到笛卡尔树当中。那么显然每次新插入的节点的位置都应该在当前笛卡尔树的右链(右链:即从根节点出发一直跳右儿子,所经过的点形成的链)的末端,以保证键值 满足 的性质。
以小根堆笛卡尔树为例,我们就要执行以下过程: 假设我们要插入 点,从原右链末端开始跳父亲,一直跳到第一个满足 的 点,让 点成为 点的右儿子,而让 点原本的右子树成为 点的左子树,易发现笛卡尔树的性质没有被破坏。
读者可以通过下图模拟一下过程:
(图源自 OI-Wiki)
显然每个节点只会进出右链一次,可以用单调栈来维护现在右链上的点,复杂度就是 的。
建树Code
#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] 树的序
因为题目中的键值 满足 的性质,而儿子总是在父亲之后插入,显然插入顺序满足小根堆性质。
将读入顺序当成 , 而键值当成 ,容易发现对这样建出来的笛卡尔树做先序遍历就是答案。
Code
#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
笛卡尔树
建一棵大根堆笛卡尔树,则有区间 的“最左端最大值位置”就是 和 在笛卡尔树上的 。且若两个序列所有“最左端最大值位置”都相同,这两个序列建出的笛卡尔树必然相同。
由 发现可以枚举每一个点取什么值,所以不妨设 为当 点取 时,其子树内的答案,于是有:
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;
}
逃(

