結果

問題 No.2080 Simple Nim Query
ユーザー 👑 箱星箱星
提出日時 2022-09-27 09:32:43
言語 Kotlin
(1.9.23)
結果
TLE  
実行時間 -
コード長 2,355 bytes
コンパイル時間 17,226 ms
コンパイル使用メモリ 422,620 KB
実行使用メモリ 87,844 KB
最終ジャッジ日時 2023-08-24 03:40:31
合計ジャッジ時間 25,486 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 278 ms
55,616 KB
testcase_01 AC 281 ms
52,484 KB
testcase_02 AC 303 ms
53,108 KB
testcase_03 AC 775 ms
61,820 KB
testcase_04 AC 786 ms
61,988 KB
testcase_05 TLE -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
権限があれば一括ダウンロードができます

ソースコード

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 = (mid..r).count().toLong()
                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