def main() -> None: N, Q = map(int, input().split()) graph = Graph(N) for i, p in enumerate(map(int, input().split())): graph.add_edge(p, i + 1, 1) hld = HeavyLightDecomposition(graph) ans = [hld.lca(*map(int, input().split())) for _ in range(Q)] print(*ans, sep="\n")
def main() -> None: N = int(input()) graph = Graph(N) for _ in range(N - 1): s, t, w = map(int, input().split()) graph.add_edge(s, t, w) graph.add_edge(t, s, w) diam = TreeDiameter(graph) print(diam.run())
def main() -> None: N, Q = map(int, input().split()) weights = [int(x) for x in input().split()] graph = Graph(N) for _ in range(N - 1): u, v = map(int, input().split()) graph.add_edge(u, v, 1) graph.add_edge(v, u, 1) hld = HeavyLightDecomposition(graph) new_weights = [0] * N for i in range(N): new_weights[hld.vid[i]] = weights[i] rsq = SegmentTree.create_from_array(new_weights, operator.add, 0) ans = [] for _ in range(Q): t, a, b = map(int, input().split()) if t == 0: rsq[hld.vid[a]] = rsq[hld.vid[a]] + b else: ans.append(hld.query_path(a, b, 0, rsq.query, operator.add)) print(*ans, sep="\n")
def main() -> None: N, Q = map(int, input().split()) weights = [int(x) for x in input().split()] graph = Graph(N) for i, v in enumerate(map(int, input().split())): graph.add_edge(i + 1, v, 1) graph.add_edge(v, i + 1, 1) hld = HeavyLightDecomposition(graph, 0, True) new_weights = [0] * N for i in range(N): new_weights[hld.vid[i]] = weights[i] rsq = SegmentTree.create_from_array(new_weights, operator.add, 0) ans = [] for _ in range(Q): query = [int(x) for x in input().split()] if query[0] == 0: a, b = query[1:] rsq[hld.vid[a]] = rsq[hld.vid[a]] + b else: u = query[1] ans.append(rsq.query(hld.vid[u], hld.tail[u])) print(*ans, sep="\n")