結果

問題 No.1441 MErGe
ユーザー first_vilfirst_vil
提出日時 2021-03-11 03:09:32
言語 Kotlin
(1.9.23)
結果
AC  
実行時間 945 ms / 1,000 ms
コード長 3,433 bytes
コンパイル時間 17,307 ms
コンパイル使用メモリ 451,084 KB
実行使用メモリ 68,944 KB
最終ジャッジ日時 2024-10-12 18:07:57
合計ジャッジ時間 40,260 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 288 ms
49,912 KB
testcase_01 AC 290 ms
49,984 KB
testcase_02 AC 294 ms
49,956 KB
testcase_03 AC 317 ms
49,992 KB
testcase_04 AC 369 ms
50,160 KB
testcase_05 AC 425 ms
58,020 KB
testcase_06 AC 423 ms
58,008 KB
testcase_07 AC 446 ms
58,008 KB
testcase_08 AC 552 ms
61,860 KB
testcase_09 AC 562 ms
61,520 KB
testcase_10 AC 575 ms
57,000 KB
testcase_11 AC 655 ms
62,928 KB
testcase_12 AC 601 ms
56,420 KB
testcase_13 AC 683 ms
58,060 KB
testcase_14 AC 658 ms
58,400 KB
testcase_15 AC 697 ms
58,564 KB
testcase_16 AC 675 ms
58,080 KB
testcase_17 AC 690 ms
61,988 KB
testcase_18 AC 945 ms
68,944 KB
testcase_19 AC 777 ms
65,212 KB
testcase_20 AC 938 ms
66,376 KB
testcase_21 AC 756 ms
67,676 KB
testcase_22 AC 829 ms
63,652 KB
testcase_23 AC 859 ms
59,940 KB
testcase_24 AC 835 ms
60,084 KB
testcase_25 AC 803 ms
60,016 KB
testcase_26 AC 829 ms
60,184 KB
testcase_27 AC 742 ms
60,004 KB
testcase_28 AC 467 ms
54,356 KB
testcase_29 AC 472 ms
54,392 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