結果

問題 No.778 クリスマスツリー
ユーザー noriocnorioc
提出日時 2023-08-29 03:32:44
言語 PyPy3
(7.3.15)
結果
RE  
実行時間 -
コード長 970 bytes
コンパイル時間 293 ms
コンパイル使用メモリ 87,220 KB
実行使用メモリ 128,988 KB
最終ジャッジ日時 2023-08-29 03:32:51
合計ジャッジ時間 6,574 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 92 ms
71,560 KB
testcase_01 AC 91 ms
71,800 KB
testcase_02 AC 91 ms
71,676 KB
testcase_03 AC 91 ms
71,444 KB
testcase_04 AC 115 ms
71,848 KB
testcase_05 AC 94 ms
71,752 KB
testcase_06 RE -
testcase_07 AC 284 ms
121,156 KB
testcase_08 RE -
testcase_09 AC 546 ms
126,976 KB
testcase_10 AC 545 ms
127,316 KB
testcase_11 AC 564 ms
127,048 KB
testcase_12 AC 569 ms
125,444 KB
testcase_13 AC 318 ms
127,356 KB
testcase_14 RE -
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import defaultdict


class FenwickTree:
    def __init__(self, n):
        self.data = [0] * (n + 10)

    def add(self, p, x):
        assert 0 <= p
        p += 1
        while p < len(self.data):
            self.data[p] += x
            p += p & -p

    def sum(self, p):
        """区間 [0, p] の和"""
        assert 0 <= p
        p += 1
        s = 0
        while p > 0:
            s += self.data[p]
            p -= p & -p
        return s

    def rangesum(self, l, r):
        """区間 [l, r] の和"""
        assert 0 <= l <= r
        s = self.sum(r)
        if l > 0:
            s -= self.sum(l-1)
        return s


N = int(input())
A = list(map(int, input().split()))

adj = defaultdict(list)
for i in range(N-1):
    par = A[i]
    adj[par].append(i+1)


def dfs(v):
    res = ft.sum(v)
    ft.add(v, 1)
    for to in adj[v]:
        res += dfs(to)
    ft.add(v, -1)
    return res


ft = FenwickTree(N)
ans = dfs(0)
print(ans)
0