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 { 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) { if (isDebug) { for (a in A) { debug(a) } } } private fun debugDim(A: Array) { 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() }