結果

問題 No.778 クリスマスツリー
ユーザー lllllll88938494lllllll88938494
提出日時 2023-05-15 21:46:44
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 981 bytes
コンパイル時間 1,398 ms
コンパイル使用メモリ 86,684 KB
実行使用メモリ 143,824 KB
最終ジャッジ日時 2023-08-20 21:01:07
合計ジャッジ時間 6,286 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 61 ms
71,384 KB
testcase_01 WA -
testcase_02 AC 59 ms
71,076 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 RE -
testcase_07 WA -
testcase_08 RE -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
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:
        ind += 1
        kukan[x][1] = ind
        return
    ind += 1
    for i in rin[x]:
        if i != pre:
            cal(i,x)
    
    kukan[x][1] = ind

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