結果

問題 No.1441 MErGe
ユーザー first_vilfirst_vil
提出日時 2021-03-11 03:09:32
言語 Kotlin
(1.9.23)
結果
AC  
実行時間 905 ms / 1,000 ms
コード長 3,433 bytes
コンパイル時間 16,988 ms
コンパイル使用メモリ 453,740 KB
実行使用メモリ 67,996 KB
最終ジャッジ日時 2024-04-20 20:07:47
合計ジャッジ時間 35,837 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 281 ms
49,760 KB
testcase_01 AC 277 ms
49,864 KB
testcase_02 AC 281 ms
50,112 KB
testcase_03 AC 296 ms
50,328 KB
testcase_04 AC 312 ms
50,388 KB
testcase_05 AC 399 ms
56,356 KB
testcase_06 AC 393 ms
56,676 KB
testcase_07 AC 402 ms
57,584 KB
testcase_08 AC 497 ms
61,844 KB
testcase_09 AC 515 ms
61,680 KB
testcase_10 AC 552 ms
58,864 KB
testcase_11 AC 561 ms
62,932 KB
testcase_12 AC 549 ms
56,468 KB
testcase_13 AC 663 ms
58,228 KB
testcase_14 AC 650 ms
58,128 KB
testcase_15 AC 682 ms
58,232 KB
testcase_16 AC 607 ms
58,060 KB
testcase_17 AC 705 ms
62,472 KB
testcase_18 AC 905 ms
66,560 KB
testcase_19 AC 705 ms
65,096 KB
testcase_20 AC 749 ms
67,996 KB
testcase_21 AC 665 ms
67,244 KB
testcase_22 AC 815 ms
64,208 KB
testcase_23 AC 731 ms
59,940 KB
testcase_24 AC 671 ms
60,168 KB
testcase_25 AC 708 ms
60,008 KB
testcase_26 AC 707 ms
60,288 KB
testcase_27 AC 658 ms
60,276 KB
testcase_28 AC 423 ms
54,184 KB
testcase_29 AC 440 ms
54,440 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Main.kt:107:24: warning: 'toByte(): Byte' is deprecated. Conversion of Char to Number is deprecated. Use Char.code property instead.
        while (b < '0'.toByte() || b > '9'.toByte()) b = readByte()
                       ^
Main.kt:107:44: warning: 'toByte(): Byte' is deprecated. Conversion of Char to Number is deprecated. Use Char.code property instead.
        while (b < '0'.toByte() || b > '9'.toByte()) b = readByte()
                                           ^
Main.kt:108:20: warning: 'toByte(): Byte' is deprecated. Conversion of Char to Number is deprecated. Use Char.code property instead.
        while ('0'.toByte() <= b && b <= '9'.toByte()) {
                   ^
Main.kt:108:46: warning: 'toByte(): Byte' is deprecated. Conversion of Char to Number is deprecated. Use Char.code property instead.
        while ('0'.toByte() <= b && b <= '9'.toByte()) {
                                             ^
Main.kt:110:26: warning: 'toByte(): Byte' is deprecated. Conversion of Char to Number is deprecated. Use Char.code property instead.
            n += b - '0'.toByte()
                         ^
Main.kt:120:25: warning: 'toByte(): Byte' is deprecated. Conversion of Char to Number is deprecated. Use Char.code property instead.
        while (b != '-'.toByte() && (b < '0'.toByte() || b > '9'.toByte())) b = readByte()
                        ^
Main.kt:120:46: warning: 'toByte(): Byte' is deprecated. Conversion of Char to Number is deprecated. Use Char.code property instead.
        while (b != '-'.toByte() && (b < '0'.toByte() || b > '9'.toByte())) b = readByte()
                                             ^
Main.kt:120:66: warning: 'toByte(): Byte' is deprecated. Conversion of Char to Number is deprecated. Use Char.code property instead.
        while (b != '-'.toByte() && (b < '0'.toByte() || b > '9'.toByte())) b = readByte()
                                                                 ^
Main.kt:121:22: warning: 'toByte(): Byte' is deprecated. Conversion of Cha

ソースコード

diff #

import java.io.PrintWriter
import java.util.*

fun PrintWriter.solve() {
    val scan = FastScanner()
    val n = scan.nextInt()
    val q = scan.nextInt()
    val a = LongArray(n) { scan.nextLong() }
    for (i in 1 until n) a[i] += a[i - 1]

    //BIT setup
    val dat = IntArray(n + 1) { 1 }
    for (i in 1..n) {
        if (i + (i and -i) <= n) dat[i + (i and -i)] += dat[i]
    }
    var n2 = 1
    while (n2 * 2 <= n) n2 *= 2

    repeat(q) {
        val t = scan.nextInt()
        val l = scan.nextInt()
        val r = scan.nextInt()
        if (t == 1) {
            repeat(r - l) {
                var j = 0
                var x = l + 1
                var k = n2
                while (0 < k) {
                    if (j + k <= n && x > dat[j + k]) {
                        x -= dat[j + k]
                        j += k
                    }
                    k /= 2
                }
                ++j
                while (j <= n) {
                    --dat[j]
                    j += j and -j
                }
            }
        } else {
            var L = 0
            var R = 0
            var x = l
            var y = r + 1
            var k = n2
            while (0 < k) {
                if (L + k <= n && x > dat[L + k]) {
                    x -= dat[L + k]
                    L += k
                }
                if (R + k <= n && y > dat[R + k]) {
                    y -= dat[R + k]
                    R += k
                }
                k /= 2
            }
            println(a[R - 1] - if (0 < L) a[L - 1] else { 0L })
        }
    }
}

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

private class FastScanner {
    private val printable = 33..126
    val `in` = System.`in`
    val buffer = ByteArray(65536)
    var ptr = 0
    var buflen = 0
    fun hasNextByte(): Boolean {
        if (ptr < buflen) {
            return true
        } else {
            ptr = 0
            buflen = `in`.read(buffer)
            if (buflen <= 0) {
                return false
            }
        }
        return true
    }
    fun readByte(): Byte {
        return if (hasNextByte()) buffer[ptr++] else return -1
    }
    fun hasNext(): Boolean {
        while (hasNextByte() && buffer[ptr] !in printable) ptr++
        return hasNextByte()
    }
    fun next(): String {
        val sb = StringBuilder()
        var b = readByte()
        while (b in printable) {
            sb.appendCodePoint(b.toInt())
            b = readByte()
        }
        return sb.toString()
    }
    fun nextInt(): Int {
        var n = 0
        var b = readByte()
        
        while (b < '0'.toByte() || b > '9'.toByte()) b = readByte()
        while ('0'.toByte() <= b && b <= '9'.toByte()) {
            n *= 10
            n += b - '0'.toByte()
            b = readByte()
        }
        return n
    }
    fun nextLong(): Long {
        var p = 1L
        var n = 0L
        var b = readByte()
        
        while (b != '-'.toByte() && (b < '0'.toByte() || b > '9'.toByte())) b = readByte()
        if (b == '-'.toByte()) {
            p = -1L
            b = readByte()
        }
        while ('0'.toByte() <= b && b <= '9'.toByte()) {
            n *= 10
            n += b - '0'.toByte()
            b = readByte()
        }
        return n * p
    }
    fun nextDouble(): Double {
        return next().toDouble()
    }
}
0