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)

use std::io::{stdin, Read, StdinLock};
use std::str::FromStr;

fn main() {
    let cin = stdin();
    let mut scan = Scanner::new(cin.lock());

    let n = scan.read::<usize>();
    let q = scan.read::<usize>();
    let mut segtree = SegRangeAdd::new(vec![0;n]);
    for _ in 0..q {
        let com = scan.read::<i32>();
        if com == 0 {
            let s = scan.read::<usize>()-1;
            let t = scan.read::<usize>()-1;
            let x = scan.read::<i64>();
            segtree.add(s..t+1, x);
        } else {
            let s = scan.read::<usize>()-1;
            let t = scan.read::<usize>()-1;
            println!("{}", segtree.sum(s..t+1));
        }
    }
}

use std::ops::Range;

fn contains(p: &Range<usize>, q: &Range<usize>) -> bool {
    p.start <= q.start && q.end <= p.end
}

fn separates(p: &Range<usize>, q: &Range<usize>) -> bool {
    p.end <= q.start || q.end <= p.start
}

fn size(p: &Range<usize>) -> usize {
    p.end - p.start
}

struct SegRangeAdd {
    node: Vec<i64>,
    lazy: Vec<i64>,
    n: usize,
}

impl SegRangeAdd {
    fn new(v: Vec<i64>) -> SegRangeAdd {
        let size = v.len();
        let mut n = 1;
        while n < size {n *= 2}
        let mut node = vec![0;2*n-1];
        let mut lazy = vec![0;2*n-1];
        for i in 0..size {node[i+n-1] = v[i]}
        for i in (0..n - 1).rev() {node[i] = node[i*2+1] + node[i*2+2]}
        SegRangeAdd {node: node, lazy: lazy, n: n}
    }

    fn eval(&mut self, k: usize, range: Range<usize>) {
        if self.lazy[k] != 0 {
            self.node[k] += self.lazy[k];
            if size(&range) > 1 {
                self.lazy[2*k+1] += self.lazy[k] / 2;
                self.lazy[2*k+2] += self.lazy[k] / 2;
            }
            self.lazy[k] = 0;
        }
    }

    fn add(&mut self, query: Range<usize>, x: i64) {
        let n = self.n;
        self.addrec(query, x, 0, 0..n)
    }

    fn addrec(&mut self, query: Range<usize>, x: i64, k: usize, range: Range<usize>) {
        self.eval(k, range.clone());
        if separates(&query, &range) {return}
        if contains(&query, &range) {
            self.lazy[k] += size(&range) as i64 * x;
            self.eval(k, range.clone());
        } else {
            self.addrec(query.clone(), x, 2*k+1, range.start..(range.start+range.end)/2);
            self.addrec(query.clone(), x, 2*k+2, (range.start+range.end)/2..range.end);
            self.node[k] = self.node[2*k+1] + self.node[2*k+2];
        }
    }

    fn sum(&mut self, query: Range<usize>) -> i64 {
        let n = self.n;
        self.sumrec(query, 0, 0..n)
    }

    fn sumrec(&mut self, query: Range<usize>, k: usize, range: Range<usize>) -> i64 {
        if separates(&query, &range) {return 0}
        self.eval(k, range.clone());
        if contains(&query, &range) {return self.node[k]}
        let vl = self.sumrec(query.clone(), 2*k+1, range.start..(range.start+range.end)/2);
        let vr = self.sumrec(query.clone(), 2*k+2, (range.start+range.end)/2..range.end);
        vl + vr
    }
}

// region template
#[allow(dead_code)]
fn uz(i: i32) -> usize { i as usize }

struct Scanner<'a> {
    cin: StdinLock<'a>,
}

#[allow(dead_code)]
impl<'a> Scanner<'a> {
    fn new(cin: StdinLock<'a>) -> Scanner<'a> {
        Scanner { cin: cin }
    }

    fn read1<T: FromStr>(&mut self) -> Option<T> {
        let token = self.cin.by_ref().bytes().map(|c| c.unwrap() as char)
            .skip_while(|c| c.is_whitespace())
            .take_while(|c| !c.is_whitespace())
            .collect::<String>();
        token.parse::<T>().ok()
    }

    fn read<T: FromStr>(&mut self) -> T {
        self.read1().unwrap()
    }

    fn readn<T: FromStr>(&mut self, n: i32) -> Vec<T> {
        let mut ret = vec![];
        for _ in 0..n { ret.push(self.read::<T>()) }
        ret
    }

    fn chars(&mut self) -> Vec<char> {
        self.read::<String>().chars().collect()
    }
}
// endregion