题目大意:
就是现在有一棵树, 每条边都有自己的边权, 然后接下来会询问两点间路径的边权和, 或者修改某条边的权值
大致思路:
树链剖分水题= =
剖分之后线段树维护即可
代码如下:
Result : Accepted Memory : 5952 KB Time : 184 ms
/* * Author: Gatevin * Created Time: 2015/9/8 18:14:17 * File Name: Sakura_Chiyo.cpp */ #include<iostream> #include<sstream> #include<fstream> #include<vector> #include<list> #include<deque> #include<queue> #include<stack> #include<map> #include<set> #include<bitset> #include<algorithm> #include<cstdio> #include<cstdlib> #include<cstring> #include<cctype> #include<cmath> #include<ctime> #include<iomanip> using namespace std; const double eps(1e-8); typedef long long lint; #define maxn 50010 int top[maxn]; int grandson[maxn]; int dep[maxn]; int siz[maxn]; int belong[maxn]; int father[maxn]; int Q[maxn]; int cnt; int hson[maxn]; int n; bool vis[maxn]; int id[maxn]; int antiID[maxn]; struct Edge { int u, v, w, nex; Edge(int _u, int _v, int _w, int _nex) { u = _u, v = _v, w = _w, nex = _nex; } Edge(){} }; Edge edge[maxn << 1]; int tot; int head[maxn]; lint w[maxn]; void add_Edge(int x, int y, int w) { edge[++tot] = Edge(x, y, w, head[x]); head[x] = tot; return; } void split() { cnt = 0; int l = 0, r = 1; dep[Q[r] = 1] = 1; father[r] = -1; w[r] = 0; while(l < r) { int x = Q[++l]; if(head[x] == -1) continue; for(int j = head[x]; j + 1; j = edge[j].nex) { int y = edge[j].v; if(y == father[x]) continue; w[y] = edge[j].w; dep[Q[++r] = y] = dep[x] + 1; father[y] = x; } } for(int i = n; i; i--) { int x = Q[i], p = -1; siz[x] = 1; if(head[x] == -1) continue; for(int j = head[x]; j + 1; j = edge[j].nex) { int y = edge[j].v; if(y == father[x]) continue; siz[x] += siz[y]; if(p == -1 || (p > 0 && siz[y] > siz[p])) p = y; } if(p == -1) { hson[x] = -1; grandson[++cnt] = x; belong[top[cnt] = x] = cnt; } else { hson[x] = p; belong[x] = belong[p]; top[belong[x]] = x; } } int idx = 0; memset(vis, 0, sizeof(vis)); for(int i = n; i; i--) { int x = Q[i]; if(vis[x]) continue; vis[x] = 1; id[x] = ++idx; antiID[idx] = x; while(father[x] != -1 && belong[father[x]] == belong[x] && !vis[father[x]]) { x = father[x]; id[x] = ++idx; antiID[idx] = x; vis[x] = 1; } } return; } struct Segment_Tree { #define lson l, mid, rt << 1 #define rson mid + 1, r, rt << 1 | 1 lint sum[maxn << 2]; void pushUp(int rt) { sum[rt] = sum[rt << 1] + sum[rt << 1 | 1]; return; } void build(int l, int r, int rt) { if(l == r) { sum[rt] = w[antiID[l]]; return; } int mid = (l + r) >> 1; build(lson); build(rson); pushUp(rt); } void update(int l, int r, int rt, int pos, lint value) { if(l == r) { sum[rt] = value; return; } int mid = (l + r) >> 1; if(mid >= pos) update(lson, pos, value); else update(rson, pos, value); pushUp(rt); } lint query(int l, int r, int rt, int L, int R) { if(l >= L && r <= R) return sum[rt]; int mid = (l + r) >> 1; lint ret = 0; if(mid >= L) ret += query(lson, L, R); if(mid + 1 <= R) ret += query(rson, L, R); return ret; } }; Segment_Tree st; lint answer(int x, int y) { lint ans = 0; while(top[belong[x]] != top[belong[y]]) { if(dep[top[belong[x]]] < dep[top[belong[y]]]) swap(x, y); ans += st.query(1, n, 1, id[x], id[top[belong[x]]]); x = father[top[belong[x]]]; } if(x == y) return ans; if(dep[x] < dep[y]) swap(x, y); ans += st.query(1, n, 1, id[x], id[hson[y]]); return ans; } int main() { int m; while(scanf("%d %d", &n, &m) != EOF) { memset(head, -1, sizeof(head)); tot = 0; int u, v, x; lint w; for(int i = 1; i < n; i++) { scanf("%d %d %I64d", &u, &v, &w); add_Edge(u, v, w); add_Edge(v, u, w); } split(); st.build(1, n, 1); int op; while(m--) { scanf("%d", &op); switch(op) { case 0: scanf("%d %I64d", &x, &w); u = edge[x << 1].u; v = edge[x << 1].v; if(father[u] == v) st.update(1, n, 1, id[u], w); else st.update(1, n, 1, id[v], w); break; case 1: scanf("%d %d", &u, &v); printf("%I64d\n", answer(u, v)); break; } } } return 0; }