結果

問題 No.1234 典型RMQ
ユーザー 箱星箱星
提出日時 2020-10-09 03:42:05
言語 Kotlin
(1.9.23)
結果
AC  
実行時間 1,852 ms / 2,000 ms
コード長 6,356 bytes
コンパイル時間 19,745 ms
コンパイル使用メモリ 467,236 KB
実行使用メモリ 106,776 KB
最終ジャッジ日時 2024-11-09 02:32:18
合計ジャッジ時間 59,150 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 313 ms
50,176 KB
testcase_01 AC 316 ms
50,152 KB
testcase_02 AC 315 ms
50,280 KB
testcase_03 AC 305 ms
50,212 KB
testcase_04 AC 311 ms
50,276 KB
testcase_05 AC 310 ms
50,324 KB
testcase_06 AC 1,759 ms
105,012 KB
testcase_07 AC 1,372 ms
88,292 KB
testcase_08 AC 1,802 ms
106,228 KB
testcase_09 AC 1,586 ms
96,708 KB
testcase_10 AC 1,852 ms
105,316 KB
testcase_11 AC 1,747 ms
104,396 KB
testcase_12 AC 1,508 ms
95,736 KB
testcase_13 AC 1,360 ms
88,692 KB
testcase_14 AC 1,554 ms
96,300 KB
testcase_15 AC 1,529 ms
94,608 KB
testcase_16 AC 1,721 ms
104,336 KB
testcase_17 AC 1,541 ms
95,960 KB
testcase_18 AC 1,136 ms
85,808 KB
testcase_19 AC 1,810 ms
106,776 KB
testcase_20 AC 1,170 ms
102,736 KB
testcase_21 AC 1,675 ms
103,872 KB
testcase_22 AC 1,364 ms
97,848 KB
testcase_23 AC 1,371 ms
98,128 KB
testcase_24 AC 1,379 ms
97,804 KB
testcase_25 AC 1,400 ms
97,876 KB
testcase_26 AC 1,448 ms
97,548 KB
testcase_27 AC 306 ms
50,248 KB
testcase_28 AC 308 ms
50,336 KB
testcase_29 AC 317 ms
50,076 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.BufferedReader
import java.io.InputStream
import java.io.InputStreamReader
import java.io.PrintWriter
import java.util.*

class LazySegTree(private val n: Int, private val op: (Long, Long) -> Long, private val e: Long, private val mapping: (Long, Long) -> Long, private val composition: (Long, Long) -> Long, private val id: Long) : Iterable<Long> {
    private var d: MutableList<Long>
    private var lz: MutableList<Long>
    private var size = 1
    private var log = 0

    init {
        while (size < n) {
            size *= 2
            log++
        }
        d = MutableList(2 * size) { e }
        lz = MutableList(size) { id }
    }

    constructor(n: Int, op: (Long, Long) -> Long, e: Long, mapping: (Long, Long) -> Long, composition: (Long, Long) -> Long, id: Long, a: Array<Long>) : this(n, op, e, mapping, composition, id) {
        for (i in 0 until n) {
            d[size + i] = a[i]
        }
        for (i in size - 1 downTo 1) {
            update(i)
        }
    }

    operator fun set(p: Int, x: Long) {
        if (p !in 0 until n) {
            throw Exception()
        }
        val p1 = p + size
        for (i in log downTo 1) {
            push(p1 shr i)
        }
        d[p1] = x
        for (i in 1..log) {
            update(p1 shr i)
        }
    }

    operator fun get(p: Int): Long {
        if (p !in 0 until n) {
            throw Exception()
        }
        val p1 = p + size
        for (i in log downTo 1) {
            push(p1 shr i)
        }
        return d[p1]
    }

    fun prod(l: Int, r: Int): Long {
        if (l !in 0..r || r !in l..n) {
            throw Exception()
        }
        if (l == r) return e
        var l1 = l + size
        var r1 = r + size
        for (i in log downTo 1) {
            if ((l1 shr i) shl i != l1) push(l1 shr i)
            if ((r1 shr i) shl i != r1) push(r1 shr i)
        }
        var sml = e
        var smr = e
        while (l1 < r1) {
            if (l1 and 1 != 0) sml = op(sml, d[l1++])
            if (r1 and 1 != 0) smr = op(d[--r1], smr)
            l1 /= 2
            r1 /= 2
        }
        return op(sml, smr)
    }

    fun allProd(): Long {
        return d[1]
    }

