結果

問題 No.778 クリスマスツリー
ユーザー むらためむらため
提出日時 2019-01-30 00:10:40
言語 Nim
(2.0.2)
結果
AC  
実行時間 65 ms / 2,000 ms
コード長 1,155 bytes
コンパイル時間 2,106 ms
コンパイル使用メモリ 63,280 KB
実行使用メモリ 38,912 KB
最終ジャッジ日時 2024-04-22 03:06:41
合計ジャッジ時間 3,245 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 1 ms
5,376 KB
testcase_04 AC 1 ms
5,376 KB
testcase_05 AC 1 ms
5,376 KB
testcase_06 AC 53 ms
38,912 KB
testcase_07 AC 25 ms
12,928 KB
testcase_08 AC 65 ms
26,496 KB
testcase_09 AC 57 ms
10,624 KB
testcase_10 AC 54 ms
10,624 KB
testcase_11 AC 53 ms
10,596 KB
testcase_12 AC 49 ms
10,592 KB
testcase_13 AC 31 ms
10,752 KB
testcase_14 AC 51 ms
38,848 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sequtils

proc getchar_unlocked():char {. importc:"getchar_unlocked",header: "<stdio.h>" .}
proc scan(): int =
  while true:
    let k = getchar_unlocked()
    if k < '0': return
    result = 10 * result + k.ord - '0'.ord

type BinaryIndexedTree*[CNT:static[int],T] = object
  data: array[CNT,T] # 引数以下の部分和(Fenwick Tree)
proc len*[CNT,T](bit:BinaryIndexedTree[CNT,T]): int = bit.data.len()
proc update*[CNT,T](bit:var BinaryIndexedTree[CNT,T],i:int,val:T) =
  var i = i
  while i <= bit.len():
    bit.data[i] += val
    i += i and -i
proc `[]`*[CNT,T](bit:BinaryIndexedTree[CNT,T],i:int): T =
  var i = i
  while i > 0:
    result += bit.data[i]
    i -= i and -i
proc `$`*[CNT,T](bit:BinaryIndexedTree[CNT,T]): string =
  result = "["
  for i in 0..<bit.len.min(100): result &= $(bit[i]) & ", "
  return result[0..result.len()-2] & (if bit.len > 100 : " ...]" else: "]")

let n = scan()
var E = newSeqWith(n,newSeq[int]())
for i in 1..<n: E[scan()] &= i
var bit : BinaryIndexedTree[200010,int]
proc solve(x:int): int =
  result += bit[x]
  bit.update(x+1, 1)
  for e in E[x]: result += solve(e)
  bit.update(x+1, -1)
echo solve(0)
0