結果
問題 | No.778 クリスマスツリー |
ユーザー | htkb |
提出日時 | 2019-01-06 14:39:07 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 804 ms / 2,000 ms |
コード長 | 1,439 bytes |
コンパイル時間 | 187 ms |
コンパイル使用メモリ | 81,664 KB |
実行使用メモリ | 393,028 KB |
最終ジャッジ日時 | 2024-05-03 03:11:34 |
合計ジャッジ時間 | 5,685 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 56 ms
67,072 KB |
testcase_01 | AC | 53 ms
66,688 KB |
testcase_02 | AC | 54 ms
67,200 KB |
testcase_03 | AC | 54 ms
67,200 KB |
testcase_04 | AC | 54 ms
67,072 KB |
testcase_05 | AC | 55 ms
67,200 KB |
testcase_06 | AC | 663 ms
393,028 KB |
testcase_07 | AC | 231 ms
140,760 KB |
testcase_08 | AC | 804 ms
231,748 KB |
testcase_09 | AC | 443 ms
132,704 KB |
testcase_10 | AC | 447 ms
132,332 KB |
testcase_11 | AC | 437 ms
132,316 KB |
testcase_12 | AC | 480 ms
132,000 KB |
testcase_13 | AC | 322 ms
132,132 KB |
testcase_14 | AC | 613 ms
393,020 KB |
ソースコード
class BinaryIndexedTree(object): __slots__ = ["tree"] def __init__(self, size: int): self.tree = [0]*(size+1) def add(self, index: int, value: int): tree = self.tree while index < len(tree): tree[index] += value index += index & -index def sum(self, index: int): tree, result = self.tree, 0 while index: result += tree[index] index -= index & -index return result def euler_tour(tree): import sys sys.setrecursionlimit(10**7) edges = [[] for _ in [0]*(len(tree)+1)] for origin, destination in enumerate(tree, start=1): edges[destination].append(origin) subtree_range = [[0, 0] for _ in [0]*(len(tree)+1)] def rec(v, index): subtree_range[v][0] = index for destination in edges[v]: index = rec(destination, index+1) subtree_range[v][1] = index return index + 1 rec(0, 1) return subtree_range def solve(): N = int(input()) tree = list(map(int, input().split())) subtree_range = euler_tour(tree) bit = BinaryIndexedTree(2*N-1) ans = 0 for start, end in subtree_range[::-1]: ans += bit.sum(end) - bit.sum(start-1) bit.add(start, 1) print(ans) if __name__ == "__main__": import threading threading.stack_size(10**9) thread = threading.Thread(target=solve) thread.start()