#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #define REP(i,n) for(int i = 0, _n = (n); i < _n; i++) #define REPD(i,n) for(int i = (n) - 1; i >= 0; i--) #define FOR(i,a,b) for (int i = (a), _b = (b); i <= _b; i++) #define FORD(i,a,b) for (int i = (a), _b = (b); i >= _b; i--) #define FORN(i,a,b) for(int i=a;i> n; for (int i = 1; i < n; i++) { int u, v; cin >> u >> v >> cost[i]; edge[i] = make_pair(u, v); adj[u].push_back(make_pair(v, cost[i])); adj[v].push_back(make_pair(u, cost[i])); } init(); int q; cin >> q; while (q--) { int t, u, v, newc, index; cin >> t; if (t == 1) { cin >> index >> newc; int u = edge[index].first; int v = edge[index].second; if (parent[u] != v) swap(u, v); //now parent[v] = u. //change the weight of the edge connecting u and v will affect all the nodes in the sub-tree rooted at v. int change = newc - cost[index]; cost[index] = newc; //change is the difference between the current value and the new value //=> we add change in all the nodes in the sub-tree rooted at v update(1, 1, n, pos[u], pos[u] + size[u] - 1, change); } else if (t == 2) { cin >> u >> v; int p = lca(u, v); //getPath(u) is the distance from 1 to u. cout << getPath(u) + getPath(v) - 2 * getPath(p) << endl; } else assert(0); } return 0; }