#include"bits/stdc++.h" using namespace std; #define REP(k,m,n) for(ll (k)=(m);(k)<(n);(k)++) #define rep(i,n) REP((i),0,(n)) using ll = long long; using Graph = vector>; using WGraph = vector>>; struct HLDecomposition { using pii = pair; ll n; Graph G; vector vid, inv, par, depth, subsize, head, prev, next, type; HLDecomposition(const Graph& G_) : n(G_.size()), G(G_), vid(n, -1), inv(n), par(n), depth(n), subsize(n, 1), head(n), prev(n, -1), next(n, -1), type(n) {} void build(vector roots = { 0 }) { ll curtype = 0, pos = 0; for (ll root : roots) { decide_heavy_edge(root); reconstruct(root, curtype++, pos); } } void decide_heavy_edge(ll root) { stack st; par[root] = -1, depth[root] = 0; st.emplace(root, 0); while (!st.empty()) { ll now = st.top().first; ll& way = st.top().second; if (way < G[now].size()) { ll child = G[now][way++]; if (child == par[now])continue; par[child] = now; depth[child] = depth[now] + 1; st.emplace(child, 0); } else { st.pop(); ll maxsize = 0; for (auto child : G[now]) { if (child == par[now])continue; subsize[now] += subsize[child]; if (maxsize < subsize[child]) { maxsize = subsize[child]; prev[child] = now; next[now] = child; } } } } } void reconstruct(ll root, ll curtype, ll& pos) { stack st({ root }); while (!st.empty()) { ll start = st.top(); st.pop(); for (ll v = start; v != -1; v = next[v]) { type[v] = curtype; vid[v] = pos++; inv[vid[v]] = v; head[v] = start; for (auto child : G[v]) { if (child != par[v] && child != next[v]) { st.push(child); } } } } } // node query [u, v], f([left, right]) void foreach_nodes(ll u, ll v, const function& f) { while (true) { if (vid[u] > vid[v])swap(u, v); f(max(vid[head[v]], vid[u]), vid[v]); if (head[u] != head[v])v = par[head[v]]; else break; } } // edge query[u,v] f([left, right]) // seg_node[vid[i]] := edge(par[i] -> i) void foreach_edges(ll u, ll v, const function& f) { while (true) { if (vid[u] > vid[v])swap(u, v); if (head[u] != head[v]) { f(vid[head[v]], vid[v]); v = par[head[v]]; } else { if (u != v)f(vid[u] + 1, vid[v]); break; } } } ll lca(ll u, ll v) { while (true) { if (vid[u] > vid[v])swap(u, v); if (head[u] == head[v])return u; v = par[head[v]]; } } }; template class LazySegmentTree { private: using F = function; using G = function; using H = function; ll sz; // 対応する配列の幅 vector data; vector lazy; const F f; // 2区間マージ演算(data-data-ボトムアップマージ) const G g; // 要素,作用素マージ演算(lazy->data同位置変換時の、(data,lazy,len)の計算) const H h; // 作用素マージ演算 (query->lazyトップダウン伝搬時の、(lazy,query_value)の計算) const Monoid M1; // モノイド単位元 (data単位元) const OperatorMonoid OM0; // 作用素単位元 (lazy単位元) void propagate(ll idx, ll len) { // 幅lenのlazy[idx]が存在するとき、値を下に流す if (lazy[idx] != OM0) { if (idx < sz) { lazy[(idx << 1) | 0] = h(lazy[(idx << 1) | 0], lazy[idx]); lazy[(idx << 1) | 1] = h(lazy[(idx << 1) | 1], lazy[idx]); } data[idx] = g(data[idx], lazy[idx], len); lazy[idx] = OM0; } } Monoid update_impl(ll a, ll b, const OperatorMonoid& val, ll idx, ll l, ll r) { propagate(idx, r - l); if (r <= a || b <= l)return data[idx]; else if (a <= l && r <= b) { lazy[idx] = h(lazy[idx], val); propagate(idx, r - l); return data[idx]; } else return data[idx] = f( update_impl(a, b, val, (idx << 1) | 0, l, (l + r) >> 1), update_impl(a, b, val, (idx << 1) | 1, (l + r) >> 1, r) ); } Monoid query_impl(ll a, ll b, ll idx, ll l, ll r) { propagate(idx, r - l); if (r <= a || b <= l)return M1; else if (a <= l && r <= b)return data[idx]; else return f( query_impl(a, b, (idx << 1) | 0, l, (l + r) >> 1), query_impl(a, b, (idx << 1) | 1, (l + r) >> 1, r) ); } public: // init忘れに注意 LazySegmentTree(ll n, const F f, const G g, const H h, const Monoid& M1, const OperatorMonoid OM0) :f(f), g(g), h(h), M1(M1), OM0(OM0) { sz = 1; while (sz < n)sz <<= 1; data.assign(2 * sz, M1); lazy.assign(2 * sz, OM0); } void build(const vector& vals) { rep(idx, vals.size())data[idx + sz] = vals[idx]; for (ll idx = sz - 1; idx > 0; idx--) { data[idx] = f(data[(idx << 1) | 0], data[(idx << 1) | 1]); } } Monoid update(ll a, ll b, const OperatorMonoid& val) { return update_impl(a, b, val, 1, 0, sz); } Monoid query(ll a, ll b) { return query_impl(a, b, 1, 0, sz); } Monoid operator[](const ll& idx) { return query(idx, idx + 1); } }; int main() { ll N; cin >> N; Graph gh(N); map, ll> weight; rep(i, N - 1) { ll u, v, w; cin >> u >> v >> w; gh[u].push_back(v); gh[v].push_back(u); weight[{u, v}] = weight[{v, u}] = w; } HLDecomposition hld(gh); hld.build(); vector weights(N, 0); REP(i, 1, N) { auto p = make_pair(i, hld.par[i]); if (weight.find(p) != weight.end()) { weights[hld.vid[i]] = weight[p]; continue; } swap(p.first, p.second); if (weight.find(p) != weight.end()) { weights[hld.vid[i]] = weight[p]; continue; } assert(false); } auto f = [](ll vl, ll vr) {return vl + vr; }; auto g = [](ll data, ll lazy, int len) {return data + lazy * len; }; auto h = [](ll lazy, ll query) {return lazy + query; }; LazySegmentTree lst(N, f, g, h, 0, 0); lst.build(weights); ll Q; cin >> Q; while (Q--) { ll type; cin >> type; if (type == 1) { ll a, x; cin >> a >> x; ll siz = hld.subsize[a]; a = hld.vid[a]; lst.update(a + 1, a + siz + 1, x); } else { ll b, res = 0; cin >> b; hld.foreach_edges(0, b, [&](ll l, ll r) { res += lst.query(l, r + 1); }); cout << res << endl; } } return 0; }