#include using namespace std; using ll = long long; struct Tree : vector> { private: void dfs_sz(int v, int p) { sz[v] = 1; ord.emplace_back(v); for(int i = p, x = 0; i != -1;) { bl[v][x] = i; i = bl[i][x], x++; } for(auto &nv : (*this)[v]) { if(nv == p) { if((int)(*this)[v].size() >= 2 && nv == (*this)[v][0]) { std::swap((*this)[v][0], (*this)[v][1]); } else { continue; } } dp[nv] = dp[v] + 1; dfs_sz(nv, v); sz[v] += sz[nv]; if(sz[nv] > sz[(*this)[v][0]]) { std::swap(nv, (*this)[v][0]); } } } void dfs_hld(int v) { down[v] = id++; for(auto &nv : (*this)[v]) { if(nv == par(v)) { continue; } nx[nv] = (nv == (*this)[v][0] ? nx[v] : nv); dfs_hld(nv); } up[v] = id; } vector> ascend(int u, int v) const { vector> r; while(nx[u] != nx[v]) { r.emplace_back(down[u], down[nx[u]]); u = par(nx[u]); } if(u != v) { r.emplace_back(down[u], down[v] + 1); } return r; } vector> descend(int u, int v) const { if(u == v) { return {}; } if(nx[u] == nx[v]) { return {{down[u] + 1, down[v]}}; } auto r = descend(u, par(nx[v])); r.emplace_back(down[nx[v]], down[v]); return r; } public: int n, root, id = 0; vector> bl; vector dp, sz, ord, down, up, nx; Tree(int n_, int r = 0): n(n_), root(r) { this->resize(n); } Tree(vector> &g, int r = 0): n(g.size()), root(r) { *this = g; build(); } void add_edge(int u, int v) { (*this)[u].emplace_back(v); (*this)[v].emplace_back(u); } void build() { bl.resize(n); dp.resize(n); sz.resize(n); down.assign(n, -1); up.assign(n, -1); nx.assign(n, root); for(auto &v : bl) { ranges::fill(v, -1); } dfs_sz(root, -1); dfs_hld(root); } int size() const { return n; } int depth(int i) const { return dp[i]; } int par(int i) const { return i == root ? root : bl[i][0]; } int order(int i) const { return ord[i]; } int in(int i) const { return down[i]; } int out(int i) const { return up[i]; } int size(int i) const { return sz[i]; } int kth_ancestor(int i, int k) const { if(dp[i] < k) { return -1; } while(k) { int t = __builtin_ctz(k); i = bl[i][t], k ^= 1 << t; } return i; } int dis(int u, int v) { return dp[u] + dp[v] - dp[lca(u, v)] * 2; } bool onpath(int u, int v, int x) { return dis(u, v) == dis(u, x) + dis(x, v); } int nxt(int u, int v) const { if(dp[u] >= dp[v]) { return par(u); } int x = kth_ancestor(v, dp[v] - dp[u] - 1); return bl[x][0] == u ? x : bl[u][0]; } vector path(int u, int v) const { vector pre, suf; while(dp[u] > dp[v]) { pre.emplace_back(u); u = bl[u][0]; } while(dp[u] < dp[v]) { suf.emplace_back(v); v = bl[v][0]; } while(u != v) { pre.emplace_back(u); suf.emplace_back(v); u = bl[u][0]; v = bl[v][0]; } pre.emplace_back(u); ranges::reverse(suf); copy(suf.begin(), suf.end(), back_inserter(pre)); return pre; } int lca(int u, int v) { while(nx[u] != nx[v]) { if(down[u] < down[v]) { std::swap(u, v); } u = par(nx[u]); } return dp[u] < dp[v] ? u : v; } int jump(int u, int v, int x) { int lc = lca(u, v), d1 = dp[u] - dp[lc]; if(x <= d1) { return kth_ancestor(u, x); } int d = d1 + dp[v] - dp[lc]; if(x <= d) { return kth_ancestor(v, d - x); } return -1; } vector diameter() { int s = ranges::max_element(dp) - dp.begin(); vector d(n, -1); d[s] = 0; queue q; q.emplace(s); while(!q.empty()) { int v = q.front(); q.pop(); for(auto &nv : (*this)[v]) { if(d[nv] == -1) { d[nv] = d[v] + 1; q.emplace(nv); } } } int t = ranges::max_element(d) - d.begin(); return path(t, s); } template void query(int u, int v, bool vertex, const F &f) { int l = lca(u, v); for(auto &&[a, b] : ascend(u, l)) { int s = a + 1, t = b; s > t ? f(t, s) : f(s, t); } if(vertex) f(down[l], down[l] + 1); for(auto &&[a, b] : descend(l, v)) { int s = a, t = b + 1; s > t ? f(t, s) : f(s, t); } } template void noncommutative_query(int u, int v, bool vertex, const F &f) { int l = lca(u, v); for(auto &&[a, b] : ascend(u, l)) { f(a + 1, b); } if(vertex) { f(down[l], down[l] + 1); } for(auto &&[a, b] : descend(l, v)) { f(a, b + 1); } } template void subtree_query(int u, bool vertex, const F &f) { f(down[u] + int(!vertex), up[u]); } }; #include using namespace atcoder; template struct DualBIT { fenwick_tree f1, f2; DualBIT(int N): f1(N + 1), f2(N + 1) {} void add(int i, T x) { add(i, i + 1, x); } void add(int l, int r, T x) { f1.add(l, x), f1.add(r, -x); f2.add(l, -x * (l - 1)), f2.add(r, x * (r - 1)); } T sum(int i) { return f1.sum(0, i) * (i - 1) + f2.sum(0, i); } T sum(int l, int r) { return sum(r) - sum(l); } T operator[](int i) { return sum(i + 1) - sum(i); } }; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); ll N; cin >> N; Tree t(N); vector> e(N - 1); for(auto &[u, v, w] : e) { cin >> u >> v >> w; t.add_edge(u, v); } t.build(); DualBIT B(N); for(auto &[u, v, w] : e) { if(t.depth(u) > t.depth(v)) { swap(u, v); } B.add(t.in(v), w); } ll Q; cin >> Q; while(Q--) { ll type, i, x; cin >> type >> i; if(type == 1) { cin >> x; t.subtree_query(i, false, [&](ll l, ll r) { B.add(l, r, x); }); } else { ll ans = 0; t.query(0, i, false, [&](ll l, ll r) { ans += B.sum(l, r); }); cout << ans << "\n"; } } }