题目链接: Housewife Wind
大致题意
给定一棵有 n n n个节点的树, 你最初位于 s s s号节点, 有 m m m次如下操作:
0 c
询问
s
s
s到
c
c
c的距离, 然后令
s
=
c
s = c
s=c.
1 x c
把第
x
x
x条边的边权修改为
c
c
c.
解题思路
树链剖分 POJ又卡我vector, 就你卡我vector!!!
很经典的树链剖分问题, 我们考虑到题目操作的本质: 求一条树链的边权和 及 单点修改.
唯一需要转化的一点是: 我们需要讲树中的边权化为点权. 我们在第一次dfs时, 将每条边的权值记录到深度更深的节点上即可.
由于第一次dfs, 我们会确定树的形态, 在dfs的过程中, 每个点都会被遍历一次, 我们将所有边权记录到深度更深的节点上, 可以保证除根节点以外的所有点都被分配一个权值.
由于题目涉及到对于边权的修改, 我们不妨对于每条边, 做一个映射 r e i d [ ] reid[] reid[], 表示第 i i i条边的权值存储到了 r e i d [ i ] reid[i] reid[i]这个点上. 这样修改操作, 我们只需要修改这个点的权值即可.
考虑到树链询问操作, 例如询问 ( a , b ) (a, b) (a,b), 我们需要注意的是, l c a ( a , b ) lca(a, b) lca(a,b)的权值不应当被计算, 因为该点是表示从 p [ l c a ( a , b ) ] p[lca(a, b)] p[lca(a,b)]到 l c a ( a , b ) lca(a, b) lca(a,b)点的边权, 我们并不需要走到 p [ l c a ( a , b ) ] p[lca(a,b)] p[lca(a,b)]点.
AC代码
#include <头文件>
#define rep(i, n) for (int i = 1; i <= (n); ++i)
using namespace std;
typedef long long ll;
const int N = 1E5 + 10;
int w[N], reid[N];
int h[N], lto[N * 2], lval[N * 2], lid[N * 2], ne[N * 2], indl;
void add(int a, int b, int c, int eid) {
lto[++indl] = b, lval[indl] = c, lid[indl] = eid;
ne[indl] = h[a], h[a] = indl;
}
int p[N], dep[N], sz[N], son[N];
void dfs1(int x = 1, int fa = 0) {
p[x] = fa, dep[x] = dep[fa] + 1, sz[x] = 1;
for (int i = h[x]; i; i = ne[i]) {
int to = lto[i], val = lval[i], eid = lid[i];
if (to == fa) continue;
dfs1(to, x);
sz[x] += sz[to];
if (sz[to] > sz[son[x]]) son[x] = to;
w[to] = val, reid[eid] = to; //边权 -> 点权
}
}
int id[N], top[N], ind, nw[N];
void dfs2(int x = 1, int tp = 1) {
id[x] = ++ind, top[x] = tp; nw[ind] = w[x];
if (!son[x]) return;
dfs2(son[x], tp);
for (int i = h[x]; i ; i = ne[i]) {
int to = lto[i];
if (to == p[x] or to == son[x]) continue;
dfs2(to, to);
}
}
struct node {
int l, r;
ll sum;
}t[N << 2];
void pushup(int x) { t[x].sum = t[x << 1].sum + t[x << 1 | 1].sum; }
void build(int l, int r, int x = 1) {
t[x] = { l, r, nw[l] };
if (l == r) return;
int mid = l + r >> 1;
build(l, mid, x << 1), build(mid + 1, r, x << 1 | 1);
pushup(x);
}
void modify(int a, int c, int x = 1) {
if (t[x].l == t[x].r) {
t[x].sum = c;
return;
}
int mid = t[x].l + t[x].r >> 1;
modify(a, c, x << 1 | (a > mid));
pushup(x);
}
ll ask(int l, int r, int x = 1) {
if (l <= t[x].l and r >= t[x].r) return t[x].sum;
int mid = t[x].l + t[x].r >> 1;
ll res = 0;
if (l <= mid) res = ask(l, r, x << 1);
if (r > mid) res += ask(l, r, x << 1 | 1);
return res;
}
ll ask_route(int a, int b) {
ll res = 0;
while (top[a] != top[b]) {
if (dep[top[a]] < dep[top[b]]) swap(a, b);
res += ask(id[top[a]], id[a]);
a = p[top[a]];
}
if (a == b) return res; // 表示当前点为LCA, 无需计算.
if (dep[a] > dep[b]) swap(a, b);
res += ask(id[son[a]], id[b]); // 边权->点权的题目无需计算LCA的点权, 特殊注意查询为(id[son[a]], id[b])
return res;
}
int main()
{
int n, m, s; cin >> n >> m >> s;
rep(i, n - 1) {
int a, b, c; scanf("%d %d %d", &a, &b, &c);
add(a, b, c, i), add(b, a, c, i);
}
dfs1();
dfs2();
build(1, n);
rep(i, m) {
int tp; scanf("%d", &tp);
if (!tp) {
int x; scanf("%d", &x);
ll res = ask_route(s, x);
s = x;
printf("%lld\n", res);
}
else {
int x, c; scanf("%d %d", &x, &c);
int qaq = reid[x];
modify(id[qaq], c);
}
}
return 0;
}