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 C# (judge verdict: Accepted)

using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Globalization;
using System.Diagnostics;
using Pair = System.Collections.Generic.KeyValuePair<int, int>;

class Program
{
    static void Main()
    {
        Console.SetOut(new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false });
        new Program().Solve();
        Console.Out.Flush();
    }
    Scanner cin = new Scanner();


    void Solve()
    {
        int n = cin.Nextint, q = cin.Nextint;
        var P = new BIT(n + 1);
        var Q = new BIT(n + 1);

        for (int i = 0; i < q; i++)
        {
            if (cin.Nextint == 0)
            {
                int a = cin.Nextint, b = cin.Nextint, x = cin.Nextint;
                P.Add(a, -a * x); P.Add(b + 1, (b + 1) * x);
                Q.Add(a, x); Q.Add(b + 1, -x);
            }
            else
            {
                int a = cin.Nextint, b = cin.Nextint;
                Console.WriteLine(P.Sum(b + 1) - P.Sum(a) + (b + 1) * Q.Sum(b + 1) - a * Q.Sum(a));
            }
        }
    }

}

class BIT //[1, n]
{
    int n;
    long[] bit;

    public BIT(int N)
    {
        n = N;
        bit = new long[n + 1];
    }

    public void Add(int i, long x)
    {
        while (i <= n)
        {
            bit[i] += x;
            i += i & -i;
        }
    }

    // [1, i] sum
    public long Sum(int i)
    {
        long s = 0;
        while(i > 0)
        {
            s = s + bit[i];
            i -= i & -i;
        }
        return s;
    }

    //[a, b] sum
    public long Sum(int a, int b)
    {
        return Sum(b) - Sum(a - 1);
    }
}
class Scanner
{
    string[] s; int i;
    char[] cs = new char[] { ' ' };
    public Scanner() { s = new string[0]; i = 0; }
    public string[] Scan { get { return Console.ReadLine().Split(); } }
    public int[] Scanint { get { return Array.ConvertAll(Scan, int.Parse); } }
    public long[] Scanlong { get { return Array.ConvertAll(Scan, long.Parse); } }
    public double[] Scandouble { get { return Array.ConvertAll(Scan, double.Parse); } }
    public string Next
    {
        get
        {
            if (i < s.Length) return s[i++];
            string st = Console.ReadLine();
            while (st == "") st = Console.ReadLine();
            s = st.Split(cs, StringSplitOptions.RemoveEmptyEntries);
            i = 0;
            return Next;
        }
    }
    public int Nextint { get { return int.Parse(Next); } }
    public long Nextlong { get { return long.Parse(Next); } }
    public double Nextdouble { get { return double.Parse(Next); } }
}