結果

問題 No.778 クリスマスツリー
ユーザー lllllll88938494lllllll88938494
提出日時 2023-05-15 21:57:23
言語 PyPy3
(7.3.15)
結果
RE  
実行時間 -
コード長 963 bytes
コンパイル時間 1,441 ms
コンパイル使用メモリ 86,788 KB
実行使用メモリ 143,924 KB
最終ジャッジ日時 2023-08-20 21:06:29
合計ジャッジ時間 5,586 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 59 ms
71,448 KB
testcase_01 AC 57 ms
71,304 KB
testcase_02 AC 59 ms
71,228 KB
testcase_03 AC 59 ms
71,448 KB
testcase_04 AC 57 ms
71,360 KB
testcase_05 AC 59 ms
71,132 KB
testcase_06 RE -
testcase_07 AC 213 ms
143,924 KB
testcase_08 RE -
testcase_09 AC 425 ms
133,376 KB
testcase_10 AC 410 ms
132,568 KB
testcase_11 AC 432 ms
132,788 KB
testcase_12 AC 383 ms
132,640 KB
testcase_13 AC 245 ms
131,932 KB
testcase_14 RE -
権限があれば一括ダウンロードができます

ソースコード

diff #

class Bit:
    def __init__(self, n):
        self.size = n
        self.tree = [0] * (n + 1)
 
    def _add(self, i, x):
        while i <= self.size:
            self.tree[i] += x
            i += i & -i
            
    def _sum(self, i):
        s = 0
        while i > 0:
            s += self.tree[i]
            i -= i & -i
        return s
        
n=int(input())
rin=[[] for i in range(n)]
a=list(map(int,input().split()))

for i in range(1,n):
    rin[a[i-1]].append(i)
    rin[i].append(a[i-1])

bit=Bit(n+2)

kukan = [[0,0] for i in range(n)]
ind=1
def  cal(x,pre):
    global ind
    kukan[x][0] = ind
    if len(rin[x]) == 1 and x!= pre:
        kukan[x][1] = ind
        ind += 1
        return
    ind += 1
    for i in rin[x]:
        if i != pre:
            cal(i,x)
    
    kukan[x][1] = ind-1

cal(0,0)
ans = 0
for x in range(n-1,-1,-1):
    l,r=kukan[x][0],kukan[x][1]
    ans += bit._sum(r) - bit._sum(l-1)
    bit._add(l,1)


print(ans)

0