結果

問題 No.1234 典型RMQ
ユーザー yakamotoyakamoto
提出日時 2020-09-22 19:34:28
言語 Kotlin
(1.9.23)
結果
AC  
実行時間 1,351 ms / 2,000 ms
コード長 5,442 bytes
コンパイル時間 17,130 ms
コンパイル使用メモリ 455,948 KB
実行使用メモリ 96,880 KB
最終ジャッジ日時 2024-04-26 11:35:32
合計ジャッジ時間 48,092 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 279 ms
50,352 KB
testcase_01 AC 280 ms
50,260 KB
testcase_02 AC 282 ms
50,344 KB
testcase_03 AC 278 ms
50,332 KB
testcase_04 AC 279 ms
50,228 KB
testcase_05 AC 285 ms
50,332 KB
testcase_06 AC 1,323 ms
95,472 KB
testcase_07 AC 1,215 ms
88,260 KB
testcase_08 AC 1,351 ms
96,788 KB
testcase_09 AC 1,255 ms
92,080 KB
testcase_10 AC 1,339 ms
96,396 KB
testcase_11 AC 1,271 ms
95,720 KB
testcase_12 AC 1,302 ms
91,888 KB
testcase_13 AC 1,173 ms
87,556 KB
testcase_14 AC 1,320 ms
91,680 KB
testcase_15 AC 1,218 ms
91,956 KB
testcase_16 AC 1,346 ms
96,764 KB
testcase_17 AC 1,224 ms
91,916 KB
testcase_18 AC 993 ms
86,908 KB
testcase_19 AC 1,346 ms
96,548 KB
testcase_20 AC 969 ms
95,384 KB
testcase_21 AC 1,333 ms
96,324 KB
testcase_22 AC 1,135 ms
96,752 KB
testcase_23 AC 1,122 ms
96,660 KB
testcase_24 AC 1,116 ms
96,880 KB
testcase_25 AC 1,104 ms
95,604 KB
testcase_26 AC 1,178 ms
96,676 KB
testcase_27 AC 280 ms
50,356 KB
testcase_28 AC 295 ms
50,284 KB
testcase_29 AC 277 ms
50,388 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Main.kt:34:11: warning: expected performance impact from inlining is insignificant. Inlining works best for functions with parameters of functional types
  private inline fun push(k: Int) {
          ^
Main.kt:101:29: warning: parameter 'num' is never used
  private fun plus(a: Long, num: Int, b: Long) = run {
                            ^
Main.kt:213:11: warning: expected performance impact from inlining is insignificant. Inlining works best for functions with parameters of functional types
  private inline fun debug(a: LongArray) {
          ^
Main.kt:217:11: warning: expected performance impact from inlining is insignificant. Inlining works best for functions with parameters of functional types
  private inline fun debug(a: IntArray) {
          ^
Main.kt:221:11: warning: expected performance impact from inlining is insignificant. Inlining works best for functions with parameters of functional types
  private inline fun debug(a: BooleanArray) {
          ^
Main.kt:225:11: warning: expected performance impact from inlining is insignificant. Inlining works best for functions with parameters of functional types
  private inline fun toString(a: BooleanArray) = run{a.map { if (it) 1 else 0 }.joinToString("")}
          ^
Main.kt:227:11: warning: expected performance impact from inlining is insignificant. Inlining works best for functions with parameters of functional types
  private inline fun debugDim(A: Array<LongArray>) {
          ^
Main.kt:234:11: warning: expected performance impact from inlining is insignificant. Inlining works best for functions with parameters of functional types
  private inline fun debugDim(A: Array<IntArray>) {
          ^
Main.kt:241:11: warning: expected performance impact from inlining is insignificant. Inlining works best for functions with parameters of functional types
  private inline fun debugDim(A: Array<BooleanArray>) {
          ^
Main.kt:258:11: warning: expected performance impact from inlining is insignificant. Inlining works

ソースコード

diff #

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

val MOD = 1_000_000_007

/**
 * @param merge ツリーを↑に上がっていくときのマージ関数 todo merge(a, a) = a でない場合要素数いるんじゃない?
 * @param plus addでノードを更新する関数 (昔の値, 要素数, 引数)
 */
class DelayMergeTree2(n: Int,
                      val mZero: Long, val merge: (Long, Long) -> Long,
                      val pZero: Long, val plus: (Long, Int, Long) -> Long) {
  private val N =
    if (Integer.highestOneBit(n) == n) n
    else Integer.highestOneBit(n) shl 1

  private val value = LongArray(N * 2){mZero}
  private val delay = LongArray(N * 2){pZero}
  private val elms = IntArray(N * 2)
  init {
    elms[1] = N
    for (i in 1 until N) {
      elms[2 * i] = elms[i] / 2
      elms[2 * i + 1] = elms[i] / 2
    }
  }

  private inline fun push(k: Int) {
    if (k < N && delay[k] != pZero) {
      value[k * 2] = plus(value[k * 2], elms[k * 2], delay[k])
      value[k * 2 + 1] = plus(value[k * 2 + 1], elms[k * 2 + 1], delay[k])
      delay[k * 2] = plus(delay[k * 2], elms[k * 2], delay[k])
      delay[k * 2 + 1] = plus(delay[k * 2 + 1], elms[k * 2 + 1], delay[k])
      delay[k] = pZero
    }
  }

  /**
   * [a, b)
   */
  fun add(a: Int, b: Int, x: Long, k: Int = 1, l: Int = 0, r: Int = N) {
    if (a >= r || l >= b) return // ノードが範囲からはずれてる
    if (a <= l && r <= b) { // ノードが完全に範囲に含まれる
      value[k] = plus(value[k], elms[k], x)
      delay[k] = plus(delay[k], elms[k], x)
      return
    }

    push(k)
    val m = (l + r) / 2
    val lft = k * 2
    val rgt = lft + 1
    add(a, b, x, lft, l, m)
    add(a, b, x, rgt, m, r)
    value[k] = merge(value[lft], value[rgt])
  }

  /**
   * [a, b)
   */
  fun query(a: Int, b: Int, k: Int = 1, l: Int = 0, r: Int = N): Long {
    if (a >= r || l >= b) return mZero // ノードが範囲からはずれてる
    if (a <= l && r <= b) { // ノードが完全に範囲に含まれる
      return value[k]
    }

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

    return merge(query(a, b, lft, l, m), query(a, b, rgt, m, r))
  }

  fun eval(i: Int): Long {
    _eval(N + i)
    return value[N + i]
  }

  fun inspect() = run { Pair(value, delay) }

  fun _eval(k: Int) {
    if (k > 1) {
      _eval(k / 2)
    }
    push(k)
  }
}
class Solver(stream: InputStream, private val out: java.io.PrintWriter) {
  private val reader = BufferedReader(InputStreamReader(stream), 32768)

  private val N = ni()

  private val INF = 1e18.toLong()
  private fun plus(a: Long, num: Int, b: Long) = run {
    if (a == INF) b else a + b
  }
  private val tree = DelayMergeTree2(N, INF, ::min, 0, ::plus)
  fun solve() {
    for (i in 0 until N) {
      tree.add(i, i + 1, nl())
    }

    for (q in 0 until ni()) {
      val type = ni()
      val l = ni() - 1
      val r = ni() - 1
      val c = nl()
      when(type) {
        1 -> {
          tree.add(l, r + 1, c)
        }
        else -> {
          out.println(tree.query(l, r + 1))
        }
      }
    }
  }













































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

  private var tokenizer: StringTokenizer? = null
  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 IntArray(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 debug(msg: () -> String) {
    if (isDebug) System.err.println(msg())
  }

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

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

  private inline fun debug(a: BooleanArray) {
    debug { toString(a) }
  }

  private inline fun toString(a: BooleanArray) = run{a.map { if (it) 1 else 0 }.joinToString("")}

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

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

  private inline fun assert(b: Boolean) = run{if (!b) throw AssertionError()}
}

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