結果
| 問題 |
No.1234 典型RMQ
|
| コンテスト | |
| ユーザー |
yakamoto
|
| 提出日時 | 2020-09-22 19:34:28 |
| 言語 | Kotlin (2.1.0) |
| 結果 |
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 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 27 |
コンパイルメッセージ
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
ソースコード
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()
}
yakamoto