题目大意:给你N个点,其中一个点是你当前所在的位置,现在提供两种操作
0 u:从当前点到u点的距离
1 u w:将第u条边的权值改成w
解题思路:树剖裸题,不过这题坑,卡了vector
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 100010;
struct Edge{
int u, v, dis, next;
Edge() {}
Edge(int u, int v, int dis, int next): u(u), v(v), dis(dis), next(next){}
}E[N * 2];
int dep[N], size[N], top[N], son[N], id[N], f[N], head[N];
int n, q, s, idx, tot;
int bit[N];
void dfs1(int u, int fa, int depth) {
dep[u] = depth; size[u] = 1; son[u] = 0; f[u] = fa;
for (int i = head[u]; ~i; i = E[i].next) {
int v = E[i].v;
if (v == fa) continue;
dfs1(v, u, depth + 1);
size[u] += size[v];
if (size[son[u]] < size[v]) son[u] = v;
}
}
void dfs2(int u, int tp) {
top[u] = tp;
id[u] = ++idx;
if (son[u]) dfs2(son[u], tp);
for (int i = head[u]; ~i; i = E[i].next) {
int v = E[i].v;
if (v == f[u] || v == son[u]) continue;
dfs2(v, v);
}
}
inline int lowbit(int x) {
return x & (-x);
}
void add(int x, int w) {
while (x < N) {
bit[x] += w;
x += lowbit(x);
}
}
int query(int x) {
int ans = 0;
while (x) {
ans += bit[x];
x -= lowbit(x);
}
return ans;
}
int query(int l, int r) {
return query(r) - query(l - 1);
}
int gao(int u, int v) {
int tp1 = top[u], tp2 = top[v];
int ans = 0;
while (tp1 != tp2) {
if (dep[tp1] < dep[tp2]) {
swap(tp1, tp2);
swap(u, v);
}
ans += query(id[tp1], id[u]);
u = f[tp1];
tp1 = top[u];
}
if (u == v) return ans;
if (dep[u] > dep[v]) swap(u, v);
ans += query(id[son[u]], id[v]);
return ans;
}
void solve() {
int op, a, b;
while (q--) {
scanf("%d", &op);
if (op == 1) {
scanf("%d%d", &a, &b);
a--;
int tmp = query(id[E[a * 2].u], id[E[a * 2].u]);
add(id[E[a * 2].u], b - tmp);
}
else {
scanf("%d", &a);
printf("%d\n", gao(s, a));
s = a;
}
}
}
void AddEdge(int u, int v, int dis) {
E[tot] = Edge(u, v, dis, head[u]);
head[u] = tot++;
E[tot] = Edge(v, u, dis, head[v]);
head[v] = tot++;
}
void init() {
memset(bit, 0, sizeof(bit));
memset(head, -1, sizeof(head));
idx = tot = 0;
int u, v, dis;
for (int i = 1; i < n; i++) {
scanf("%d%d%d", &u, &v, &dis);
AddEdge(u, v, dis);
}
dfs1(1, 0, 1);
dfs2(1, 1);
for (int i = 0; i < tot; i += 2) {
if (dep[E[i].u] < dep[E[i].v]) swap(E[i].u, E[i].v);
add(id[E[i].u], E[i].dis);
}
}
int main() {
while (scanf("%d%d%d", &n, &q, &s) != EOF) {
init();
solve();
}
return 0;
}