結果

問題 No.1558 Derby Live
ユーザー yakamotoyakamoto
提出日時 2021-06-26 19:29:28
言語 Kotlin
(1.9.23)
結果
AC  
実行時間 827 ms / 2,000 ms
コード長 5,065 bytes
コンパイル時間 18,158 ms
コンパイル使用メモリ 444,056 KB
実行使用メモリ 69,012 KB
最終ジャッジ日時 2023-09-07 15:30:40
合計ジャッジ時間 46,071 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 638 ms
60,780 KB
testcase_01 AC 827 ms
66,348 KB
testcase_02 AC 260 ms
50,328 KB
testcase_03 AC 728 ms
67,004 KB
testcase_04 AC 529 ms
56,956 KB
testcase_05 AC 559 ms
58,568 KB
testcase_06 AC 648 ms
57,932 KB
testcase_07 AC 381 ms
56,308 KB
testcase_08 AC 618 ms
57,940 KB
testcase_09 AC 629 ms
57,212 KB
testcase_10 AC 657 ms
60,108 KB
testcase_11 AC 679 ms
60,008 KB
testcase_12 AC 673 ms
62,636 KB
testcase_13 AC 714 ms
62,292 KB
testcase_14 AC 727 ms
61,252 KB
testcase_15 AC 747 ms
61,532 KB
testcase_16 AC 749 ms
67,456 KB
testcase_17 AC 769 ms
66,712 KB
testcase_18 AC 765 ms
63,668 KB
testcase_19 AC 813 ms
69,012 KB
testcase_20 AC 799 ms
68,024 KB
testcase_21 AC 769 ms
68,916 KB
testcase_22 AC 787 ms
63,548 KB
testcase_23 AC 799 ms
68,028 KB
testcase_24 AC 773 ms
64,640 KB
testcase_25 AC 810 ms
63,220 KB
testcase_26 AC 791 ms
63,724 KB
testcase_27 AC 791 ms
67,576 KB
testcase_28 AC 802 ms
61,556 KB
testcase_29 AC 813 ms
67,856 KB
testcase_30 AC 778 ms
63,272 KB
testcase_31 AC 790 ms
64,244 KB
testcase_32 AC 801 ms
68,532 KB
testcase_33 AC 285 ms
52,992 KB
testcase_34 AC 286 ms
52,956 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Main.kt:54:11: warning: expected performance impact from inlining is insignificant. Inlining works best for functions with parameters of functional types
  private inline fun fx(a: IntArray, b: IntArray) = run {
          ^
Main.kt:63:13: warning: expected performance impact from inlining is insignificant. Inlining works best for functions with parameters of functional types
    private inline fun ap(x: IntArray, m: IntArray) = m // 入れ替え
            ^
Main.kt:63:27: warning: parameter 'x' is never used
    private inline fun ap(x: IntArray, m: IntArray) = m // 入れ替え
                          ^
Main.kt:83:31: warning: parameter 'l' is never used
    fun query(a: Int, b: Int, l: Int = 0, r: Int = N, k: Int = 1): IntArray {
                              ^
Main.kt:83:43: warning: parameter 'r' is never used
    fun query(a: Int, b: Int, l: Int = 0, r: Int = N, k: Int = 1): IntArray {
                                          ^
Main.kt:83:55: warning: parameter 'k' is never used
    fun query(a: Int, b: Int, l: Int = 0, r: Int = N, k: Int = 1): IntArray {
                                                      ^
Main.kt:194:11: warning: expected performance impact from inlining is insignificant. Inlining works best for functions with parameters of functional types
  private inline fun debug(a: LongArray) {
          ^
Main.kt:198:11: warning: expected performance impact from inlining is insignificant. Inlining works best for functions with parameters of functional types
  private inline fun debug(a: IntArray) {
          ^
Main.kt:202:11: warning: expected performance impact from inlining is insignificant. Inlining works best for functions with parameters of functional types
  private inline fun debug(a: BooleanArray) {
          ^
Main.kt:206:11: warning: expected performance impact from inlining is insignificant. Inlining works best for functions with parameters of functional types
  private inline fun toString(a: BooleanArray) = run{a.map { if (it) 1 else

ソースコード

diff #

import java.io.BufferedReader
import java.io.InputStream
import java.io.InputStreamReader
import java.lang.AssertionError
import java.util.*
import kotlin.math.abs
import kotlin.math.max
import kotlin.math.min

val MOD = 1_000_000_007

class Solver(stream: InputStream, private val out: java.io.PrintWriter) {
  private val reader = BufferedReader(InputStreamReader(stream), 32768)

  private val NN = ni()
  private val M = ni()
  private val Q = ni()
  fun solve() {
    val zero = IntArray(NN) { it }
    val tree = SegmentTreeA(M, zero)

    for (q in 0 until Q) {
      val type = ni()
      when (type) {
        1 -> {
          val d = ni() - 1
          val p = na(NN, -1)
          tree.update(d, p)
        }
        2 -> {
          val s = ni()
          val p = tree.query(0, s)
          debug { "type2 ${p.joinToString(" ")}" }
          val inv = IntArray(NN)
          for (i in 0 until NN) {
            inv[p[i]] = i
          }
          out.println(inv.map { it + 1 }.joinToString(" "))
        }
        else -> {
          val (l, r) = na(2, -1)
          val p = tree.query(l, r + 1)
          debug { "type3 ${p.joinToString(" ")}" }
          var ans = 0L
          for (i in 0 until NN) {
            ans += abs(i - p[i])
          }
          out.println(ans)
        }
      }
    }
  }

  private inline fun fx(a: IntArray, b: IntArray) = run {
    val res = IntArray(NN)
    for (i in res.indices) {
      res[i] = b[a[i]]
    }
    res
  }

  inner class SegmentTreeA(val n: Int, val zero: IntArray) {
    private inline fun ap(x: IntArray, m: IntArray) = m // 入れ替え

    private val N =
      if (Integer.highestOneBit(n) == n) n
      else Integer.highestOneBit(n) shl 1

    private val dat = Array(2*N){zero}

    fun update(i: Int, a: IntArray) {
      var ix = i + N
      dat[ix] = ap(dat[ix], a)
      while(ix > 1) {
        dat[ix shr 1] = if (ix and 1 == 0) fx(dat[ix], dat[ix xor 1]) else fx(dat[ix xor 1], dat[ix])
        ix = ix shr 1
      }
    }

    /**
     * [a, b)
     */
    fun query(a: Int, b: Int, l: Int = 0, r: Int = N, k: Int = 1): IntArray {
      var resL = zero
      var resR = zero
      var left = a + N
      var right = b - 1 + N

      while(left <= right) {
        if ((left and 1) == 1) resL = fx(resL, dat[left])
        if ((right and 1) == 0) resR = fx(dat[right], resR)
        left = (left + 1) shr 1 // 右の子供なら右の親に移動
        right = (right - 1) shr 1 // 左の子供なら左の親に移動
      }

      return fx(resL, resR)
    }

    fun get(i: Int) = dat[N + i]

    override fun toString() = "[${(0 until n).map{get(it)}.joinToString(" ")}]"
  }













































  private val isDebug = try {
    // なんか本番でエラーでる
    System.getenv("MY_DEBUG") != null
  } catch (t: Throwable) {
    false
  }

  private var tokenizer: StringTokenizer? = null
  private fun next(): String {
    while (tokenizer == null || !tokenizer!!.hasMoreTokens()) {
      tokenizer = StringTokenizer(reader.readLine())
    }
    return tokenizer!!.nextToken()
  }

  private fun ni() = next().toInt()
  private fun nl() = next().toLong()
  private fun ns() = next()
  private fun na(n: Int, offset: Int = 0): IntArray {
    return IntArray(n) { ni() + offset }
  }
  private fun nal(n: Int, offset: Int = 0): LongArray {
    val res = LongArray(n)
    for (i in 0 until n) {
      res[i] = nl() + offset
    }
    return res
  }

  private fun na2(n: Int, offset: Int = 0): Array<IntArray> {
    val a  = Array(2){IntArray(n)}
    for (i in 0 until n) {
      for (e in a) {
        e[i] = ni() + offset
      }
    }
    return a
  }

  private inline fun debug(msg: () -> String) {
    if (isDebug) System.err.println(msg())
  }

  /**
   * コーナーケースでエラー出たりするので、debug(dp[1])のように添え字付きの場合はdebug{}をつかうこと
   */
  private inline fun debug(a: LongArray) {
    debug { a.joinToString(" ") }
  }

  private inline fun debug(a: IntArray) {
    debug { a.joinToString(" ") }
  }

  private inline fun debug(a: BooleanArray) {
    debug { toString(a) }
  }

  private inline fun toString(a: BooleanArray) = run{a.map { if (it) 1 else 0 }.joinToString("")}

  private inline fun debugDim(A: Array<LongArray>) {
    if (isDebug) {
      for (a in A) {
        debug(a)
      }
    }
  }
  private inline fun debugDim(A: Array<IntArray>) {
    if (isDebug) {
      for (a in A) {
        debug(a)
      }
    }
  }
  private inline fun debugDim(A: Array<BooleanArray>) {
    if (isDebug) {
      for (a in A) {
        debug(a)
      }
    }
  }

  /**
   * 勝手にimport消されるのを防ぎたい
   */
  private fun hoge() {
    min(1, 2)
    max(1, 2)
    abs(-10)
  }

  private inline fun assert(b: Boolean) = run{if (!b) throw AssertionError()}
  private inline fun assert(b: Boolean, f: () -> String) = run{if (!b) throw AssertionError(f())}
}

fun main() {
  val out = java.io.PrintWriter(System.out)
  Solver(System.`in`, out).solve()
  out.flush()
}
0