結果

問題 No.1282 Display Elements
コンテスト
ユーザー rutilicus
提出日時 2020-12-06 14:05:53
言語 Kotlin
(2.3.20)
コンパイル:
kotlinc _filename_ -include-runtime -d main.jar
実行:
kotlin main.jar
結果
CE  
(最新)
AC  
(最初)
実行時間 -
コード長 1,728 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 8,447 ms
コンパイル使用メモリ 442,116 KB
最終ジャッジ日時 2026-04-06 07:21:26
合計ジャッジ時間 8,988 ms
ジャッジサーバーID
(参考情報)
judge2_1 / judge1_1
このコードへのチャレンジ
(要ログイン)
コンパイルエラー時のメッセージ・ソースコードは、提出者また管理者しか表示できないようにしております。(リジャッジ後のコンパイルエラーは公開されます)
ただし、clay言語の場合は開発者のデバッグのため、公開されます。

コンパイルメッセージ
Main.kt:75:13: error: 'fun StringBuilder.appendln(value: Long): StringBuilder' is deprecated. Use appendLine instead. Note that the new method always appends the line feed character '\n' regardless of the system line separator.
    builder.appendln(ans)
            ^^^^^^^^

ソースコード

diff #
raw source code

// 1-indexed binary indexed tree
class BIT(n: Int, private val init: Long = 0L) {
    private var size = 1
    private var arr: LongArray

    init {
        while (size < n) {
            size *= 2
        }
        arr = LongArray(size + 1) { init }
    }

    // index(1-indexed)にvalueを加算
    fun add(index: Int, value: Long) {
        var i = index
        while (i <= size) {
            arr[i] += value
            i += i and (-i)
        }
    }

    // [l, r]の総和を求める
    fun sum(l: Int, r: Int): Long {
        // [1, i]の総和を求める副関数
        fun subFunc(i: Int): Long {
            var current = i
            var ret = 0L
            while (current > 0) {
                ret += arr[current]
                current -= current and (-current)
            }
            return ret
        }

        return subFunc(r) - subFunc(l - 1)
    }
}

fun main() {
    val builder = StringBuilder()

    val n = readInputLine().toInt()

    val allSet = sortedSetOf<Int>()

    val aList = readInputLine().split(" ").map { it.toInt() }.sorted()
    val bArr = readInputLine().split(" ").map { it.toInt() }.toIntArray()

    for (a in aList) {
        allSet.add(a)
    }
    for (b in bArr) {
        allSet.add(b)
    }

    val item2Map = mutableMapOf<Int, Int>()

    for ((i, a) in allSet.withIndex()) {
        item2Map[a] = i
    }

    val bit = BIT(2 * n)

    var ans = 0L

    for ((i, a) in aList.withIndex()) {
        val bIndex = item2Map[bArr[i]]!!
        val aIndex = item2Map[a]!!

        bit.add(bIndex + 1, 1L)

        ans += bit.sum(0, aIndex)
    }

    builder.appendln(ans)

    print(builder.toString())
}

fun readInputLine(): String {
    return readLine()!!
}
0