結果

問題 No.1705 Mode of long array
ユーザー 箱星箱星
提出日時 2021-10-08 23:29:59
言語 Kotlin
(1.9.23)
結果
AC  
実行時間 999 ms / 3,000 ms
コード長 4,506 bytes
コンパイル時間 19,121 ms
コンパイル使用メモリ 462,752 KB
実行使用メモリ 96,044 KB
最終ジャッジ日時 2024-07-23 07:39:23
合計ジャッジ時間 61,150 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 302 ms
50,044 KB
testcase_01 AC 308 ms
50,232 KB
testcase_02 AC 310 ms
50,056 KB
testcase_03 AC 415 ms
53,800 KB
testcase_04 AC 354 ms
50,816 KB
testcase_05 AC 418 ms
54,112 KB
testcase_06 AC 453 ms
55,028 KB
testcase_07 AC 445 ms
56,020 KB
testcase_08 AC 441 ms
56,136 KB
testcase_09 AC 440 ms
55,760 KB
testcase_10 AC 448 ms
55,500 KB
testcase_11 AC 449 ms
55,776 KB
testcase_12 AC 450 ms
55,256 KB
testcase_13 AC 833 ms
96,044 KB
testcase_14 AC 750 ms
85,808 KB
testcase_15 AC 711 ms
79,752 KB
testcase_16 AC 753 ms
84,472 KB
testcase_17 AC 704 ms
77,004 KB
testcase_18 AC 687 ms
72,508 KB
testcase_19 AC 796 ms
86,556 KB
testcase_20 AC 737 ms
76,232 KB
testcase_21 AC 766 ms
88,396 KB
testcase_22 AC 938 ms
91,352 KB
testcase_23 AC 579 ms
83,624 KB
testcase_24 AC 581 ms
83,460 KB
testcase_25 AC 582 ms
83,544 KB
testcase_26 AC 580 ms
83,660 KB
testcase_27 AC 582 ms
83,680 KB
testcase_28 AC 578 ms
84,004 KB
testcase_29 AC 590 ms
83,704 KB
testcase_30 AC 582 ms
83,828 KB
testcase_31 AC 584 ms
83,584 KB
testcase_32 AC 583 ms
83,908 KB
testcase_33 AC 909 ms
91,720 KB
testcase_34 AC 981 ms
91,676 KB
testcase_35 AC 923 ms
91,340 KB
testcase_36 AC 903 ms
91,476 KB
testcase_37 AC 906 ms
91,652 KB
testcase_38 AC 885 ms
91,412 KB
testcase_39 AC 926 ms
91,024 KB
testcase_40 AC 913 ms
91,560 KB
testcase_41 AC 921 ms
91,540 KB
testcase_42 AC 999 ms
91,552 KB
testcase_43 AC 782 ms
91,704 KB
testcase_44 AC 774 ms
92,928 KB
testcase_45 AC 801 ms
91,572 KB
testcase_46 AC 837 ms
91,500 KB
testcase_47 AC 802 ms
91,732 KB
testcase_48 AC 821 ms
91,688 KB
testcase_49 AC 896 ms
91,780 KB
testcase_50 AC 871 ms
90,916 KB
testcase_51 AC 864 ms
92,288 KB
testcase_52 AC 902 ms
91,928 KB
testcase_53 AC 908 ms
92,000 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