結果

問題 No.1282 Display Elements
ユーザー rutilicusrutilicus
提出日時 2020-12-06 14:05:53
言語 Kotlin
(1.9.23)
結果
AC  
実行時間 1,636 ms / 2,000 ms
コード長 1,728 bytes
コンパイル時間 17,209 ms
コンパイル使用メモリ 450,920 KB
実行使用メモリ 97,804 KB
最終ジャッジ日時 2023-10-17 15:22:06
合計ジャッジ時間 35,672 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 333 ms
58,460 KB
testcase_01 AC 326 ms
58,456 KB
testcase_02 AC 335 ms
58,472 KB
testcase_03 AC 327 ms
58,444 KB
testcase_04 AC 327 ms
58,460 KB
testcase_05 AC 325 ms
58,456 KB
testcase_06 AC 320 ms
58,456 KB
testcase_07 AC 324 ms
58,464 KB
testcase_08 AC 329 ms
58,456 KB
testcase_09 AC 325 ms
58,456 KB
testcase_10 AC 378 ms
58,768 KB
testcase_11 AC 371 ms
58,768 KB
testcase_12 AC 324 ms
58,552 KB
testcase_13 AC 369 ms
58,740 KB
testcase_14 AC 386 ms
59,060 KB
testcase_15 AC 1,406 ms
89,396 KB
testcase_16 AC 869 ms
70,180 KB
testcase_17 AC 1,413 ms
90,272 KB
testcase_18 AC 1,031 ms
76,308 KB
testcase_19 AC 792 ms
70,428 KB
testcase_20 AC 789 ms
71,796 KB
testcase_21 AC 1,531 ms
97,804 KB
testcase_22 AC 880 ms
77,952 KB
testcase_23 AC 1,636 ms
97,708 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Main.kt:75:13: warning: 'appendln(Long): kotlin.text.StringBuilder /* = java.lang.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 #

// 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