#include #include #include #include #include #define _USE_MATH_DEFINES #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; #define FOR(i,m,n) for(int i=(m);i<(n);++i) #define REP(i,n) FOR(i,0,n) #define ALL(v) (v).begin(),(v).end() const int INF = 0x3f3f3f3f; const long long LINF = 0x3f3f3f3f3f3f3f3fLL; const double EPS = 1e-8; const int MOD = 1000000007; // 998244353; const int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1}; /*-------------------------------------------------*/ using CostType = long long; struct Edge { int src, dst; CostType cost; Edge(int src, int dst, CostType cost = 0) : src(src), dst(dst), cost(cost) {} inline bool operator<(const Edge &rhs) const { return cost != rhs.cost ? cost < rhs.cost : dst != rhs.dst ? dst < rhs.dst : src < rhs.src; } inline bool operator<=(const Edge &rhs) const { return cost <= rhs.cost; } inline bool operator>(const Edge &rhs) const { return cost != rhs.cost ? cost > rhs.cost : dst != rhs.dst ? dst > rhs.dst : src > rhs.src; } inline bool operator>=(const Edge &rhs) const { return cost >= rhs.cost; } }; struct HLD { vector parent, subtree, id, inv, head; vector cost; HLD(const vector > &graph, int root = 0) : graph(graph) { int n = graph.size(); parent.assign(n, -1); subtree.assign(n, 1); id.resize(n); inv.resize(n); head.resize(n); dfs1(root); head[root] = root; int now_id = 0; dfs2(root, now_id); } void v_update(int u, int v, const function &f) { while (true) { if (id[u] > id[v]) swap(u, v); f(max(id[head[v]], id[u]), id[v] + 1); if (head[u] == head[v]) return; v = parent[head[v]]; } } template T v_query(int u, int v, const function &f, const function &g, T &UNITY) { T left = UNITY, right = UNITY; while (true) { if (id[u] > id[v]) { swap(u, v); swap(left, right); } left = g(left, f(max(id[head[v]], id[u]), id[v] + 1)); if (head[u] == head[v]) break; v = parent[head[v]]; } return g(left, right); } void subtree_v_update(int v, const function &f) { f(id[v], id[v] + subtree[v]); } template T subtree_v_query(int v, const function &f) { return f(id[v], id[v] + subtree[v]); } void e_update(int u, int v, const function &f) { while (true) { if (id[u] > id[v]) swap(u, v); if (head[u] == head[v]) { f(id[u], id[v]); break; } else { f(id[head[v]] - 1, id[v]); v = parent[head[v]]; } } } template T e_query(int u, int v, const function &f, const function &g, T &UNITY) { T left = UNITY, right = UNITY; while (true) { if (id[u] > id[v]) { swap(u, v); swap(left, right); } if (head[u] == head[v]) { left = g(left, f(id[u], id[v])); break; } else { left = g(left, f(id[head[v]] - 1, id[v])); v = parent[head[v]]; } } return g(left, right); } void subtree_e_update(int v, const function &f) { f(id[v], id[v] + subtree[v] - 1); } template T subtree_e_query(int v, const function &f) { return f(id[v], id[v] + subtree[v] - 1); } int lca(int u, int v) { while (true) { if (id[u] > id[v]) swap(u, v); if (head[u] == head[v]) return u; v = parent[head[v]]; } } private: vector > graph; void dfs1(int ver) { for (Edge &e : graph[ver]) if (e.dst != parent[ver]) { parent[e.dst] = ver; dfs1(e.dst); subtree[ver] += subtree[e.dst]; if (subtree[e.dst] > subtree[graph[ver].front().dst]) swap(e, graph[ver].front()); } } void dfs2(int ver, int &now_id) { id[ver] = now_id++; inv[id[ver]] = ver; for (Edge &e : graph[ver]) if (e.dst != parent[ver]) { head[e.dst] = (e.dst == graph[ver].front().dst ? head[ver] : e.dst); cost.emplace_back(e.cost); dfs2(e.dst, now_id); } } }; template struct BIT { BIT(int n_, const Abelian UNITY = 0) : n(n_), UNITY(UNITY) { ++n; dat_const.assign(n, UNITY); dat_linear.assign(n, UNITY); } void add(int left, int right, Abelian value) { for (int i = left; i < n; i += i & -i) { dat_const[i] -= value * (left - 1); dat_linear[i] += value; } for (int i = right + 1; i < n; i += i & -i) { dat_const[i] += value * right; dat_linear[i] -= value; } } Abelian sum(int idx) { Abelian res = UNITY; for (int i = idx; i > 0; i -= i & -i) res += dat_linear[i]; res *= idx; for (int i = idx; i > 0; i -= i & -i) res += dat_const[i]; return res; } Abelian sum(int left, int right) { return sum(right) - sum(left - 1); } Abelian operator[](const int idx) { return sum(idx, idx); } private: int n; const Abelian UNITY; vector dat_const, dat_linear; }; // https://onlinejudge.u-aizu.ac.jp/problems/2667 int main() { cin.tie(nullptr); ios::sync_with_stdio(false); // freopen("input.txt", "r", stdin); int n; cin >> n; vector > graph(n); REP(_, n - 1) { int u, v, w; cin >> u >> v >> w; graph[u].emplace_back(u, v, w); } HLD hld(graph, 0); BIT bit(n - 1); REP(i, n - 1) bit.add(i + 1, i + 1, hld.cost[i]); int q; cin >> q; while (q--) { int query; cin >> query; if (query == 1) { int a, x; cin >> a >> x; hld.subtree_e_update(a, [&](int lhs, int rhs) { return bit.add(lhs + 1, rhs, x); }); } else if (query == 2) { int b; cin >> b; function f = [&](int lhs, int rhs) { return bit.sum(lhs + 1, rhs); }; function g = [&](long long lhs, long long rhs) { return lhs + rhs; }; long long UNITY = 0; cout << hld.e_query(0, b, f, g, UNITY) << '\n'; } } return 0; }