結果

問題 No.778 クリスマスツリー
ユーザー 6soukiti296soukiti29
提出日時 2018-12-31 14:37:43
言語 Nim
(2.0.2)
結果
AC  
実行時間 91 ms / 2,000 ms
コード長 779 bytes
コンパイル時間 4,135 ms
コンパイル使用メモリ 65,716 KB
実行使用メモリ 42,840 KB
最終ジャッジ日時 2024-04-22 03:05:01
合計ジャッジ時間 4,274 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 1 ms
6,940 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 1 ms
6,940 KB
testcase_04 AC 1 ms
6,940 KB
testcase_05 AC 1 ms
6,944 KB
testcase_06 AC 75 ms
42,840 KB
testcase_07 AC 43 ms
24,300 KB
testcase_08 AC 91 ms
33,480 KB
testcase_09 AC 90 ms
20,828 KB
testcase_10 AC 89 ms
20,824 KB
testcase_11 AC 83 ms
20,920 KB
testcase_12 AC 88 ms
20,824 KB
testcase_13 AC 55 ms
20,856 KB
testcase_14 AC 71 ms
42,816 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sequtils,strutils
type
    binaryIndexTree[I:static[int]] = array[I + 1,int] 

proc add(BIT :var binaryIndexTree,i : int, a : int)=
    var index = i
    while BIT[0] >= index:
        BIT[index] += a
        index += ((index xor (index - 1)) and index)

proc sum(BIT : binaryIndexTree, i : int):int =
    var index = i
    while index > 0:
        result += BIT[index]
        index = (index and (index - 1))


var
    BIT : binaryIndexTree[200010]
BIT[0] = 200010

var
    N = stdin.readline.parseInt
    A = stdin.readline.split.map(parseInt)
    B = newSeqWith(N + 1,newSeq[int](0))
    ans = 0
    
for i,a in A:
    B[a + 1].add(i + 2)


proc dfs(i : int) =
    ans += BIT.sum(i)
    BIT.add(i, 1)
    for p in B[i]:
        dfs(p)
    BIT.add(i, -1)
dfs(1)
echo ans
0