結果

問題 No.778 クリスマスツリー
ユーザー lllllll88938494lllllll88938494
提出日時 2023-05-15 21:57:23
言語 PyPy3
(7.3.15)
結果
RE  
実行時間 -
コード長 963 bytes
コンパイル時間 1,021 ms
コンパイル使用メモリ 82,144 KB
実行使用メモリ 133,244 KB
最終ジャッジ日時 2024-05-08 03:28:31
合計ジャッジ時間 4,783 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 40 ms
52,352 KB
testcase_01 AC 39 ms
52,480 KB
testcase_02 AC 41 ms
52,608 KB
testcase_03 AC 40 ms
52,120 KB
testcase_04 AC 40 ms
52,352 KB
testcase_05 AC 40 ms
52,348 KB
testcase_06 RE -
testcase_07 AC 251 ms
124,572 KB
testcase_08 RE -
testcase_09 AC 532 ms
129,624 KB
testcase_10 AC 532 ms
130,264 KB
testcase_11 AC 516 ms
129,756 KB
testcase_12 AC 510 ms
129,320 KB
testcase_13 AC 299 ms
130,192 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