Problem:
RSQ and RAQ Write a program which manipulates a sequence A = { a 1 , a 2 , . . .  , a n } with the following operations:  add(s, t, x) : add x to a s , a s+1 , ..., a t .  getSum(s, t) : report the sum of a s , a s+1 , ..., a t .  Note that the initial values of a i ( i = 1, 2, . . .  , n ) are 0. Input  n  q  query 1  query 2 : query q  In the first line, n (the number of elements in A ) and q (the number of queries) are given. Then, i th query query i 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 (Python):


import sys

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7

class BIT2:
    """ 区間加算BIT(区間加算・区間合計取得) """

    def __init__(self, N):
        N += 1
        self.N = N
        self.data0 = [0] * N
        self.data1 = [0] * N

    def _add(self, data, k, x):
        k += 1
        while k < self.N:
            data[k] += x
            k += k & -k

    def _get(self, data, k):
        k += 1
        s = 0
        while k:
            s += data[k]
            k -= k & -k
        return s

    def add(self, l, r, x):
        """ 区間[l,r)に値xを追加 """

        self._add(self.data0, l, -x*(l-1))
        self._add(self.data0, r, x*(r-1))
        self._add(self.data1, l, x)
        self._add(self.data1, r, -x)

    def query(self, l, r):
        """ 区間[l,r)の和を取得 """

        return self._get(self.data1, r-1) * (r-1) + self._get(self.data0, r-1) \
             - self._get(self.data1, l-1) * (l-1) - self._get(self.data0, l-1)

N, Q = MAP()
bit = BIT2(N+1)

for i in range(Q):
    cmd, *arg = MAP()
    if cmd == 0:
        s, t, x = arg
        bit.add(s, t+1, x)
    else:
        s, t = arg
        print(bit.query(s, t+1))

Solution (Python):
class BinaryIndexedTree():
    def __init__(self, n):
        self.n = n
        self.dat = [0] * (n + 1)

    def sum(self, i):
        s = 0
        while i:
            s += self.dat[i]
            i -= i & -i
        return s

    def add(self, i, x):
        while i <= self.n:
            self.dat[i] += x
            i += i & -i


N, Q = [int(_) for _ in input().split()]
Query = [input().split() for _ in range(Q)]
BIT0 = BinaryIndexedTree(N + 1)
BIT1 = BinaryIndexedTree(N + 1)



for query in Query:
    c, *array = query
    array = list(map(int, array))
    if c == '1':
        a, b = array
        a -= 1
        res = 0
        res += b * BIT1.sum(b) + BIT0.sum(b)
        res -= a * BIT1.sum(a) + BIT0.sum(a)
        print(res)
    else:
        a, b, x = array
        b += 1
        BIT1.add(a, x)
        BIT0.add(a, -(a - 1) * x)
        BIT1.add(b, -x)
        BIT0.add(b, (b - 1) * x)

Solution (Python):
class BinaryIndexedTree(object):
    def __init__(self, n: int) -> None:
        self.size = n
        self.bit = [0] * (self.size + 1)

    def add(self, i: int, v: int) -> None:
        while (i <= self.size):
            self.bit[i] += v
            i += (i & -i)  

    def sum(self, i: int) -> int:
        s = 0
        while (i > 0):
            s += self.bit[i]
            i -= (i & -i)
        return s


class RangeQuery(object):
    def __init__(self, n: int) -> None:
        self.size = n
        self.bit1 = BinaryIndexedTree(n + 1)
        self.bit2 = BinaryIndexedTree(n + 1)

    def add(self, i: int, j: int, v: int) -> None:
        self.bit1.add(i, v * -i)
        self.bit1.add(j + 1, v * (j + 1))
        self.bit2.add(i, v)
        self.bit2.add(j + 1, -v)

    def sum(self, i: int, j: int) -> int:
        s = self.bit1.sum(j + 1) + (j + 1) * self.bit2.sum(j + 1)
        s -= self.bit1.sum(i) + i * self.bit2.sum(i)
        return s


