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

// ---------- begin Lazy Segment Tree ----------
mod segment_tree {
    pub struct Lazy<T, E, F, G, H> {
        n: usize,
        k: usize,
        a: Vec<(T, E)>,
        e: T,
        id: E,
        f: F,
        g: G,
        h: H,
    }
    #[allow(dead_code)]
    impl<T: Clone, E: Clone, F: Fn(T, T) -> T, G: Fn(T, E) -> T, H: Fn(E, E) -> E> Lazy<T, E, F, G, H> {
        pub fn new(n: usize, e: T, id: E, f: F, g: G, h: H) -> Lazy<T, E, F, G, H> {
            let mut k = 0;
            while (1 << k) < n {
                k += 1;
            }
            Lazy {
                n: 1 << k,
                k: k,
                a: vec![(e.clone(), id.clone()); 2 << k],
                e: e,
                id: id,
                f: f,
                g: g,
                h: h,
            }
        }
        pub fn build_by(z: &Vec<T>, e: T, id: E, f: F, g: G, h: H) -> Lazy<T, E, F, G, H> {
            let n = z.len();
            let mut k = 0;
            while (1 << k) < n {
                k += 1;
            }
            let mut a = vec![(e.clone(), id.clone()); 2 << k];
            for i in 0..n {
                a[(1 << k) + i].0 = z[i].clone();
            }
            for i in (1..(1 << k)).rev() {
                let l = g(a[2 * i].0.clone(), id.clone());
                let r = g(a[2 * i + 1].0.clone(), id.clone());
                a[i].0 = f(l, r);
            }
            Lazy {
                n: 1 << k,
                k: k,
                a: a,
                e: e,
                id: id,
                f: f,
                g: g,
                h: h,
            }
        }
        fn eval (&self, x: usize) -> T {
            (self.g)(self.a[x].0.clone(), self.a[x].1.clone())
        }
        fn propagate (&mut self, mut x: usize) {
            x += self.n;
            for k in (1..(self.k + 1)).rev() {
                let y = x >> k;
                self.a[2 * y].1 = (self.h)(self.a[2 * y].1.clone(), self.a[y].1.clone());
                self.a[2 * y + 1].1 = (self.h)(self.a[2 * y + 1].1.clone(), self.a[y].1.clone());
                self.a[y].1 = self.id.clone();
                self.a[y].0 = (self.f)(self.eval(2 * y), self.eval(2 * y + 1));
            }
        }
        fn save (&mut self, x: usize) {
            let mut k = (x + self.n) >> 1;
            while k > 0 {
                self.a[k].0 = (self.f)(self.eval(2 * k), self.eval(2 * k + 1));
                k >>= 1;
            }
        }
        pub fn update (&mut self, l: usize, r: usize, p: E) {
            self.propagate(l);
            self.propagate(r - 1);
            let mut x = l + self.n;
            let mut y = r + self.n;
            while x < y {
                if (x & 1) == 1 {
                    self.a[x].1 = (self.h)(self.a[x].1.clone(), p.clone());
                    x += 1;
                }
                if (y & 1) == 1 {
                    y -= 1;
                    self.a[y].1 = (self.h)(self.a[y].1.clone(), p.clone());
                }
                x >>= 1;
                y >>= 1;
            }
            self.save(l);
            self.save(r - 1);
        }
        pub fn find (&mut self, l: usize, r: usize) -> T {
            self.propagate(l);
            self.propagate(r - 1);
            let mut p = self.e.clone();
            let mut q = self.e.clone();
            let mut x = l + self.n;
            let mut y = r + self.n;
            while x < y {
                if (x & 1) == 1 {
                    p = (self.f)(p, self.eval(x));
                    x += 1;
                }
                if (y & 1) == 1 {
                    y -= 1;
                    q = (self.f)(self.eval(y), q);
                }
                x >>= 1;
                y >>= 1;
            }
            (self.f)(p, q)
        }
    }
}

// ---------- end Lazy Segment Tree ----------
//---------- begin scannner ----------
#[allow(dead_code)]
mod scanner {
    use std::str::FromStr;
    use std::str::SplitWhitespace;
    use std::io::Read;
    use std;
    pub struct Scanner<'a> {
        it: SplitWhitespace<'a>
    }
    impl<'a> Scanner<'a> {
        pub fn new(s: &'a String) -> Scanner<'a> {
            Scanner {
                it: s.split_whitespace()
            }
        }
        pub fn next<T: FromStr>(&mut self) -> T {
            match self.it.next().unwrap().parse::<T>() {
                Ok(v) => v,
                _ => panic!("Scanner error")
            }
        }
        pub fn next_vec<T: FromStr>(&mut self, n: usize) -> Vec<T> {
            (0..n).map(|_| self.next()).collect::<Vec<T>>()
        }
        pub fn next_chars(&mut self) -> Vec<char> {
            self.next::<String>().chars().collect()
        }
    }
    pub fn read_string() -> String {
        let mut s = String::new();
        std::io::stdin().read_to_string(&mut s).unwrap();
        s
    }
}

//---------- end scannner ----------

use std::io::Write;

fn main() {
    let sc = scanner::read_string();
    let sc = scanner::Scanner::new(&sc);
    let out = std::io::stdout();
    let out = std::io::BufWriter::new(out.lock());
    run(sc, out);
}

fn run(mut sc: scanner::Scanner, mut out: std::io::BufWriter<std::io::StdoutLock>) {
    let n: usize = sc.next();
    let q: usize = sc.next();
    let f = |(a, b), (c, d)| (a + c, b + d);
    let g = |(a, b), x| (a + b * x, b);
    let h = |x, y| x + y;
    let mut s = segment_tree::Lazy::build_by(&vec![(0, 1); n], (0, 0), 0, f, g, h);
    for _ in 0..q {
        let t: u8 = sc.next();
        let l: usize = sc.next();
        let r: usize = sc.next();
        if t == 0 {
            let v: u64 = sc.next();
            s.update(l - 1, r, v);
        } else {
            writeln!(out, "{}", s.find(l - 1, r).0).unwrap();
        }
    }
}