結果

問題 No.777 再帰的ケーキ
ユーザー ikdikd
提出日時 2018-12-24 10:26:10
言語 Nim
(2.0.2)
結果
AC  
実行時間 895 ms / 2,000 ms
コード長 1,340 bytes
コンパイル時間 3,686 ms
コンパイル使用メモリ 72,064 KB
実行使用メモリ 62,996 KB
最終ジャッジ日時 2023-09-13 21:54:22
合計ジャッジ時間 12,826 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,380 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,376 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 1 ms
4,384 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 1 ms
4,376 KB
testcase_16 AC 1 ms
4,380 KB
testcase_17 AC 2 ms
4,380 KB
testcase_18 AC 2 ms
4,380 KB
testcase_19 AC 2 ms
4,376 KB
testcase_20 AC 2 ms
4,380 KB
testcase_21 AC 6 ms
4,376 KB
testcase_22 AC 6 ms
4,380 KB
testcase_23 AC 3 ms
4,376 KB
testcase_24 AC 3 ms
4,376 KB
testcase_25 AC 6 ms
4,376 KB
testcase_26 AC 6 ms
4,376 KB
testcase_27 AC 4 ms
4,376 KB
testcase_28 AC 895 ms
62,380 KB
testcase_29 AC 892 ms
62,996 KB
testcase_30 AC 888 ms
62,784 KB
testcase_31 AC 894 ms
61,980 KB
testcase_32 AC 446 ms
61,312 KB
testcase_33 AC 154 ms
25,092 KB
testcase_34 AC 216 ms
36,536 KB
testcase_35 AC 717 ms
49,828 KB
testcase_36 AC 518 ms
61,920 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import strutils, sequtils, algorithm, tables

type SegmentTree = object
  n: int
  dat: seq[int64]
proc init(sz: int): SegmentTree =
  var n = 1
  while n<sz: n*=2
  var dat = newSeqWith(n*2-1, 0'i64)
  result = SegmentTree(n: n, dat: dat)
proc update(this: var SegmentTree, i: int, x: int64) =
  var k = i+this.n-1
  if this.dat[k]<x:
    this.dat[k] = x
    while k>0:
      k = (k-1) div 2
      this.dat[k] = max(this.dat[k*2+1], this.dat[k*2+2])
proc rmax(this: SegmentTree, ql, qr, i, il, ir: int): int64 =
  if qr<=il or ir<=ql:
    result = 0
  elif ql<=il and ir<=qr:
    result = this.dat[i]
  else:
    let m = (il+ir) div 2
    result = max(
      this.rmax(ql, qr, i*2+1, il, m), this.rmax(ql, qr, i*2+2, m, ir))

proc main() =
  let n = stdin.readLine.parseInt
  var abcs = (0..<n).mapIt(stdin.readLine.split.map(parseInt))
  abcs.sort(proc(x, y: seq[int]): int = cmp(x[1], y[1]))
  var map = newTable[int64, int]()
  for abc in abcs:
    if not map.hasKey(abc[1]):
      map[abc[1]] = map.len
  abcs.sort do (x, y: seq[int])->int:
    result = cmp(y[0], x[0])
    if result==0:
      result = cmp(x[1], y[1])
  let m = map.len
  var
    tree = init(m)
    ans = 0'i64
  for i, abc in abcs:
    let s = tree.rmax(map[abc[1]]+1, m, 0, 0, tree.n) + abc[2]
    ans = max(ans, s)
    tree.update(map[abc[1]], s)
  echo ans
main()
0