跳到正文

个人记录

笛卡尔树入门

feather02约 11 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2026年1月1日
篇幅
约 11 分钟
获赞
3

版本

  1. v1当前

笛卡尔树入门

part -1: 闲话

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

part 0: 前置知识

BSTBST, HeapHeap, 单调栈

part 1: 概念

一种二叉树,每个结点维护两个值 (k,w)(k, w),其中 kk 要满足 BSTBST 的性质,而 ww 要满足堆的性质(大/小根堆视题目不同而不同)。由定义可以知晓,TreapTreap 也是一种笛卡尔树,只不过它的 ww 值是随机赋的。

事实上,如果笛卡尔树每个结点的 kk 各不相同, ww 也各不相同,那么这棵笛卡尔树的形态是唯一确定的,如图:

(图源自维基百科)

上图将数组下标当作 kk,数组元素值当作 ww,对于这样的笛卡尔树,则显然有一棵子树内的 kk 也就是下标是一段连续的区间。

那么可以发现,笛卡尔树可以用来解决 RMQRMQ 问题,区间 [l,r][l, r] 的最大/最小值即为点 (l,wl)(l, w_l), (r,wr)(r, w_r) 在笛卡尔树上 LCALCA 的 ww 值。

part 2: 建树

构建方式:栈构建

我们考虑将所有元素按 kk 值排好序后一个个插入到笛卡尔树当中。那么显然每次新插入的节点的位置都应该在当前笛卡尔树的右链(右链:即从根节点出发一直跳右儿子,所经过的点形成的链)的末端,以保证键值 kk 满足 BSTBST 的性质。

以小根堆笛卡尔树为例,我们就要执行以下过程: 假设我们要插入 vv 点,从原右链末端开始跳父亲,一直跳到第一个满足 wu<wvw_u < w_v 的 uu 点,让 vv 点成为 uu 点的右儿子,而让 uu 点原本的右子树成为 vv 点的左子树,易发现笛卡尔树的性质没有被破坏。

读者可以通过下图模拟一下过程:

(图源自 OI-Wiki)

显然每个节点只会进出右链一次,可以用单调栈来维护现在右链上的点,复杂度就是 O(N)O(N) 的。

建树Code

P5854 【模板】笛卡尔树

C++
#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] 树的序

因为题目中的键值 kk 满足 BSTBST 的性质,而儿子总是在父亲之后插入,显然插入顺序满足小根堆性质。

将读入顺序当成 ww, 而键值当成 kk,容易发现对这样建出来的笛卡尔树做先序遍历就是答案。

Code
C++
#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

笛卡尔树 DPDP

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

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

fu,i=∑j=1i−1fch[u][0],j×∑j=1ifch[u][1],jf_u,_i = \sum_{j = 1}^{i - 1} f_{ch[u][0]},_ j \times \sum_{j = 1}^{i} f_{ch[u][1]},_ j

Code
C++
#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;
}

逃(

原文