    fun apply(p: Int, f: Long) {
        if (p !in 0 until n) {
            throw Exception()
        }
        val p1 = p + size
        for (i in log downTo 1) {
            push(p1 shr i)
        }
        d[p1] = mapping(f, d[p1])
        for (i in 1..log) {
            update(p1 shr i)
        }
    }

    fun apply(l: Int, r: Int, f: Long) {
        if (l !in 0..r || r !in l..n) {
            throw Exception()
        }
        if (l == r) return
        var l1 = l + size
        var r1 = r + size
        for (i in log downTo 1) {
            if ((l1 shr i) shl i != l1) push(l1 shr i)
            if ((r1 shr i) shl i != r1) push((r1 - 1) shr i)
        }
        val l2 = l1
        val r2 = r1
        while (l1 < r1) {
            if (l1 and 1 != 0) allApply(l1++, f)
            if (r1 and 1 != 0) allApply(--r1, f)
            l1 /= 2
            r1 /= 2
        }
        l1 = l2
        r1 = r2
        for (i in 1..log) {
            if ((l1 shr i) shl i != l1) update(l1 shr i)
            if ((r1 shr i) shl i != r1) update((r1 - 1) shr i)
        }
    }

    fun maxRight(l: Int, g: (Long) -> Boolean): Int {
        if (l !in 0..n || !g(e)) {
            throw Exception()
        }
        if (l == n) return n
        var l1 = l + size
        for (i in log downTo 1) push(l1 shr i)
        var sm = e
        do {
            while (l1 % 2 == 0) l1 /= 2
            if (!g(op(sm, d[l1]))) {
                while (l1 < size) {
                    push(l1)
                    l1 *= 2
                    if (g(op(sm, d[l1]))) {
                        sm = op(sm, d[l1])
                        l1++
                    }
                }
                return l1 - size
            }
            sm = op(sm, d[l1])
            l1++
        } while ((l1 and -l1) != l1)
        return n
    }

    fun minLeft(r: Int, g: (Long) -> Boolean): Int {
        if (r !in 0..n || !g(e)) {
            throw Exception()
        }
        if (r == 0) return 0
        var r1 = r + size
        for (i in log downTo 1) push((r1 - 1) shr i)
        var sm = e
        do {
            r1--
            while (r1 > 1 && r1 % 2 != 0) r1 /= 2
            if (!g(op(d[r1], sm))) {
                while (r1 < size) {
                    push(r1)
                    r1 = 2 * r1 + 1
                    if (g(op(d[r1], sm))) {
                        sm = op(d[r1], sm)
                        r1--
                    }
                }
                return r1 + 1 - size
            }
            sm = op(d[r1], sm)
        } while ((r1 and -r1) != r1)
        return 0
    }

    private fun update(k: Int) {
        d[k] = op(d[2 * k], d[2 * k + 1])
    }

    private fun allApply (k: Int, f: Long) {
        d[k] = mapping(f, d[k])
        if (k < size) {
            lz[k] = composition(f, lz[k])
        }
    }

    private fun push(k: Int) {
        allApply(2 * k, lz[k])
        allApply(2 * k + 1, lz[k])
        lz[k] = id
    }

    override fun iterator(): Iterator<Long> {
        return (0 until n).map { this[it] }.iterator()
    }
}

fun PrintWriter.solve(sc: FastScanner) {
    val n = sc.nextInt()
    val a = Array(n) { sc.nextLong() }
    val seg = LazySegTree(n, ::minOf, Long.MAX_VALUE, { x, y -> x + y }, { x, y -> x + y }, 0L, a)
    val q = sc.nextInt()
    for (_i in 0 until q) {
        val k = sc.nextInt()
        val l = sc.nextInt() - 1
        val r = sc.nextInt()
        val c = sc.nextLong()
        if (k == 1) {
            seg.apply(l, r, c)
        } else {
            println(seg.prod(l, r))
        }
    }
}

fun main() {
    val writer = PrintWriter(System.out, false)
    writer.solve(FastScanner(System.`in`))
    writer.flush()
}

class FastScanner(s: InputStream) {
    private var st = StringTokenizer("")
    private val br = BufferedReader(InputStreamReader(s))

    fun next(): String {
        while (!st.hasMoreTokens()) st = StringTokenizer(br.readLine())

        return st.nextToken()
    }

    fun nextInt() = next().toInt()
    fun nextLong() = next().toLong()
    fun nextLine() = br.readLine()
    fun nextDouble() = next().toDouble()
    fun ready() = br.ready()
}
0