树链剖分模板题。。。维护一下树链即可。。。
#include <iostream> #include <queue> #include <stack> #include <map> #include <set> #include <bitset> #include <cstdio> #include <algorithm> #include <cstring> #include <climits> #include <cstdlib> #include <cmath> #include <time.h> #define maxn 50005 #define maxm 100005 #define eps 1e-10 #define mod 1000000007 #define INF 1e17 #define lowbit(x) (x&(-x)) #define mp make_pair #define ls o<<1 #define rs o<<1 | 1 #define lson o<<1, L, mid #define rson o<<1 | 1, mid+1, R //#pragma comment(linker, "/STACK:16777216") typedef long long LL; typedef unsigned long long ULL; //typedef int LL; using namespace std; LL qpow(LL a, LL b){LL res=1,base=a;while(b){if(b%2)res=res*base;base=base*base;b/=2;}return res;} LL powmod(LL a, LL b){LL res=1,base=a;while(b){if(b%2)res=res*base%mod;base=base*base%mod;b/=2;}return res;} void scanf(int &__x){__x=0;char __ch=getchar();while(__ch==' '||__ch=='\n')__ch=getchar();while(__ch>='0'&&__ch<='9')__x=__x*10+__ch-'0',__ch = getchar();} LL gcd(LL _a, LL _b){if(!_b) return _a;else return gcd(_b, _a%_b);} // head LL tree[maxn]; int size[maxn]; int son[maxn]; int dep[maxn]; int top[maxn]; int fa[maxn]; int w[maxn]; int n, m, z; struct Edge { int v; Edge *next; }pool[maxm], *H[maxn], *edges; struct node { int u, v, c; node(int u = 0, int v = 0, int c = 0) : u(u), v(v), c(c) {} }edge[maxn]; void init(void) { z = 0; edges = pool; for(int i = 0; i <= n; i++) H[i] = 0, son[i] = 0, tree[i] = 0; memset(H, 0, sizeof H); memset(son, 0, sizeof son); memset(tree, 0, sizeof tree); } void addedges(int u, int v) { edges->v = v; edges->next = H[u]; H[u] = edges++; } void read(void) { int u, v, c; for(int i = 1; i < n; i++) { scanf("%d%d%d", &u, &v, &c); addedges(u, v); addedges(v, u); edge[i] = node(u, v, c); } } void dfs1(int u) { size[u] = 1, son[u] = 0; for(Edge *e = H[u]; e; e = e->next) { int v = e->v; if(v != fa[u]) { dep[v] = dep[u] + 1; fa[v] = u; dfs1(v); size[u] += size[v]; if(size[son[u]] < size[v]) son[u] = v; } } } void dfs2(int u, int tp) { w[u] = ++z, top[u] = tp; if(son[u]) dfs2(son[u], tp); for(Edge *e = H[u]; e; e = e->next) { int v = e->v; if(v != son[u] && v != fa[u]) dfs2(v, v); } } void add(int x, int v) { for(int i = x; i <= n; i += lowbit(i)) tree[i] += v; } int sum(int x) { int ans = 0; for(int i = x; i > 0; i -= lowbit(i)) ans += tree[i]; return ans; } LL query(int a, int b) { int f1 = top[a], f2 = top[b]; LL ans = 0; while(f1 != f2) { if(dep[f1] < dep[f2]) { swap(f1, f2); swap(a, b); } ans += sum(w[a]) - sum(w[f1] - 1); a = fa[f1], f1 = top[a]; } if(a == b) return ans; if(dep[a] > dep[b]) swap(a, b); ans += sum(w[b]) - sum(w[a]); return ans; } void work(void) { dfs1(1); dfs2(1, 1); for(int i = 1; i < n; i++) if(fa[edge[i].u] == edge[i].v) swap(edge[i].u, edge[i].v); for(int i = 1; i < n; i++) add(w[edge[i].v], edge[i].c); int k, a, b; while(m--) { scanf("%d%d%d", &k, &a, &b); if(k) { printf("%I64d\n", query(a, b)); } else { add(w[edge[a].v], -edge[a].c); edge[a].c = b; add(w[edge[a].v], edge[a].c); } } } int main(void) { while(scanf("%d%d", &n, &m)!=EOF) { init(); read(); work(); } return 0; }