1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
| #include <bits/stdc++.h>
#define debug(x) cerr << #x << " = " << x << endl
using namespace std; typedef long long LL;
const int MAXN = 2E5 + 5; const int MOD = 1E9 + 7; int n, m, q, T; vector<int> G[MAXN]; array<int, MAXN> in, out, depth; LL tot;
struct Edge { int a, b; } e[MAXN];
template <class T> void read(T& x) { x = 0; T f = 1; char ch = getchar(); while (ch < '0' || ch > '9') f = (ch == '-' ? -1 : 1), ch = getchar(); while (ch >= '0' && ch <= '9') x = x * 10 + ch - 48, ch = getchar(); x *= f; }
template <class T, class... Args> void read(T& x, Args&... args) { read(x), read(args...); }
struct BIT { int lowbit(int x) { return x & (~x + 1); } BIT() { for (int i = 1; i <= n; i++) c[i] = 0; } void build() { for (int i = 1; i <= n; i++) { int j = i + lowbit(i); if (j <= n) c[j] += c[i]; } } void add(int x, LL val) { while (x <= n) c[x] += val, x += lowbit(x); } LL query(int x, LL res = 0) { while (x) res += c[x], x -= lowbit(x); return res; } array<LL, MAXN> c; } bit[3];
inline void add(int l, int r, LL val) { bit[1].add(l, val), bit[1].add(r + 1, -val); bit[2].add(l, val * l), bit[2].add(r + 1, -val * (r + 1)); }
inline LL query(int l, int r, LL res = 0) { res = bit[0].query(r) + bit[1].query(r) * (r + 1) - bit[2].query(r); res -= bit[0].query(l - 1) + bit[1].query(l - 1) * l - bit[2].query(l - 1); return res; }
void dfs(int now, int fa) { static int times = 1; depth[now] = depth[fa] + 1, in[now] = times++; for (auto& to : G[now]) { if (to != fa) dfs(to, now); } out[now] = times; }
signed main() { read(n); for (int i = 1; i <= n - 1; i++) { read(e[i].a, e[i].b); G[e[i].a].push_back(e[i].b); G[e[i].b].push_back(e[i].a); } dfs(1, 1); read(q); for (int i = 1; i <= q; i++) { int type, ei, x; read(type, ei, x); if (type == 1) { if (depth[e[ei].a] > depth[e[ei].b]) add(in[e[ei].a], out[e[ei].a] - 1, x); else { tot += x; add(in[e[ei].b], out[e[ei].b] - 1, -x); } } else { if (depth[e[ei].a] < depth[e[ei].b]) add(in[e[ei].b], out[e[ei].b] - 1, x); else { tot += x; add(in[e[ei].a], out[e[ei].a] - 1, -x); } } } for (int i = 1; i <= n; i++) printf("%lld\n", query(in[i], in[i]) + tot); return 0; }
|