結果

問題 No.1000 Point Add and Array Add
ユーザー yakamotoyakamoto
提出日時 2020-03-10 15:20:27
言語 Kotlin
(1.9.23)
結果
AC  
実行時間 957 ms / 2,000 ms
コード長 3,586 bytes
コンパイル時間 12,929 ms
コンパイル使用メモリ 463,844 KB
実行使用メモリ 106,768 KB
最終ジャッジ日時 2024-04-27 18:37:37
合計ジャッジ時間 28,209 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 301 ms
57,848 KB
testcase_01 AC 264 ms
57,896 KB
testcase_02 AC 261 ms
57,748 KB
testcase_03 AC 259 ms
57,728 KB
testcase_04 AC 255 ms
57,884 KB
testcase_05 AC 263 ms
57,932 KB
testcase_06 AC 263 ms
57,548 KB
testcase_07 AC 261 ms
57,860 KB
testcase_08 AC 255 ms
57,660 KB
testcase_09 AC 266 ms
57,772 KB
testcase_10 AC 257 ms
57,800 KB
testcase_11 AC 264 ms
57,692 KB
testcase_12 AC 358 ms
61,956 KB
testcase_13 AC 427 ms
62,104 KB
testcase_14 AC 356 ms
62,008 KB
testcase_15 AC 360 ms
62,132 KB
testcase_16 AC 907 ms
104,936 KB
testcase_17 AC 751 ms
100,352 KB
testcase_18 AC 957 ms
105,548 KB
testcase_19 AC 915 ms
104,752 KB
testcase_20 AC 760 ms
96,400 KB
testcase_21 AC 896 ms
106,768 KB
testcase_22 AC 929 ms
104,264 KB
testcase_23 AC 917 ms
106,012 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Main.kt:146:27: warning: parameter 'offset' is never used
  private fun nal(n: Int, offset: Int = 0): LongArray {
                          ^

ソースコード

diff #

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

val MOD = 1_000_000_007

class RangeUpdateTree(n: Int,
                      private val zero: Long,
                      private val f: (Long, Long) -> Long) {
  private val N =
    if (Integer.highestOneBit(n) == n) n
    else Integer.highestOneBit(n) shl 1

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

  /**
   * [l, r)
   */
  fun add(l: Int, r: Int, a: Long) {
    var left = l + N
    var right = r - 1 + N

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

  fun query(i: Int): Long {
    var ix = N + i
    var res: Long = zero

    while(ix >= 1) {
      res = f(res, dat[ix])
      ix = ix shr 1
    }

    return res
  }
}

data class Query(val isA: Boolean, val x: Int, val y: Int)

class Solver(stream: InputStream, private val out: java.io.PrintWriter) {
  fun solve() {
    val N = ni()
    val Q = ni()
    val A = na(N)
    val queries = Array(Q) {
      Query(ns() == "A", ni(), ni())
    }

    val ans = LongArray(N)
    val tree = RangeUpdateTree(N, 0, Long::plus)
    for (i in Q -1 downTo 0) {
      if (queries[i].isA) {
        val ix = queries[i].x - 1
        val v = queries[i].y
        val times = tree.query(ix)
        ans[ix] += times * v
      } else {
        val s = queries[i].x - 1
        val t = queries[i].y
        tree.add(s, t, 1)
      }
    }
    for (i in 0 until N) {
      val times = tree.query(i)
      ans[i] += times * A[i]
    }
    out.println(ans.joinToString(" "))
  }













































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

  private var tokenizer: StringTokenizer? = null
  private val reader = BufferedReader(InputStreamReader(stream), 32768)
  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 map(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()
    }
    return res
  }

  private inline fun map(n: Int, f: (Int) -> Int): IntArray {
    val res = IntArray(n)
    for (i in 0 until n) {
      res[i] = f(i)
    }
    return res
  }

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

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

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

  private fun debug(a: BooleanArray) {
    debug { a.map { if (it) 1 else 0 }.joinToString("") }
  }

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

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

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