結果

問題 No.778 クリスマスツリー
ユーザー flippergoflippergo
提出日時 2021-02-28 14:03:30
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 674 ms / 2,000 ms
コード長 593 bytes
コンパイル時間 275 ms
コンパイル使用メモリ 82,432 KB
実行使用メモリ 338,392 KB
最終ジャッジ日時 2024-04-10 15:58:39
合計ジャッジ時間 5,059 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 31 ms
52,096 KB
testcase_01 AC 30 ms
52,608 KB
testcase_02 AC 34 ms
52,008 KB
testcase_03 AC 34 ms
52,608 KB
testcase_04 AC 37 ms
52,608 KB
testcase_05 AC 31 ms
52,480 KB
testcase_06 AC 609 ms
338,032 KB
testcase_07 AC 176 ms
140,672 KB
testcase_08 AC 674 ms
189,880 KB
testcase_09 AC 388 ms
118,520 KB
testcase_10 AC 420 ms
118,140 KB
testcase_11 AC 391 ms
118,304 KB
testcase_12 AC 445 ms
120,912 KB
testcase_13 AC 201 ms
120,960 KB
testcase_14 AC 589 ms
338,392 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
sys.setrecursionlimit(200010)
N = int(input())
A = list(map(int,input().split()))
A.insert(0,0)
G = {i:[] for i in range(1,N+1)}
for i in range(1,N):
    a = A[i]+1
    G[a].append(i+1)
k = 0
while 2**k<N:
    k += 1
K = 2**k
B = [0 for _ in range(K+1)]
def update(i,x):
    j = i
    while j<=K:
        B[j] += x
        j += j&(-j)
def csum(i):
    j = i
    tot = 0
    while j>0:
        tot += B[j]
        j -= j&(-j)
    return tot
cnt = 0
def dfs(i):
    global cnt
    cnt += csum(i)
    update(i,1)
    for j in G[i]:
        dfs(j)
        update(j,-1)
dfs(1)
print(cnt)
0