Problem:
RSQ and RAQ

Write a program which manipulates a sequence A = {a1, a2, . . . , an} with the following operations:

add(s, t, x): add x to as, as+1, ..., at.

getSum(s, t): report the sum of as, as+1, ..., at.

Note that the initial values of ai (i = 1, 2, . . . , n) are 0.

Input

n q
query1
query2
:
queryq

In the first line, n (the number of elements in A) and q (the number of queries) are given. Then, ith query queryi is given in the following format:

0 s t x

or

1 s t

The first digit represents the type of the query. '0' denotes add(s, t, x) and '1' denotes getSum(s, t).

Output

For each getSum operation, print the sum;

Constraints

1 ≤ n ≤ 100000

1 ≤ q ≤ 100000

1 ≤ s ≤ t ≤ n

0 ≤ x < 1000

Sample Input 1

3 5
0 1 2 1
0 2 3 2
0 3 3 3
1 1 2
1 2 3

Sample Output 1

4
8

Sample Input 2

4 3
1 1 4
0 1 4 1
1 1 4

Sample Output 2

0
4

Solution in Kotlin (judge verdict: Accepted)

import java.io.PrintWriter
import java.util.*

fun main(args: Array<String>) = IO().exec {
    val n = int()
    val q = int()
    val segRangeAdd = SegRangeAdd(n)
    for (i in 0 until q) {
        val com = int()
        if (com == 0) segRangeAdd.add(int()-1..int()-1, long())
        else println(segRangeAdd.sum(int()-1..int()-1))
    }
}

operator fun IntRange.contains(target: IntRange): Boolean = this.first <= target.first && target.last <= this.last
infix fun IntRange.separates(other: IntRange): Boolean = this.last < other.first || other.last < this.first
val IntRange.endExclusive
    get() = this.last + 1
fun IntRange.size(): Int = this.last + 1 - this.first

class SegRangeAdd(val size: Int) {
    val v = LongArray(size)
    val n = run {
        var n = 1
        while (n < size) n *= 2
        n
    }
    val node = LongArray(2*n-1)
    val lazy = LongArray(2*n-1)
    init {
        for (i in 0 until size) node[i+n-1] = v[i]
        for (i in n-2 downTo 0) node[i] = node[i*2+1] + node[i*2+2]
    }

    fun eval(k: Int, range: IntRange) {
        if (lazy[k] != 0L) {
            node[k] += lazy[k]
            if (range.size() > 1) {
                lazy[2*k+1] += lazy[k] / 2
                lazy[2*k+2] += lazy[k] / 2
            }
            lazy[k] = 0
        }
    }

    fun add(query: IntRange, x: Long) = addrec(query, x, 0, 0 until n)

    fun addrec(query: IntRange, x: Long, k: Int, range: IntRange) {
        eval(k, range)
        if (query separates range) return
        if (range in query) {
            lazy[k] += range.size() * x
            eval(k, range)
        } else {
            addrec(query, x, 2*k+1, with(range){first until (first+endExclusive)/2})
            addrec(query, x, 2*k+2, with(range){(first+endExclusive)/2 until endExclusive})
            node[k] = node[2*k+1] + node[2*k+2]
        }
    }

    fun sum(query: IntRange) = sumrec(query, 0, 0 until n)

    fun sumrec(query: IntRange, k: Int, range: IntRange): Long {
        if (query separates range) return 0
        eval(k, range)
        if (range in query) return node[k]
        val vl = sumrec(query, 2*k+1, with(range){first until (first+endExclusive)/2})
        val vr = sumrec(query, 2*k+2, with(range){(first+endExclusive)/2 until endExclusive})
        return vl + vr
    }
}

class IO {
    val printable = 33..126
    val buffer = ByteArray(1024)
    var ptr = 0
    var buflen = 0
    val out = PrintWriter(System.out)
    fun hasNextByte(): Boolean {
        if (ptr < buflen) {
            return true
        } else {
            ptr = 0
            buflen = System.`in`.read(buffer)
            if (buflen <= 0) {
                return false
            }
        }
        return true
    }
    fun readByte(): Byte {
        return if (hasNextByte()) buffer[ptr++] else return -1
    }
    fun hasNext(): Boolean {
        while (hasNextByte() && buffer[ptr] !in printable) ptr++
        return hasNextByte()
    }
    fun string(): String {
        if (!hasNext()) throw NoSuchElementException()
        val sb = StringBuilder()
        var b = readByte()
        while (b in printable) {
            sb.appendCodePoint(b.toInt())
            b = readByte()
        }
        return sb.toString()
    }
    fun long(): Long {
        if (!hasNext()) throw NoSuchElementException()
        var n = 0L
        var minus = false
        var b = readByte()
        if (b == '-'.toByte()) {
            minus = true
            b = readByte()
        }
        if (b !in '0'.toByte()..'9'.toByte()) {
            throw NumberFormatException()
        }
        while (true) {
            if (b in '0'.toByte()..'9'.toByte()) {
                n *= 10
                n += b - '0'.toByte()
            } else if (b == (-1).toByte() || b !in printable) {
                return if (minus) -n else n
            } else {
                throw NumberFormatException()
            }
            b = readByte()
        }
    }
    fun int(): Int {
        val nl = long()
        if (nl !in Integer.MIN_VALUE..Integer.MAX_VALUE) throw NumberFormatException()
        return nl.toInt()
    }
    fun double(): Double {
        return string().toDouble()
    }
    fun print(obj: Any) = out.print(obj)
    fun print(i: Int) = out.print(i)
    fun print(l: Long) = out.print(l)
    fun println(obj: Any) = out.println(obj)
    fun println(i: Int) = out.println(i)
    fun println(l: Long) = out.println(l)
    inline fun exec(block: IO.() -> Unit) {
        block()
        out.flush()
    }
}