結果

問題 No.1074 増殖
ユーザー yakamotoyakamoto
提出日時 2020-06-06 00:05:58
言語 Kotlin
(1.9.23)
結果
AC  
実行時間 1,115 ms / 2,000 ms
コード長 5,270 bytes
コンパイル時間 21,363 ms
コンパイル使用メモリ 441,736 KB
実行使用メモリ 67,812 KB
最終ジャッジ日時 2023-08-22 18:51:20
合計ジャッジ時間 34,018 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 309 ms
52,868 KB
testcase_01 AC 317 ms
52,824 KB
testcase_02 AC 308 ms
52,860 KB
testcase_03 AC 310 ms
52,812 KB
testcase_04 AC 314 ms
52,920 KB
testcase_05 AC 315 ms
52,900 KB
testcase_06 AC 839 ms
62,448 KB
testcase_07 AC 830 ms
63,132 KB
testcase_08 AC 847 ms
60,928 KB
testcase_09 AC 976 ms
61,056 KB
testcase_10 AC 719 ms
61,144 KB
testcase_11 AC 1,016 ms
65,660 KB
testcase_12 AC 1,022 ms
67,812 KB
testcase_13 AC 1,067 ms
67,432 KB
testcase_14 AC 1,115 ms
67,468 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Main.kt:21:11: warning: expected performance impact from inlining is insignificant. Inlining works best for functions with parameters of functional types
  private inline fun f(a: Int, b:Int): Int {
          ^
Main.kt:64:11: warning: expected performance impact from inlining is insignificant. Inlining works best for functions with parameters of functional types
  private inline fun contains(a: Int, x: Int): Boolean = a > x
          ^

ソースコード

diff #

import java.io.BufferedReader
import java.io.InputStream
import java.io.InputStreamReader
import java.util.*
import kotlin.math.abs
import kotlin.math.max
import kotlin.math.min

val MOD = 1_000_000_007L
val INF = 1e9.toInt()

/**
 * @param n 個数 最大値じゃないぞ。
 * iの範囲は[0, n - 1]
 *
 * AがIntやLongのときは埋め込んでしまおう
 * type A = Int
 */
class RangeUpdateTree(n: Int) {

  private inline fun f(a: Int, b:Int): Int {
    return min(a, b)
  }

  private val zero = INF

  private val N =
    if (Integer.highestOneBit(n) == n) n
    else Integer.highestOneBit(n) shl 1

  private val dat = IntArray(2 * N){zero}

//  private inline fun f(a: Int, b: Int) = a + b

  /**
   * [l, r)
   */
  fun add(l: Int, r: Int, a: Int) {
    var left = l + N
    var right = r - 1 + N

    while(left <= right) {
      if ((left and 1) == 1) dat[left] = f(dat[left], a)
      if ((right and 1) == 0) dat[right] = f(dat[right], a)
      left = (left + 1) shr  1 // 右の子供なら右の親に移動
      right = (right - 1) shr 1 // 左の子供なら左の親に移動
    }
  }

  fun query(i: Int): Int {
    var ix = N + i
    var res: Int = zero

    while(ix >= 1) {
      res = f(res, dat[ix])
      ix = ix shr 1
    }

    return res
  }


  // 条件を満たすか。maxなら '>' minなら '<' が使える
  private inline fun contains(a: Int, x: Int): Boolean = a > x

  /**
   * @param x 左右どちらからか最初に a < x になる場所を探す
   * @return -1
   */
  fun search(x: Int, k: Int = 1, l: Int = 0, r: Int = N): Int {
    if (!contains(dat[k], x)) return -1
    if (l + 1 == r) return l // 1-indexed

    val m = (l + r) / 2
    val lft = k * 2
    val rgt = lft + 1

    val ans = search(x, lft, l, m)
    if (ans != -1) return ans
    return search(x, rgt, m, r)
  }
}
data class Query(val i: Int, val x1: Int, val y1: Int, val x2: Int, val y2: Int)
data class Point(val i: Int, val x: Int, val y: Int)
class Solver(stream: InputStream, private val out: java.io.PrintWriter) {
  fun solve() {
    val N = ni()
    val Q = Array(N){Query(it, ni(), ni(), ni(), ni())}

    val ans = IntArray(N)
    fun calc(P: MutableList<Point>) {
      val t = RangeUpdateTree(20_000)
      P.sortWith(compareBy({-it.y}, {it.x}))
      for (p in P) {
        var i = p.i
        var x = t.search(i)
        debug{"i:$i x:$x"}
        while(x != -1 && x <= p.x) {
          val ni = t.query(x)
          if (ni == INF) {
            ans[p.i] += (p.x - x + 1) * p.y
            break
          }
          val nx = t.search(ni)
          debug{"i:$i ni:$ni x:$x nx:$nx"}
          if (nx != -1) {
            ans[ni] -= (min(nx, p.x + 1) - x) * p.y
            ans[p.i] += (min(nx, p.x + 1) - x) * p.y
          }
          i = ni
          x = nx
        }

        t.add(0, p.x + 1, p.i)
      }
    }

    calc(Q.map{Point(it.i, it.x2 - 1, it.y2)} as MutableList<Point>)
    calc(Q.map{Point(it.i, -it.x1 - 1, -it.y1)} as MutableList<Point>)
    calc(Q.map{Point(it.i, -it.x1 - 1, it.y2)} as MutableList<Point>)
    calc(Q.map{Point(it.i, it.x2 - 1, -it.y1)} as MutableList<Point>)

    for (i in 0 until N) {
      out.println(ans[i])
    }
  }













































  private val isDebug = try {
    // なんか本番でエラーでる
    System.getenv("MY_DEBUG") != null
  } catch (t: Throwable) {
    false
  }

  private var tokenizer: StringTokenizer? = null
  private val reader = BufferedReader(InputStreamReader(stream), 32768)
  private fun next(): String {
    while (tokenizer == null || !tokenizer!!.hasMoreTokens()) {
      tokenizer = StringTokenizer(reader.readLine())
    }
    return tokenizer!!.nextToken()
  }

  private fun ni() = next().toInt()
  private fun nl() = next().toLong()
  private fun ns() = next()
  private fun na(n: Int, offset: Int = 0): IntArray {
    return map(n) { ni() + offset }
  }
  private fun nal(n: Int, offset: Int = 0): LongArray {
    val res = LongArray(n)
    for (i in 0 until n) {
      res[i] = nl() + offset
    }
    return res
  }

  private fun na2(n: Int, offset: Int = 0): Array<IntArray> {
    val a  = Array(2){IntArray(n)}
    for (i in 0 until n) {
      for (e in a) {
        e[i] = ni() + offset
      }
    }
    return a
  }

  private inline fun map(n: Int, f: (Int) -> Int): IntArray {
    val res = IntArray(n)
    for (i in 0 until n) {
      res[i] = f(i)
    }
    return res
  }

  private inline fun debug(msg: () -> String) {
    if (isDebug) System.err.println(msg())
  }

  private fun debug(a: LongArray) {
    debug { a.joinToString(" ") }
  }

  private fun debug(a: IntArray) {
    debug { a.joinToString(" ") }
  }

  private fun debug(a: BooleanArray) {
    debug { a.map { if (it) 1 else 0 }.joinToString("") }
  }

  private fun debugDim(A: Array<LongArray>) {
    if (isDebug) {
      for (a in A) {
        debug(a)
      }
    }
  }
  private fun debugDim(A: Array<IntArray>) {
    if (isDebug) {
      for (a in A) {
        debug(a)
      }
    }
  }

  /**
   * 勝手にimport消されるのを防ぎたい
   */
  private fun hoge() {
    min(1, 2)
    max(1, 2)
    abs(-10)
  }
}

fun main() {
  val out = java.io.PrintWriter(System.out)
  Solver(System.`in`, out).solve()
  out.flush()
}
0