結果

問題 No.1282 Display Elements
ユーザー rutilicusrutilicus
提出日時 2020-11-30 23:32:51
言語 Kotlin
(1.9.23)
結果
AC  
実行時間 1,939 ms / 2,000 ms
コード長 2,015 bytes
コンパイル時間 17,903 ms
コンパイル使用メモリ 430,280 KB
実行使用メモリ 96,112 KB
最終ジャッジ日時 2023-10-11 03:11:26
合計ジャッジ時間 37,483 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 311 ms
57,300 KB
testcase_01 AC 308 ms
57,012 KB
testcase_02 AC 307 ms
57,012 KB
testcase_03 AC 292 ms
52,992 KB
testcase_04 AC 314 ms
56,904 KB
testcase_05 AC 312 ms
57,100 KB
testcase_06 AC 311 ms
57,084 KB
testcase_07 AC 315 ms
58,832 KB
testcase_08 AC 313 ms
57,160 KB
testcase_09 AC 315 ms
57,116 KB
testcase_10 AC 369 ms
57,476 KB
testcase_11 AC 370 ms
57,276 KB
testcase_12 AC 331 ms
57,460 KB
testcase_13 AC 371 ms
57,576 KB
testcase_14 AC 385 ms
57,656 KB
testcase_15 AC 1,766 ms
90,228 KB
testcase_16 AC 1,050 ms
73,472 KB
testcase_17 AC 1,615 ms
88,644 KB
testcase_18 AC 1,136 ms
79,632 KB
testcase_19 AC 882 ms
74,364 KB
testcase_20 AC 900 ms
74,360 KB
testcase_21 AC 1,939 ms
96,112 KB
testcase_22 AC 1,123 ms
73,628 KB
testcase_23 AC 1,926 ms
95,608 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 aList = readInputLine().split(" ").map { it.toInt() }.sorted()
    val bArr = readInputLine().split(" ").map { it.toInt() }.toIntArray()

    val allList = mutableSetOf<Int>()

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

    val item2Map = mutableMapOf<Int, Int>()

    for ((i, a) in allList.sorted().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