跳到正文

题解

P2486 题解

BlackHoles约 8 分钟
原文
文章信息与版本

文章信息

发布
2025年8月13日
收录
2025年12月12日
更新
2026年11月11日
篇幅
约 8 分钟
获赞
1
题目
P2486

版本

  1. v1当前

背景:我刚学树剖,写一篇详细题解整理思路。

前置知识

树链剖分,线段树。

正解

不妨以 11 为根。

题目需要支持两种操作:路径赋值,路径查询颜色段。

然而在树上无法快速维护这些操作的信息。

于是我们对树进行树链剖分,同时记录这样每一个结点 ii 的 DFS 序,这样对于每一条重链,上面的点的 DFS 序是连续的,可以看作 DFS 序的一个区间。于是每条路径就可以看作 O(log⁡n)O(\log n) 条重链。我们对 DFS 序建立一颗线段树,那么操作就变成了区间赋值和区间查询颜色段,可以使用线段树维护。

具体维护方式:线段树的每个结点记录 33 个信息:颜色段,左端点颜色和右端点颜色,同时记录 tagtag 表示区间赋值懒标记。

实现细节

线段树的区间合并时,要判断左区间右端点是否和右区间左端点颜色相同,是则可以合并为同一个颜色段。

在求 x,yx, y 两点的路径颜色段时,我们需要记录 cx,cycx, cy 分别表示 x,yx, y 两点之前路径的左端点颜色,方便不断跳重链时进行去重。

具体实现见代码。

代码

C++
#include <bits/stdc++.h>
#define ls(p) (p << 1)
#define rs(p) (p << 1 | 1)
using namespace std;
const int N = 100005;
const int T = (N << 3);
int n, m, w[N];
vector <int> G[N];
int fa[N], siz[N], dep[N], top[N], son[N], dfn[N], cnt, col[N];
struct SMT {
	int num;
	int lc, rc;
	SMT () {}
	SMT (int num, int lc, int rc) : num(num), lc(lc), rc(rc) {}
} tr[T];
int tag[T];
SMT operator + (const SMT a, const SMT b) {
	SMT res = SMT(a.num + b.num, a.lc, b.rc);
	if (a.rc == b.lc) --res.num;
	return res;
}
void preDfs(int u, int father) {
	fa[u] = father, dep[u] = dep[father] + 1;
	siz[u] = 1;
	for (int v : G[u]) {
		if (v == father) continue;
		preDfs(v, u);
		siz[u] += siz[v];
		if (!son[u] || siz[v] > siz[son[u]]) son[u] = v;
	}
	return ;
}
void Dfs(int u, int topu) {
	top[u] = topu;
	dfn[u] = ++cnt, col[cnt] = w[u];
	if (!son[u]) return ;
	Dfs(son[u], topu);
	for (int v : G[u]) {
		if (v == fa[u] || v == son[u]) continue;
		Dfs(v, v);
	}
	return ;
}
void addtag(int p, int color) {
	tr[p] = SMT(1, color, color);
	tag[p] = color;
	return ;
}
void push_up(int p) {
	tr[p] = tr[ls(p)] + tr[rs(p)];
	return ;
}
void push_down(int p) {
	if (tag[p]) {
		addtag(ls(p), tag[p]);
		addtag(rs(p), tag[p]);
		tag[p] = 0;
	}
	return ;
}
void Build(int p, int l, int r) {
	if (l == r) {
		tr[p] = SMT(1, col[l], col[l]);
		return ;
	}
	int mid = (l + r) >> 1;
	Build(ls(p), l, mid), Build(rs(p), mid + 1, r);
	push_up(p);
	return ;
}
void Update(int p, int l, int r, int L, int R, int color) {
	if (L <= l && r <= R) {
		addtag(p, color);
		return ;
	}
	push_down(p);
	int mid = (l + r) >> 1;
	if (L <= mid) Update(ls(p), l, mid, L, R, color);
	if (R > mid) Update(rs(p), mid + 1, r, L, R, color);
	push_up(p);
	return ;
}
SMT Query(int p, int l, int r, int L, int R) {
	if (L <= l && r <= R) return tr[p];
	push_down(p);
	int mid = (l + r) >> 1;
	if (L <= mid && R <= mid) return Query(ls(p), l, mid, L, R);
	if (L > mid && R > mid) return Query(rs(p), mid + 1, r, L, R);
	return (Query(ls(p), l, mid, L, R) + Query(rs(p), mid + 1, r, L, R));
}
void Paint_path(int x, int y, int color) {
	while (top[x] != top[y]) {
		if (dep[top[x]] < dep[top[y]]) swap(x, y);
		Update(1, 1, n, dfn[top[x]], dfn[x], color);
		x = fa[top[x]];
	}
	if (dep[x] > dep[y]) swap(x, y);
	Update(1, 1, n, dfn[x], dfn[y], color);
	return ;
}
int Query_path(int x, int y) {
	int ans = 0;
	int cx = 0, cy = 0;
	while (top[x] != top[y]) {
		if (dep[top[x]] < dep[top[y]]) swap(x, y), swap(cx, cy);
		SMT res = Query(1, 1, n, dfn[top[x]], dfn[x]);
		ans += res.num;
		if (res.rc == cx) --ans;
		cx = res.lc;
		x = fa[top[x]];
	}
	if (dep[x] > dep[y]) swap(x, y), swap(cx, cy);
	SMT res = Query(1, 1, n, dfn[x], dfn[y]);
	ans += res.num;
	if (res.lc == cx) --ans;
	if (res.rc == cy) --ans;
	return ans;
}
int main(void) {
	ios::sync_with_stdio(false);
	cin.tie(0), cout.tie(0);
	cin >> n >> m;
	for (int i = 1; i <= n; ++i)
		cin >> w[i];
	for (int i = 1, u, v; i < n; ++i) {
		cin >> u >> v;
		G[u].push_back(v), G[v].push_back(u);
	}
	preDfs(1, 0), Dfs(1, 1);
	Build(1, 1, n);
	while (m--) {
		char op;
		int a, b, c;
		cin >> op;
		if (op == 'C') {
			cin >> a >> b >> c;
			Paint_path(a, b, c);
		}
		else {
			cin >> a >> b;
			cout << Query_path(a, b) << '\n';
		}
	}
	return 0;
}
原文
存档评论2 条抓取于