def main(): n, q = map(int, input().split()) edges = [list(map(int, input().split())) for i in range(n - 1)] queries = [list(map(int, input().split())) for i in range(q)] tree = [[] for i in range(n)] for u, v in edges: tree[u].append(v) tree[v].append(u) hld = HLDecomposition(tree) bit = BinaryIndexedTree(n) ans = [] for flag, *query in queries: if flag == 0: u, v = query res = 0 for l, r in hld.range_edge_path(u, v): res += bit.sum(l, r) ans.append(res) else: v, x = query l, r = hld.range_subtree(v) bit.add(l, r, x) bit.add(hld[v], hld[v] + 1, -x) print('\n'.join(map(str, ans)))
def main(): n = int(input()) nodes = [list(map(int, input().split())) for i in range(n)] q = int(input()) queries = [list(map(int, input().split())) for i in range(q)] tree = [[] for i in range(n)] for u in range(n): for v in nodes[u][1:]: tree[u].append(v) tree[v].append(u) hld = HLDecomposition(tree) bit = BinaryIndexedTree(n) ans = [] for flag, *query in queries: if flag == 0: v, val = query for l, r in hld.range_edge_path(0, v): bit.add(l, r, val) else: v = query[0] res = 0 for l, r in hld.range_edge_path(0, v): res += bit.sum(l, r) ans.append(res) print('\n'.join(map(str, ans)))
def main(): n, q = map(int, input().split()) a = list(map(int, input().split())) edges = [list(map(int, input().split())) for i in range(n - 1)] queries = [list(map(int, input().split())) for i in range(q)] tree = [[] for i in range(n)] for u, v in edges: tree[u].append(v) tree[v].append(u) hld = HLDecomposition(tree) bit = BinaryIndexedTree(n) for i, val in enumerate(a): bit.add(hld[i], val) ans = [] for flag, u, v in queries: if flag == 0: val = v bit.add(hld[u], val) else: res = 0 for l, r in hld.range_vertex_path(u, v): res += bit.sum(l, r) ans.append(res) print("\n".join(map(str, ans)))
def main(): n, q = map(int, input().split()) a = list(map(int, input().split())) p = list(map(int, input().split())) queries = [list(map(int, input().split())) for i in range(q)] tree = [[] for i in range(n)] for u, v in enumerate(p): u += 1 tree[u].append(v) tree[v].append(u) hld = HLDecomposition(tree) bit = BinaryIndexedTree(n) for i, val in enumerate(a): bit.add(hld[i], val) ans = [] for flag, *query in queries: if flag == 0: u, val = query bit.add(hld[u], val) else: u = query[0] l, r = hld.range_subtree(u) ans.append(bit.sum(l, r)) print("\n".join(map(str, ans)))
def main(): n, q = map(int, input().split()) p = list(map(int, input().split())) queries = [list(map(int, input().split())) for i in range(q)] tree = [[] for i in range(n)] for u, v in enumerate(p): u += 1 tree[u].append(v) tree[v].append(u) hld = HLDecomposition(tree) ans = [] for u, v in queries: ans.append(hld.lca(u, v)) print('\n'.join(map(str, ans)))
def main(): n, m, q = map(int, input().split()) edges = [list(map(int, input().split())) for i in range(m)] queries = [list(map(int, input().split())) for i in range(q)] tree = [[] for i in range(n)] uf = UnionFind(n) for i, (u, v) in enumerate(edges): u -= 1 v -= 1 tree[u].append(v) tree[v].append(u) uf.merge(u, v) edges[i] = (u, v) ans = 0 weights = [0] * n hld = HLDecomposition(tree) for u, v in queries: u -= 1 v -= 1 dist = hld.distance(u, v) if dist == -1: weights[u] += 1 weights[v] += 1 else: ans += dist unit = (0, 0) merge = lambda x1, x2: (x1[0] + x2[0], x1[1] + x2[1]) addnode = lambda x1, v: (x1[0] + weights[v], x1[0] + x1[1]) res = rerooting(n, edges, unit, merge, addnode) for gp in uf.groups(): min_cost = 10**9 for i in gp: min_cost = min(res[i][1], min_cost) ans += min_cost print(ans)