結果

問題 No.778 クリスマスツリー
ユーザー むらためむらため
提出日時 2019-01-29 23:51:49
言語 Nim
(2.0.2)
結果
WA  
実行時間 -
コード長 1,207 bytes
コンパイル時間 2,396 ms
コンパイル使用メモリ 67,128 KB
実行使用メモリ 41,632 KB
最終ジャッジ日時 2023-09-14 03:12:43
合計ジャッジ時間 4,457 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 AC 70 ms
41,632 KB
testcase_07 AC 45 ms
21,824 KB
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
権限があれば一括ダウンロードができます

ソースコード

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] # 引数以下の部分和
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) =
  assert i > 0
  var i = i
  while i < bit.len():
    bit.data[i] += val
    i = i or (i + 1)
proc `[]`*[CNT,T](bit:BinaryIndexedTree[CNT,T],i:int): T =
  result = 0
  var i = i
  while i >= 0:
    result += bit.data[i]
    i = (i and (i + 1)) - 1
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]
  for e in E[x]:
    result += bit[e]
    bit.update(e, 1)
    result += solve(e)
    bit.update(e, -1)
echo solve(0)
0