結果
問題 | No.1099 Range Square Sum |
ユーザー | yakamoto |
提出日時 | 2020-06-26 23:23:06 |
言語 | Kotlin (1.9.23) |
結果 |
AC
|
実行時間 | 1,353 ms / 2,000 ms |
コード長 | 5,215 bytes |
コンパイル時間 | 21,788 ms |
コンパイル使用メモリ | 457,684 KB |
実行使用メモリ | 112,388 KB |
最終ジャッジ日時 | 2024-07-05 00:05:44 |
合計ジャッジ時間 | 39,939 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 285 ms
50,132 KB |
testcase_01 | AC | 288 ms
49,820 KB |
testcase_02 | AC | 327 ms
49,912 KB |
testcase_03 | AC | 283 ms
49,956 KB |
testcase_04 | AC | 281 ms
49,988 KB |
testcase_05 | AC | 285 ms
49,900 KB |
testcase_06 | AC | 282 ms
49,916 KB |
testcase_07 | AC | 280 ms
49,948 KB |
testcase_08 | AC | 283 ms
49,884 KB |
testcase_09 | AC | 284 ms
49,848 KB |
testcase_10 | AC | 281 ms
49,912 KB |
testcase_11 | AC | 374 ms
52,028 KB |
testcase_12 | AC | 384 ms
52,136 KB |
testcase_13 | AC | 369 ms
52,124 KB |
testcase_14 | AC | 373 ms
52,048 KB |
testcase_15 | AC | 375 ms
51,964 KB |
testcase_16 | AC | 372 ms
51,852 KB |
testcase_17 | AC | 372 ms
52,060 KB |
testcase_18 | AC | 373 ms
52,092 KB |
testcase_19 | AC | 418 ms
52,216 KB |
testcase_20 | AC | 374 ms
52,120 KB |
testcase_21 | AC | 1,139 ms
112,264 KB |
testcase_22 | AC | 1,137 ms
105,700 KB |
testcase_23 | AC | 1,353 ms
112,388 KB |
testcase_24 | AC | 1,337 ms
112,276 KB |
testcase_25 | AC | 1,292 ms
112,224 KB |
testcase_26 | AC | 1,201 ms
105,628 KB |
testcase_27 | AC | 1,222 ms
110,288 KB |
testcase_28 | AC | 1,184 ms
112,036 KB |
testcase_29 | AC | 1,181 ms
111,808 KB |
testcase_30 | AC | 1,178 ms
112,324 KB |
ソースコード
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_007L class Solver(stream: InputStream, private val out: java.io.PrintWriter) { fun solve() { val N = ni() val A = na(N) val s = LongArray(N) val s2 = LongArray(N) for (i in 0 until N) { s[i] = A[i].toLong() s2[i] = A[i].toLong() * A[i] } val t = DelayMergeTree(N, s, s2) debug(t.values()) debug(t.values2()) for (i in 0 until ni()) { when(ni()) { 1 -> { val l = ni() - 1 val r = ni() val x = nl() t.add(l, r, x) debug(t.values()) debug(t.values2()) } else -> { val l = ni() - 1 val r = ni() out.println(t.query(l, r)) } } } } class DelayMergeTree(n: Int, s: LongArray, s2: LongArray) { private val N = if (Integer.highestOneBit(n) == n) n else Integer.highestOneBit(n) shl 1 private val value = LongArray(N * 2) private val value2 = LongArray(N * 2) private val delay = LongArray(N * 2) private val elm = IntArray(N * 2) init { elm[1] = N for (i in 2 until N * 2) { elm[i] = elm[i / 2] / 2 } for (i in 0 until n) { value[N + i] = s[i] value2[N + i] = s2[i] } for (i in N - 1 downTo 1) { value[i] = value[i * 2] + value[i * 2 + 1] value2[i] = value2[i * 2] + value2[i * 2 + 1] } } fun values() = value fun values2() = value2 private fun push(k: Int) { if (k < N) { val x = delay[k] value2[k * 2] += x * 2 * value[k * 2] + x * x * elm[k * 2] value2[k * 2 + 1] += x * 2 * value[k * 2 + 1] + x * x * elm[k * 2 + 1] value[k * 2] += x * elm[k * 2] value[k * 2 + 1] += x * elm[k * 2 + 1] delay[k * 2] += x delay[k * 2 + 1] += x delay[k] = 0 } } /** * [a, b) */ fun add(a: Int, b: Int, x: Long, k: Int = 1, l: Int = 0, r: Int = N) { if (a >= r || l >= b) return // ノードが範囲からはずれてる if (a <= l && r <= b) { // ノードが完全に範囲に含まれる value2[k] += elm[k] * x * x + 2 * value[k] * x value[k] += x * elm[k] delay[k] += x return } push(k) val m = (l + r) / 2 val lft = k * 2 val rgt = lft + 1 add(a, b, x, lft, l, m) add(a, b, x, rgt, m, r) value[k] = value[lft] + value[rgt] value2[k] = value2[lft] + value2[rgt] } fun query(a: Int, b: Int, k: Int = 1, l: Int = 0, r: Int = N): Long { if (a >= r || l >= b) return 0// ノードが範囲からはずれてる if (a <= l && r <= b) { // ノードが完全に範囲に含まれる return value2[k] } if (l + 1 == r) return value2[l] push(k) val m = (l + r) / 2 val lft = k * 2 val rgt = lft + 1 return query(a, b, rgt, m, r) + query(a, b, lft, l, m) } } 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() + 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 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<LongArray>) { if (isDebug) { for (a in A) { debug(a) } } } 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() }