結果
問題 |
No.1282 Display Elements
|
ユーザー |
![]() |
提出日時 | 2020-12-06 14:05:53 |
言語 | Kotlin (2.1.0) |
結果 |
AC
|
実行時間 | 1,260 ms / 2,000 ms |
コード長 | 1,728 bytes |
コンパイル時間 | 11,983 ms |
コンパイル使用メモリ | 451,784 KB |
実行使用メモリ | 117,040 KB |
最終ジャッジ日時 | 2024-09-17 13:04:34 |
合計ジャッジ時間 | 26,713 ms |
ジャッジサーバーID (参考情報) |
judge6 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 24 |
コンパイルメッセージ
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) ^
ソースコード
// 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()!! }