結果

問題 No.1826 Fruits Collecting
コンテスト
ユーザー yudedako
提出日時 2022-01-31 16:08:20
言語 Scala(Beta)
(3.8.1)
コンパイル:
scalac _filename_
実行:
java -cp .:/home/linuxbrew/.linuxbrew/Cellar/scala/3.8.1/libexec/maven2/org/scala-lang/scala3-library_3/3.8.1/scala3-library_3-3.8.1.jar:/home/linuxbrew/.linuxbrew/Cellar/scala/3.8.1/libexec/maven2/org/scala-lang/scala-library/3.8.1/scala-library-3.8.1.jar _class_
結果
AC  
(最新)
TLE  
(最初)
実行時間 1,531 ms / 2,000 ms
コード長 1,482 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 12,543 ms
コンパイル使用メモリ 284,084 KB
実行使用メモリ 101,148 KB
最終ジャッジ日時 2026-03-09 19:54:26
合計ジャッジ時間 59,179 ms
ジャッジサーバーID
(参考情報)
judge1_0 / judge3_1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 43
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

import scala.collection.mutable
import scala.collection.mutable.ArrayBuffer
import scala.io.StdIn.*
import scala.math.*
import scala.reflect.ClassTag
import scala.util.chaining.*


class BinaryIndexedTree(val size: Int):
  private val vec = Array.fill(size){0L}
  def get(position: Int): Long =
    var result = 0L
    var pos = position
    while vec.indices.contains(pos) do
      result = max(result, vec(pos))
      pos -= ~pos & (pos + 1)
    result
  def add(position: Int, value: Long): Unit =
    var pos = position
    while vec.indices.contains(pos) do
      vec(pos) = max(vec(pos), value)
      pos += ~pos & (pos + 1)
  def getAll(): Long = get(size - 1)

extension (array: Array[Int])
  def lowerBound(value: Int): Int =
    var min = 0
    var max = array.length
    while min < max do
      val mid = (min + max) >>> 1
      if array(mid) < value then
        min = mid + 1
      else
        max = mid
    max

@main def main =
  val n = readLine().toInt
  val fruits = Array.fill(n){
    val Array(t, x, v) = readLine().split(' ').map(_.toInt)
    (t, x, v)
  }
  val diff = fruits.map{(t, x, _) => t - x}.distinct.sorted
  val bit = BinaryIndexedTree(diff.length)
  for (t, x, v) <- fruits.sorted(using Ordering.by[(Int, Int, Int), Int]{(t, x, _) => t + x}.orElseBy{(t, _, _) => t}) do
    if t >= abs(x) then
      val di = diff.lowerBound(t - x)
      val maxValue = bit.get(di) + v
      bit.add(di, maxValue)
  val result = bit.getAll()
  println(result)



0