結果

問題 No.1282 Display Elements
ユーザー rutilicusrutilicus
提出日時 2020-11-30 23:39:21
言語 Kotlin
(1.9.23)
結果
AC  
実行時間 1,782 ms / 2,000 ms
コード長 2,001 bytes
コンパイル時間 17,018 ms
コンパイル使用メモリ 427,580 KB
実行使用メモリ 103,988 KB
最終ジャッジ日時 2023-10-11 03:12:28
合計ジャッジ時間 33,482 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 322 ms
56,884 KB
testcase_01 AC 323 ms
57,172 KB
testcase_02 AC 320 ms
56,872 KB
testcase_03 AC 315 ms
57,072 KB
testcase_04 AC 319 ms
57,136 KB
testcase_05 AC 315 ms
56,924 KB
testcase_06 AC 315 ms
56,788 KB
testcase_07 AC 321 ms
56,836 KB
testcase_08 AC 313 ms
56,848 KB
testcase_09 AC 314 ms
56,888 KB
testcase_10 AC 373 ms
58,768 KB
testcase_11 AC 367 ms
57,144 KB
testcase_12 AC 330 ms
56,928 KB
testcase_13 AC 376 ms
57,420 KB
testcase_14 AC 399 ms
57,540 KB
testcase_15 AC 1,563 ms
101,292 KB
testcase_16 AC 1,013 ms
77,992 KB
testcase_17 AC 1,451 ms
90,168 KB
testcase_18 AC 1,075 ms
80,124 KB
testcase_19 AC 881 ms
72,644 KB
testcase_20 AC 1,045 ms
73,124 KB
testcase_21 AC 1,782 ms
103,820 KB
testcase_22 AC 1,159 ms
78,036 KB
testcase_23 AC 1,741 ms
103,988 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Main.kt:80: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 #

class LongSegmentTree(n: Int, private val init: Long, private val func: (Long, Long) -> Long) {
    private var size = 1
    private var segTree: LongArray

    init {
        while (size < n) {
            size *= 2
        }

        segTree = LongArray(size * 2 - 1) { init }
    }

    // 指定要素を更新
    fun update(x: Int, value: Long) {
        var tmp = x + size - 1

        segTree[tmp] = value
        while (tmp > 0) {
            tmp = (tmp - 1) / 2
            segTree[tmp] = func(segTree[2 * tmp + 1], segTree[2 * tmp + 2])
        }
    }

    // [a, b)の関数適用結果を取得
    // bが開区間であることに注意
    fun get(a: Int, b: Int, k: Int = 0, l: Int = 0, r: Int = size): Long {
        if (r <= a || b <= l) {
            return init
        }
        if (a <= l && r <= b) {
            return segTree[k]
        }

        val vl = get(a, b, 2 * k + 1, l, (l + r) / 2)
        val vr = get(a, b, 2 * k + 2, (l + r) / 2, r)

        return func(vl, vr)
    }

    // 指定要素を取得
    fun getElem(x: Int): Long = segTree[x + size - 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 segTree = LongSegmentTree(2 * n, 0L, Long::plus)

    var ans = 0L

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

        segTree.update(bIndex, segTree.getElem(bIndex) + 1L)

        ans += segTree.get(0, aIndex)
    }

    builder.appendln(ans)

    print(builder.toString())
}

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