結果

問題 No.2080 Simple Nim Query
ユーザー 👑 箱星箱星
提出日時 2022-09-27 09:33:47
言語 Kotlin
(1.9.23)
結果
AC  
実行時間 1,154 ms / 3,000 ms
コード長 2,342 bytes
コンパイル時間 12,202 ms
コンパイル使用メモリ 422,880 KB
実行使用メモリ 79,188 KB
最終ジャッジ日時 2023-09-03 02:50:54
合計ジャッジ時間 22,016 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 243 ms
54,328 KB
testcase_01 AC 236 ms
52,392 KB
testcase_02 AC 258 ms
53,040 KB
testcase_03 AC 620 ms
62,172 KB
testcase_04 AC 611 ms
62,104 KB
testcase_05 AC 1,154 ms
79,188 KB
testcase_06 AC 1,059 ms
63,304 KB
testcase_07 AC 971 ms
64,836 KB
testcase_08 AC 1,031 ms
62,904 KB
testcase_09 AC 1,131 ms
70,960 KB
testcase_10 AC 1,046 ms
69,508 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.PrintWriter
import java.util.*
import kotlin.math.*

fun PrintWriter.solve() {
    val n = nextInt()
    val q = nextInt()
    val a = Array(n) { nextInt() }
    val bit = BIT(n)
    for (i in 0 until n) {
        if (a[i] == 1) {
            bit.add(i, 1)
        }
    }
    for (_i in 0 until q) {
        val t = nextInt()
        val x = nextInt()
        val y = nextInt()
        if (t == 1) {
            if (a[x - 1] == 1) {
                bit.add(x - 1, -1)
            }
            a[x - 1] = y
            if (y == 1) {
                bit.add(x - 1, 1)
            }
        } else {
            var l = x - 1
            var r = y - 1
            while (r - l > 1) {
                val mid = (l + r) / 2
                val sum = bit.sum(mid..r)
                val len = r - mid + 1L
                if (sum == len) {
                    r = mid
                } else {
                    l = mid
                }
            }
            val count = if (a[r] != 1) 0 else if (a[l] == 1) y - x + 2 else y - 1 - l
            println(if (count % 2 == 0) "F" else "S")
        }
    }
}

class BIT(private val n: Int) {
    private val data = LongArray(n) { 0 }

    fun add(i: Int, x: Long) {
        var p = i + 1
        while (p <= n) {
            data[p - 1] += x
            p += p and -p
        }
    }

    fun sum(range: IntRange): Long {
        val l = range.first
        val r = range.last + 1
        return sum(r) - sum(l)
    }

    private fun sum(i: Int): Long {
        var r = i
        var s = 0L
        while(r > 0) {
            s += data[r - 1]
            r -= r and -r
        }
        return s
    }
}

fun main() {
    Thread(null, {
        val writer = PrintWriter(System.out, false)
        writer.solve()
        writer.flush()
    }, "solve", abs(1L.shl(26)))
        .apply { setUncaughtExceptionHandler { _, e -> e.printStackTrace(); kotlin.system.exitProcess(1) } }
        .apply { start() }.join()
}

// region Scanner
private var st = StringTokenizer("")
private val br = System.`in`.bufferedReader()

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()
// endregion
0