結果

問題 No.876 Range Compress Query
ユーザー yakamotoyakamoto
提出日時 2019-09-06 22:31:45
言語 Kotlin
(1.9.23)
結果
AC  
実行時間 992 ms / 2,000 ms
コード長 2,665 bytes
コンパイル時間 19,529 ms
コンパイル使用メモリ 434,936 KB
実行使用メモリ 68,088 KB
最終ジャッジ日時 2023-09-07 01:16:38
合計ジャッジ時間 31,780 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 293 ms
52,964 KB
testcase_01 AC 343 ms
53,456 KB
testcase_02 AC 299 ms
53,408 KB
testcase_03 AC 335 ms
53,916 KB
testcase_04 AC 317 ms
53,216 KB
testcase_05 AC 315 ms
53,180 KB
testcase_06 AC 351 ms
53,400 KB
testcase_07 AC 350 ms
53,556 KB
testcase_08 AC 354 ms
53,640 KB
testcase_09 AC 351 ms
53,756 KB
testcase_10 AC 349 ms
53,356 KB
testcase_11 AC 992 ms
66,076 KB
testcase_12 AC 919 ms
65,996 KB
testcase_13 AC 922 ms
66,268 KB
testcase_14 AC 969 ms
66,072 KB
testcase_15 AC 876 ms
66,032 KB
testcase_16 AC 943 ms
68,044 KB
testcase_17 AC 953 ms
68,088 KB
testcase_18 AC 973 ms
68,044 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// なんか本番でエラーでる
private val isDebug = runCatching {
  System.getenv("MY_DEBUG") != null
}.fold({it}, {false})

private fun readLn() = readLine()!!
private fun readInt() = readLn().toInt()
private fun readStrings() = readLn().split(" ")
private fun readInts() = readStrings().map { it.toInt() }.toIntArray()
private fun readLongs() = readStrings().map { it.toLong() }.toLongArray()
private fun debug(msg: () -> String) {
  if (isDebug) System.err.println(msg())
}
private fun debug(a: LongArray) {
  if (isDebug) debug{a.joinToString(" ")}
}
private fun debug(a: IntArray) {
  if (isDebug) debug{a.joinToString(" ")}
}
private fun debugDim(A: Array<IntArray>) {
  if (isDebug) {
    for (a in A) {
      debug(a)
    }
  }
}

val MOD = 1000000007

class BIT(n: Int) {
  private val N =
    if (Integer.highestOneBit(n) == n) n
    else Integer.highestOneBit(n) shl 1

  private val bit = LongArray(N + 1)

  fun sum(i: Int): Long {
    var x = i
    var s = 0L
    while(x > 0) {
      s += bit[x]
      x -= x and -x
    }
    return s
  }

  fun add(i: Int, a: Long) {
    var x = i + 1
    while(x <= N) {
      bit[x] += a
      x += x and -x
    }
  }
}

fun main() {
  val (N, Q) = readInts()
  val A = BIT(N + 1)
  for ((i, a) in readLongs().withIndex()) {
    debug{"i:$i a:$a"}
    A.add(i, a)
    A.add(i + 1, -a)
  }

  fun get(i: Int) = A.sum(i + 1)

  val bit = BIT(N + 1)
  repeat(N - 1) {
    if (get(it) != get(it + 1)) bit.add(it, 1)
  }

  fun debugA() {
    if (isDebug) {
      val v = LongArray(N)
      repeat(N){ v[it] = get(it) }
      debug(v)
    }
  }

  fun debugBit() {
    if (isDebug) {
      val v = LongArray(N)
      repeat(N) {
        v[it] = bit.sum(it + 1)
      }
      debug(v)
    }
  }

  debugA()
  debugBit()

  fun diff(a: Long, b: Long) = if(a != b) 1 else 0

  val ans = mutableListOf<Int>()
  repeat(Q) {
    val q = readInts()
    when(q[0]) {
      1 -> {
        var (_, l, r, x) = q
        l--; r--
        val oldL = get(l)
        val oldL2 = if (l > 0) get(l-1) else -1
        val oldR = get(r)
        val oldR2 = if (r < N - 1) get(r+1) else -1

        A.add(l, x.toLong())
        A.add(r  + 1, -x.toLong())
        if (l > 0) {
          bit.add(l-1, (diff(get(l-1), get(l)) - diff(oldL2, oldL)).toLong())
        }
        if (r < N - 1) {
          bit.add(r, (diff(get(r), get(r+1)) - diff(oldR, oldR2)).toLong())
        }

        debugA()
        debugBit()
      }

      2 -> {
        var (_, l, r) = q
        l--; r--
        debug{"${bit.sum(r)} ${bit.sum(l)}"}
        ans += (bit.sum(r) - bit.sum(l)).toInt() + 1
      }
    }
  }
  println(ans.joinToString("\n"))
}


0