結果

問題 No.778 クリスマスツリー
ユーザー H3PO4H3PO4
提出日時 2021-02-02 10:07:10
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 775 ms / 2,000 ms
コード長 689 bytes
コンパイル時間 270 ms
コンパイル使用メモリ 87,120 KB
実行使用メモリ 365,160 KB
最終ジャッジ日時 2023-09-12 11:01:13
合計ジャッジ時間 7,321 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 71 ms
71,504 KB
testcase_01 AC 69 ms
71,400 KB
testcase_02 AC 74 ms
71,396 KB
testcase_03 AC 70 ms
71,388 KB
testcase_04 AC 70 ms
71,480 KB
testcase_05 AC 70 ms
71,404 KB
testcase_06 AC 724 ms
360,404 KB
testcase_07 AC 214 ms
109,316 KB
testcase_08 AC 775 ms
198,092 KB
testcase_09 AC 443 ms
104,336 KB
testcase_10 AC 436 ms
104,348 KB
testcase_11 AC 412 ms
104,220 KB
testcase_12 AC 462 ms
107,664 KB
testcase_13 AC 329 ms
106,204 KB
testcase_14 AC 693 ms
365,160 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys

sys.setrecursionlimit(10 ** 9)


class Bit:
    """1-indexed"""

    def __init__(self, n):
        self.size = n
        self.tree = [0] * (n + 1)

    def sum(self, i):
        s = 0
        while i > 0:
            s += self.tree[i]
            i -= i & -i
        return s

    def add(self, i, x):
        while i <= self.size:
            self.tree[i] += x
            i += i & -i


N = int(input())
A = map(int, input().split())
T = [[] for _ in range(N)]
for i, a in enumerate(A, 1):
    T[a].append(i)

bit = Bit(N)


def dfs(v):
    res = bit.sum(v)
    bit.add(v + 1, 1)
    for x in T[v]:
        res += dfs(x)
    bit.add(v + 1, -1)
    return res


print(dfs(0))
0