if __name__ == "__main__":
    n, q = map(lambda x: int(x), input().split())
    rq = RangeQuery(n)

    for _ in range(q):
        com, *v = map(lambda x: int(x), input().split())
        if (0 == com):
            rq.add(v[0], v[1], v[2])
        else:
            print(rq.sum(v[0], v[1]))

Solution (Python):
class BIT:
    def __init__(self, n):
        self.size = n
        self.tree = [0]*(n+1)
    
    def Sum(self,i):
        s = 0
        while i > 0:
            s += self.tree[i]
            i -= i & -i
        return s
    
    def add(self,i,x):
        while i <= self.size:
            self.tree[i] += x
            i += i & -i

class RangeUpdate:
    def __init__(self, n):
        self.p = BIT(n+1)
        self.q = BIT(n+1)
        
    def add(self,s,t,x):
        t+=1
        self.p.add(s,-x*s)
        self.p.add(t,x*t)
        self.q.add(s,x)
        self.q.add(t,-x)
        
    def Sum(self,s,t):
        t+=1
        return self.p.Sum(t) + self.q.Sum(t)*t-self.p.Sum(s)-self.q.Sum(s)*s
    
n, q = map(int, input().split())
tree = RangeUpdate(n)
for _ in range(q):
    a, *b = map(int,input().split())
    if a == 0:
        tree.add(b[0],b[1],b[2])
    else:
        print(tree.Sum(b[0],b[1]))

Solution (Python):
import sys
readline = sys.stdin.readline
write = sys.stdout.write
N, Q = map(int, readline().split())

data0 = [0]*(N+1)
data1 = [0]*(N+1)
def _add(data, k, x):
    while k <= N:
        data[k] += x
        k += k & -k
def add(l, r, x):
    _add(data0, l, -x*(l-1))
    _add(data0, r, x*(r-1))
    _add(data1, l, x)
    _add(data1, r, -x)

def _get(data, k):
    s = 0
    while k:
        s += data[k]
        k -= k & -k
    return s
def query(l, r):
    return _get(data1, r-1) * (r-1) + _get(data0, r-1) - _get(data1, l-1) * (l-1) - _get(data0, l-1)


ans = []
for q in range(Q):
    t, *cmd = map(int, readline().split())
    if t:
        s, t = cmd
        ans.append(str(query(s, t+1)))
    else:
        s, t, x = cmd
        add(s, t+1, x)
write("\n".join(ans))
write("\n")

Solution (Python):





class BinaryIndexedTree:
    def __init__(self, n):
        self.size = n
        self.bit = [0] * (self.size+1)

    def add(self, i, v):
        while i <= self.size:
            self.bit[i] += v
            i += (i & -i)

    def sum(self, i):
        s = 0
        while i > 0:
            s += self.bit[i]
            i -= (i & -i)
        return s


class RangeQuery:
    def __init__(self, n):
        self.size = n
        self.bit1 = BinaryIndexedTree(n+1)
        self.bit2 = BinaryIndexedTree(n+1)

    def add(self, i, j, v):
        self.bit1.add(i, v * -i)
        self.bit1.add(j+1, v * (j+1))
        self.bit2.add(i, v)
        self.bit2.add(j+1, -v)

    def sum(self, i, j):
        s = self.bit1.sum(j+1) + (j+1)*self.bit2.sum(j+1)
        s -= self.bit1.sum(i) + i*self.bit2.sum(i)
        return s


def run():
    n, q = [int(i) for i in input().split()]

    r = RangeQuery(n)

    for _ in range(q):
        com, *args = input().split()
        if com == '0':
            s, t, x = [int(i) for i in args]
            r.add(s, t, x)
        elif com == '1':
            s, t = [int(i) for i in args]
            print(r.sum(s, t))


if __name__ == '__main__':
    run()