結果

問題 No.1234 典型RMQ
ユーザー yakamotoyakamoto
提出日時 2020-09-22 19:34:28
言語 Kotlin
(1.9.23)
結果
AC  
実行時間 1,512 ms / 2,000 ms
コード長 5,442 bytes
コンパイル時間 22,679 ms
コンパイル使用メモリ 459,836 KB
実行使用メモリ 96,976 KB
最終ジャッジ日時 2024-11-09 02:26:38
合計ジャッジ時間 53,913 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 304 ms
50,360 KB
testcase_01 AC 305 ms
50,256 KB
testcase_02 AC 308 ms
50,400 KB
testcase_03 AC 306 ms
50,284 KB
testcase_04 AC 305 ms
50,456 KB
testcase_05 AC 307 ms
50,300 KB
testcase_06 AC 1,371 ms
95,700 KB
testcase_07 AC 1,288 ms
87,424 KB
testcase_08 AC 1,447 ms
95,708 KB
testcase_09 AC 1,312 ms
92,092 KB
testcase_10 AC 1,491 ms
96,360 KB
testcase_11 AC 1,392 ms
95,560 KB
testcase_12 AC 1,343 ms
91,804 KB
testcase_13 AC 1,232 ms
87,584 KB
testcase_14 AC 1,354 ms
91,748 KB
testcase_15 AC 1,343 ms
91,772 KB
testcase_16 AC 1,512 ms
95,652 KB
testcase_17 AC 1,338 ms
91,896 KB
testcase_18 AC 1,058 ms
86,844 KB
testcase_19 AC 1,438 ms
95,988 KB
testcase_20 AC 1,024 ms
95,236 KB
testcase_21 AC 1,444 ms
95,472 KB
testcase_22 AC 1,203 ms
96,320 KB
testcase_23 AC 1,189 ms
96,608 KB
testcase_24 AC 1,193 ms
96,976 KB
testcase_25 AC 1,185 ms
96,356 KB
testcase_26 AC 1,195 ms
95,968 KB
testcase_27 AC 307 ms
50,252 KB
testcase_28 AC 311 ms
50,372 KB
testcase_29 AC 311 ms
50,208 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