結果
| 問題 | No.778 クリスマスツリー | 
| コンテスト | |
| ユーザー |  | 
| 提出日時 | 2021-02-28 14:03:30 | 
| 言語 | PyPy3 (7.3.15) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 764 ms / 2,000 ms | 
| コード長 | 593 bytes | 
| コンパイル時間 | 316 ms | 
| コンパイル使用メモリ | 82,128 KB | 
| 実行使用メモリ | 338,324 KB | 
| 最終ジャッジ日時 | 2024-10-02 18:13:18 | 
| 合計ジャッジ時間 | 5,963 ms | 
| ジャッジサーバーID (参考情報) | judge4 / judge3 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 3 | 
| other | AC * 12 | 
ソースコード
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)
            
            
            
        