====== 회사 문화 2 ====== ===== 풀이 ===== * [[ps:구간 쿼리#오일러 경로 테크닉]]을 적용한 뒤, 구간합 업데이트 + 포인트 쿼리를 처리하는 문제. * 서브트리의 루트도 업데이트 된다는 점만 제외하면 [[ps:problems:boj:2820|자동차 공장]]과 동일한 문제이므로, 자세한 풀이는 그쪽을 참조. * [[ps:problems:boj:16404|주식회사 승범이네]]와는 문제가 전부 똑같다. ===== 코드 ===== """Solution code for "BOJ 14268. 회사 문화 2". - Problem link: https://www.acmicpc.net/problem/14268 - Solution link: http://www.teferi.net/ps/problems/boj/14268 """ import sys from teflib import fenwicktree from teflib import tgraph def euler_tour_technique(tree, root): subtree_ranges = [[None, None] for _ in tree] order = 0 for node in tgraph.dfs(tree, root, yields_on_leave=True): if subtree_ranges[node][0] is None: subtree_ranges[node][0] = order order += 1 else: subtree_ranges[node][1] = order return subtree_ranges def main(): n, m = [int(x) for x in sys.stdin.readline().split()] tree = [[] for _ in range(n)] bosses = [int(x) for x in sys.stdin.readline().split()] for employee, boss in enumerate(bosses): if boss != -1: tree[boss - 1].append(employee) subtree_ranges = euler_tour_technique(tree, 0) fenwick = fenwicktree.FenwickTree(n + 1) for _ in range(m): query = [int(x) for x in sys.stdin.readline().split()] if query[0] == 1: _, i, w = query beg, end = subtree_ranges[i - 1] fenwick.update(beg, w) fenwick.update(end, -w) else: i = query[1] pos = subtree_ranges[i - 1][0] print(fenwick.query(0, pos + 1)) if __name__ == '__main__': main() * Dependency: [[:ps:teflib:fenwicktree#FenwickTree|teflib.fenwicktree.FenwickTree]], [[:ps:teflib:tgraph#dfs|teflib.tgraph.dfs]] {{tag>BOJ ps:problems:boj:플래티넘_3}}