結果

問題 No.59 鉄道の旅
ユーザー むらためむらため
提出日時 2019-09-30 17:06:58
言語 Nim
(2.0.2)
結果
AC  
実行時間 90 ms / 5,000 ms
コード長 2,046 bytes
コンパイル時間 4,826 ms
コンパイル使用メモリ 61,380 KB
実行使用メモリ 19,608 KB
最終ジャッジ日時 2024-04-14 08:14:29
合計ジャッジ時間 3,860 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 12 ms
19,212 KB
testcase_01 AC 11 ms
19,272 KB
testcase_02 AC 12 ms
19,276 KB
testcase_03 AC 12 ms
19,388 KB
testcase_04 AC 90 ms
19,276 KB
testcase_05 AC 12 ms
19,276 KB
testcase_06 AC 10 ms
19,608 KB
testcase_07 AC 11 ms
19,288 KB
testcase_08 AC 22 ms
19,416 KB
testcase_09 AC 19 ms
19,196 KB
testcase_10 AC 20 ms
19,204 KB
testcase_11 AC 16 ms
19,212 KB
testcase_12 AC 58 ms
19,240 KB
testcase_13 AC 69 ms
19,236 KB
testcase_14 AC 88 ms
19,200 KB
testcase_15 AC 12 ms
19,272 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sequtils,math
template times*(n:int,body) = (for _ in 0..<n: body)
template `max=`*(x,y) = x = max(x,y)
template `min=`*(x,y) = x = min(x,y)

proc getchar_unlocked():char {. importc:"getchar_unlocked",header: "<stdio.h>" .}
proc scan(): int =
  result = 0
  var minus = false
  while true:
    var k = getchar_unlocked()
    if k == '-' : minus = true
    elif k < '0' or k > '9': break
    else: result = 10 * result + k.ord - '0'.ord
  if minus: result *= -1

type
  SegmentTree[T] = ref object
    n : int
    data:seq[T]
    infinity: T
    cmp:proc(x,y:T):T
proc newSegmentTree[T](size:int,infinity:T) : SegmentTree[T] =
  new(result)
  result.n = size.nextPowerOfTwo()
  result.data = newSeqWith(result.n * 2,infinity)
  result.infinity = infinity
proc `[]=`[T](self:var SegmentTree[T],i:int,val:T) =
  var i = i + self.n - 1
  self.data[i] = val
  while i > 0:
    i = (i - 1) shr 1
    self.data[i] = self.cmp(self.data[i * 2 + 1],self.data[i * 2 + 2])
proc queryImpl[T](self:SegmentTree[T],target,now:Slice[int],i:int) : T =
  if now.b <= target.a or target.b <= now.a : return self.infinity
  if target.a <= now.a and now.b <= target.b : return self.data[i]
  let next = (now.a + now.b) shr 1
  let vl = self.queryImpl(target, now.a..next, i*2+1)
  let vr = self.queryImpl(target, next..now.b, i*2+2)
  return self.cmp(vl,vr)
proc `[]`[T](self:SegmentTree[T],slice:Slice[int]): T =
  return self.queryImpl(slice.a..slice.b+1,0..self.n,0)
proc `[]`[T](self:SegmentTree[T],i:int): T = self[i..i]

proc newAddSegmentTree[T](size:int) : SegmentTree[T] =
  # 区間和のセグツリ
  result = newSegmentTree[T](size,0.int)
  proc addimpl[T](x,y:T): T = x + y
  result.cmp = addimpl[T]


let n = scan()
let k = scan()
const MAX = 100_0001
var cargo =  newAddSegmentTree[int](MAX)
n.times:
  var w = scan()
  if w > 0:
    if cargo[w..<MAX] >= k: continue # W以上の荷物がK個以上
    cargo[w] = cargo[w] + 1
  else:
    w *= -1
    if cargo[w] == 0 : continue # 荷物が無い
    cargo[w] = cargo[w] - 1
echo cargo[0..<MAX]
0