結果

問題 No.1705 Mode of long array
ユーザー 👑 箱
提出日時 2021-10-08 23:29:59
言語 Kotlin
(1.9.10)
結果
AC  
実行時間 891 ms / 3,000 ms
コード長 4,506 bytes
コンパイル時間 19,377 ms
コンパイル使用メモリ 446,016 KB
実行使用メモリ 73,060 KB
最終ジャッジ日時 2023-09-30 13:37:16
合計ジャッジ時間 60,272 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 274 ms
54,572 KB
testcase_01 AC 280 ms
52,924 KB
testcase_02 AC 273 ms
52,856 KB
testcase_03 AC 377 ms
57,432 KB
testcase_04 AC 319 ms
53,304 KB
testcase_05 AC 380 ms
57,276 KB
testcase_06 AC 421 ms
57,124 KB
testcase_07 AC 429 ms
57,976 KB
testcase_08 AC 431 ms
59,852 KB
testcase_09 AC 412 ms
59,292 KB
testcase_10 AC 423 ms
59,272 KB
testcase_11 AC 420 ms
57,260 KB
testcase_12 AC 427 ms
59,000 KB
testcase_13 AC 795 ms
71,464 KB
testcase_14 AC 707 ms
62,752 KB
testcase_15 AC 702 ms
60,560 KB
testcase_16 AC 739 ms
62,520 KB
testcase_17 AC 674 ms
59,008 KB
testcase_18 AC 649 ms
60,476 KB
testcase_19 AC 759 ms
60,520 KB
testcase_20 AC 673 ms
60,780 KB
testcase_21 AC 756 ms
68,524 KB
testcase_22 AC 831 ms
70,836 KB
testcase_23 AC 619 ms
58,476 KB
testcase_24 AC 612 ms
58,468 KB
testcase_25 AC 612 ms
60,728 KB
testcase_26 AC 608 ms
59,048 KB
testcase_27 AC 612 ms
59,692 KB
testcase_28 AC 604 ms
60,388 KB
testcase_29 AC 611 ms
59,020 KB
testcase_30 AC 608 ms
58,620 KB
testcase_31 AC 616 ms
60,568 KB
testcase_32 AC 602 ms
60,272 KB
testcase_33 AC 881 ms
71,084 KB
testcase_34 AC 867 ms
73,060 KB
testcase_35 AC 864 ms
72,836 KB
testcase_36 AC 885 ms
71,288 KB
testcase_37 AC 886 ms
72,632 KB
testcase_38 AC 880 ms
71,196 KB
testcase_39 AC 877 ms
72,672 KB
testcase_40 AC 859 ms
72,792 KB
testcase_41 AC 891 ms
72,980 KB
testcase_42 AC 867 ms
70,736 KB
testcase_43 AC 752 ms
68,780 KB
testcase_44 AC 755 ms
68,760 KB
testcase_45 AC 748 ms
69,028 KB
testcase_46 AC 740 ms
70,576 KB
testcase_47 AC 740 ms
70,732 KB
testcase_48 AC 748 ms
69,700 KB
testcase_49 AC 831 ms
68,868 KB
testcase_50 AC 821 ms
70,972 KB
testcase_51 AC 835 ms
71,060 KB
testcase_52 AC 826 ms
68,912 KB
testcase_53 AC 848 ms
70,732 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Main.kt:6:9: warning: variable 'n' is never used
    val n = nextLong()
        ^

ソースコード

diff #

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

fun PrintWriter.solve() {
    val n = nextLong()
    val m = nextInt()
    val a = Array(m) { nextLong() }
    val b = Array(m) { 0L to 0 }
    for (i in 0 until m) {
        b[i] = a[i] to i
    }
    val seg = SegTree(b, { x, y -> if (x.first > y.first) x else y }, 0L to 0)
    val q = nextInt()
    for (_i in 0 until q) {
        val t = nextInt()
        val x = nextInt() - 1
        val y = nextLong()
        if (t == 1) {
            seg[x] = seg[x].first + y to seg[x].second
        } else if (t == 2) {
            seg[x] = seg[x].first - y to seg[x].second
        } else {
            println(seg.allProd().second + 1)
        }
    }
}

class SegTree<Monoid>(private val n: Int, private val op: (Monoid, Monoid) -> Monoid, private val e: Monoid) {
    private var d: MutableList<Monoid>
    private var size = 1
    private var log = 0

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

    constructor(a: Array<Monoid>, op: (Monoid, Monoid) -> Monoid, e: Monoid) : this(a.count(), op, e) {
        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: Monoid) {
        if (p !in 0 until n) {
            throw IllegalArgumentException()
        }
        val p1 = p + size
        d[p1] = x
        for (i in 1..log) {
            update(p1 shr i)
        }
    }

    operator fun get(p: Int): Monoid {
        if (p !in 0 until n) {
            throw IllegalArgumentException()
        }
        return d[p + size]
    }

    fun prod(range: IntRange): Monoid {
        val l = range.first
        val r = range.last + 1
        if (l !in 0..r || r > n) {
            throw IllegalArgumentException()
        }
        var sml = e
        var smr = e
        var l1 = l + size
        var r1 = r + size
        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(): Monoid {
        return d[1]
    }

    fun maxRight(l: Int, f: (Monoid) -> Boolean): Int {
        if (l !in 0..n || !f(e)) {
            throw IllegalArgumentException()
        }
        if (l == n) return n
        var l1 = l + size
        var sm = e
        do {
            while (l1 % 2 == 0) l1 /= 2
            if (!f(op(sm, d[l1]))) {
                while (l1 < size) {
                    l1 *= 2
                    if (f(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, f: (Monoid) -> Boolean): Int {
        if (r !in 0..n || !f(e)) {
            throw IllegalArgumentException()
        }
        if (r == 0) return 0
        var r1 = r + size
        var sm = e
        do {
            r1--
            while (r1 > 1 && r1 % 2 != 0) r1 /= 2
            if (!f(op(d[r1], sm))) {
                while (r1 < size) {
                    r1 = 2 * r1 + 1
                    if (f(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])
    }

    fun joinToString(s: String = ", "): String {
        return (0 until n).map { this[it] }.joinToString(s)
    }
}

fun main() {
    Thread(null, {
        val writer = PrintWriter(System.out, false)
        writer.solve()
        writer.flush()
    }, "solve", 1.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