結果

問題 No.1282 Display Elements
ユーザー rutilicusrutilicus
提出日時 2020-12-06 14:09:21
言語 Kotlin
(1.9.23)
結果
AC  
実行時間 1,650 ms / 2,000 ms
コード長 1,744 bytes
コンパイル時間 18,159 ms
コンパイル使用メモリ 446,080 KB
実行使用メモリ 97,432 KB
最終ジャッジ日時 2023-10-17 15:23:33
合計ジャッジ時間 31,309 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 323 ms
58,488 KB
testcase_01 AC 328 ms
58,488 KB
testcase_02 AC 327 ms
58,492 KB
testcase_03 AC 327 ms
58,476 KB
testcase_04 AC 336 ms
58,484 KB
testcase_05 AC 327 ms
58,468 KB
testcase_06 AC 327 ms
58,468 KB
testcase_07 AC 331 ms
58,476 KB
testcase_08 AC 311 ms
58,488 KB
testcase_09 AC 334 ms
58,492 KB
testcase_10 AC 379 ms
58,800 KB
testcase_11 AC 378 ms
58,800 KB
testcase_12 AC 340 ms
58,576 KB
testcase_13 AC 377 ms
58,768 KB
testcase_14 AC 378 ms
58,844 KB
testcase_15 AC 1,444 ms
85,432 KB
testcase_16 AC 887 ms
72,332 KB
testcase_17 AC 1,375 ms
84,032 KB
testcase_18 AC 1,034 ms
82,780 KB
testcase_19 AC 781 ms
76,724 KB
testcase_20 AC 791 ms
76,544 KB
testcase_21 AC 1,481 ms
97,292 KB
testcase_22 AC 896 ms
77,996 KB
testcase_23 AC 1,650 ms
97,432 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Main.kt:76: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().apply {
            allSet.add(this)
        }
    }.sorted()
    val bArr = readInputLine().split(" ").map {
        it.toInt().apply {
            allSet.add(this)
        }
    }.toIntArray()

    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