// なんか本番でエラーでる 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) { 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() 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")) }