結果

問題 No.59 鉄道の旅
ユーザー むらためむらため
提出日時 2019-09-30 16:59:31
言語 Nim
(2.0.0)
結果
WA  
実行時間 -
コード長 2,030 bytes
コンパイル時間 2,548 ms
コンパイル使用メモリ 66,676 KB
実行使用メモリ 20,004 KB
最終ジャッジ日時 2023-07-26 18:43:53
合計ジャッジ時間 3,797 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 AC 44 ms
19,536 KB
testcase_14 AC 44 ms
19,540 KB
testcase_15 AC 9 ms
19,276 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 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]


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