P2486 题解
BlackHoles · · 题解
背景:我刚学树剖,写一篇详细题解整理思路。
前置知识
树链剖分,线段树。
正解
不妨以
题目需要支持两种操作:路径赋值,路径查询颜色段。
然而在树上无法快速维护这些操作的信息。
于是我们对树进行树链剖分,同时记录这样每一个结点
具体维护方式:线段树的每个结点记录
实现细节
线段树的区间合并时,要判断左区间右端点是否和右区间左端点颜色相同,是则可以合并为同一个颜色段。
在求
具体实现见代码。
代码
#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;
}