結果

問題 No.777 再帰的ケーキ
ユーザー ikdikd
提出日時 2020-05-03 10:49:06
言語 Nim
(2.0.2)
結果
AC  
実行時間 484 ms / 2,000 ms
コード長 1,817 bytes
コンパイル時間 3,574 ms
コンパイル使用メモリ 71,688 KB
実行使用メモリ 29,656 KB
最終ジャッジ日時 2023-09-02 09:14:45
合計ジャッジ時間 8,788 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 1 ms
4,376 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 1 ms
4,380 KB
testcase_16 AC 1 ms
4,384 KB
testcase_17 AC 2 ms
4,376 KB
testcase_18 AC 2 ms
4,380 KB
testcase_19 AC 2 ms
4,380 KB
testcase_20 AC 2 ms
4,376 KB
testcase_21 AC 5 ms
4,376 KB
testcase_22 AC 5 ms
4,380 KB
testcase_23 AC 3 ms
4,380 KB
testcase_24 AC 3 ms
4,376 KB
testcase_25 AC 5 ms
4,376 KB
testcase_26 AC 5 ms
4,376 KB
testcase_27 AC 3 ms
4,376 KB
testcase_28 AC 463 ms
28,360 KB
testcase_29 AC 446 ms
28,408 KB
testcase_30 AC 477 ms
29,656 KB
testcase_31 AC 484 ms
28,356 KB
testcase_32 AC 306 ms
28,560 KB
testcase_33 AC 103 ms
13,648 KB
testcase_34 AC 145 ms
17,212 KB
testcase_35 AC 394 ms
21,920 KB
testcase_36 AC 322 ms
29,480 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import strutils, algorithm, sequtils, math, sugar

type SegmentTree[T] = object
  n: int
  dat: seq[T]
  e: T
  multiply: proc(a, b: T): T

proc initSegmentTree[T](n: int, e: T, f: proc(a, b: T): T): SegmentTree[T] =
  let nn = nextPowerOfTwo(n)
  var dat = newSeqWith(nn * 2 - 1, e)
  return SegmentTree[T](n: nn, dat: dat, e: e, multiply: f)

proc get[T](this: SegmentTree[T], i: int): T =
  return this.dat[i + this.n - 1]

proc update[T](this: var SegmentTree[T], i: int, x: T) =
  var k = i + this.n - 1
  this.dat[k] = x
  while k > 0:
    k = (k - 1) div 2
    this.dat[k] = this.multiply(this.dat[k * 2 + 1], this.dat[k * 2 + 2])

proc query[T](this: SegmentTree[T], ql, qr, i, il, ir: int): T =
  if ql <= il and ir <= qr:
    return this.dat[i]
  if qr <= il or ir <= ql:
    return this.e
  let m = (il + ir) div 2
  return this.multiply(
    this.query(ql, qr, i * 2 + 1, il, m),
    this.query(ql, qr, i * 2 + 2, m, ir))

proc query[T](this: SegmentTree[T], ql, qr: int): T =
  return this.query(ql, qr, 0, 0, this.n)

let read = iterator: string {.closure.} =
  while true:
    for s in stdin.readLine.split:
      yield s

proc main() =
  let n = read().parseInt
  type P = tuple[a, b, c: int]
  var cakes = newSeq[P]()
  for i in 0..<n:
    let a, b, c = read().parseInt
    cakes.add((a, b, c))

  var bs = newSeq[int]()
  for p in cakes:
    bs.add(p.b)
  bs.sort(cmp)
  bs = bs.deduplicate(isSorted = true)
  proc map(b: int): int =
    return bs.lowerBound(b)
  let m = bs.len
  var seg = initSegmentTree[int64](
    m,
    0.int64,
    proc(x, y: int64): int64 = max(x, y))
  cakes.sort((p, q: P) => (if p.a == q.a: cmp(p.b, q.b) else: cmp(q.a, p.a)))
  for p in cakes:
    let i = map(p.b)
    let h = seg.query(i + 1, m)
    seg.update(i, max(seg.get(i), h + p.c))
  echo seg.query(0, m)
main()
0