結果

問題 No.1074 増殖
ユーザー yakamotoyakamoto
提出日時 2020-06-06 00:05:58
言語 Kotlin
(1.9.23)
結果
AC  
実行時間 1,086 ms / 2,000 ms
コード長 5,270 bytes
コンパイル時間 18,222 ms
コンパイル使用メモリ 470,264 KB
実行使用メモリ 92,324 KB
最終ジャッジ日時 2024-05-10 00:38:05
合計ジャッジ時間 30,069 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 330 ms
52,652 KB
testcase_01 AC 332 ms
52,684 KB
testcase_02 AC 327 ms
52,424 KB
testcase_03 AC 329 ms
52,596 KB
testcase_04 AC 331 ms
52,664 KB
testcase_05 AC 331 ms
52,684 KB
testcase_06 AC 820 ms
87,904 KB
testcase_07 AC 819 ms
88,064 KB
testcase_08 AC 844 ms
88,284 KB
testcase_09 AC 954 ms
89,300 KB
testcase_10 AC 674 ms
85,272 KB
testcase_11 AC 968 ms
92,316 KB
testcase_12 AC 1,048 ms
92,324 KB
testcase_13 AC 1,073 ms
92,104 KB
testcase_14 AC 1,086 ms
92,000 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