結果

問題 No.778 クリスマスツリー
ユーザー H3PO4H3PO4
提出日時 2021-02-02 10:06:51
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 1,402 ms / 2,000 ms
コード長 689 bytes
コンパイル時間 153 ms
コンパイル使用メモリ 10,804 KB
実行使用メモリ 78,776 KB
最終ジャッジ日時 2023-09-12 11:01:05
合計ジャッジ時間 11,443 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 16 ms
7,904 KB
testcase_01 AC 16 ms
7,848 KB
testcase_02 AC 16 ms
7,952 KB
testcase_03 AC 16 ms
7,768 KB
testcase_04 AC 16 ms
7,816 KB
testcase_05 AC 16 ms
7,848 KB
testcase_06 AC 1,058 ms
72,504 KB
testcase_07 AC 878 ms
31,624 KB
testcase_08 AC 1,402 ms
54,508 KB
testcase_09 AC 1,171 ms
45,864 KB
testcase_10 AC 1,168 ms
45,776 KB
testcase_11 AC 1,163 ms
45,876 KB
testcase_12 AC 1,145 ms
47,032 KB
testcase_13 AC 922 ms
46,688 KB
testcase_14 AC 1,096 ms
78,776 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