Wannfly day2 采蘑菇
作者:互联网
这题简直树链剖分板子题。。。。真没啥可说的。当时不懂树剖,可惜啦
#include<cstring> #include<iostream> #include<algorithm> #include<queue> using namespace std; typedef long long ll; const int maxn = 1e6 + 1000; ll tree[4 * maxn]; int update(int node, int be, int en, int i, ll val) { int mid = be + en >> 1; int l = node * 2; int r = node * 2 + 1; if (be == en) { tree[node] += val; return 0; } if (i <= mid) update(l, be, mid, i, val); else update(r, mid + 1, en, i, val); tree[node] = tree[l] + tree[r]; return 0; } ll qurry(int node, int be, int en, int LL, int RR) { int mid = be + en >> 1; int l = node * 2; int r = node * 2 + 1; if (LL > en || RR < be) return 0; if (LL <= be && en <= RR) { return tree[node]; } ll val1 = qurry(l, be, mid, LL, RR); ll val2 = qurry(r, mid + 1, en, LL, RR); return val1 + val2; } int n; struct Node { int to; int next; ll len; }G[2*maxn]; int cn = 0; int head[maxn]; int add(int be, int en, ll len) { G[++cn].to = en; G[cn].len = len; G[cn].next = head[be]; head[be] = cn; return 0; } int dep[maxn], son[maxn], siz[maxn], top[maxn]; int id[maxn],f[maxn]; int dfs(int x, int fa, int d) { dep[x] = d; f[x] = fa; siz[x] = 1; int s = 0; for (int i = head[x]; i; i = G[i].next) { int p = G[i].to; if (p == fa) continue; dfs(p, x, d + 1); siz[x] += siz[p]; if (s < siz[p]) { s = siz[p]; son[x] = p; } } return 0; } int cnt = 0; int dfs2(int x, int t) { id[x] = ++cnt; top[x] = t; if (son[x]) dfs2(son[x], t); for (int i = head[x]; i; i = G[i].next) { int p = G[i].to; if (p == f[x] || p == son[x]) continue; dfs2(p, p); } return 0; } int main() { scanf("%d", &n); int be, en; ll len; for (int i = 1; i < n; i++) { scanf("%d %d %lld", &be, &en, &len); add(be, en, len); add(en, be, len); } dfs(1, -1, 0); dfs2(1, 1); int q; scanf("%d", &q); int x; int root = 1; ll ans = 0; ll cns = 0; ll sum = 0; ll a = 0; ll b = 0; while (q--) { int op; scanf("%d", &op); if (op == 1) { scanf("%d %lld", &x, &len); sum += len; update(1, 1, n, id[x], len); a = qurry(1, 1, n, id[root], id[root]); cns = 0; ans = 0; b = 0; for (int i = head[root]; i; i = G[i].next) { int p = G[i].to; ll ln = G[i].len; if (dep[p] > dep[root]) { ll c = qurry(1, 1, n, id[p], id[p] + siz[p] - 1); ans += ln * c; cns += c; } else { b = ln; } } ans += (sum - a - cns)*b; printf("%lld\n", ans); } else { scanf("%d", &x); root = x; a = qurry(1, 1, n, id[root], id[root]); cns = 0; ans = 0; b = 0; for (int i = head[root]; i; i = G[i].next) { int p = G[i].to; ll ln = G[i].len; if (dep[p] > dep[root]) { ll c = qurry(1, 1, n, id[p], id[p] + siz[p] - 1); ans += ln * c; cns += c; } else { b = ln; } } ans += (sum - a - cns)*b; printf("%lld\n", ans); } } return 0; }
标签:node,采蘑菇,root,ll,day2,int,ans,Wannfly,id 来源: https://www.cnblogs.com/lesning/p/12430180.html