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)

#[doc = " https://github.com/hatoo/competitive-rust-snippets"]
#[allow(unused_imports)]
use std::cmp::{max, min, Ordering};
#[allow(unused_imports)]
use std::collections::{BTreeMap, BTreeSet, BinaryHeap, HashMap, HashSet, VecDeque};
#[allow(unused_imports)]
use std::io::{stdin, stdout, BufWriter, Write};
#[allow(unused_imports)]
use std::iter::FromIterator;
#[allow(unused_macros)]
macro_rules ! debug { ( $ ( $ a : expr ) ,* ) => { eprintln ! ( concat ! ( $ ( stringify ! ( $ a ) , " = {:?}, " ) ,* ) , $ ( $ a ) ,* ) ; } }
#[macro_export]
macro_rules ! input { ( source = $ s : expr , $ ( $ r : tt ) * ) => { let mut parser = Parser :: from_str ( $ s ) ; input_inner ! { parser , $ ( $ r ) * } } ; ( parser = $ parser : ident , $ ( $ r : tt ) * ) => { input_inner ! { $ parser , $ ( $ r ) * } } ; ( new_stdin_parser = $ parser : ident , $ ( $ r : tt ) * ) => { let stdin = std :: io :: stdin ( ) ; let reader = std :: io :: BufReader :: new ( stdin . lock ( ) ) ; let mut $ parser = Parser :: new ( reader ) ; input_inner ! { $ parser , $ ( $ r ) * } } ; ( $ ( $ r : tt ) * ) => { input ! { new_stdin_parser = parser , $ ( $ r ) * } } ; }
#[macro_export]
macro_rules ! input_inner { ( $ parser : ident ) => { } ; ( $ parser : ident , ) => { } ; ( $ parser : ident , $ var : ident : $ t : tt $ ( $ r : tt ) * ) => { let $ var = read_value ! ( $ parser , $ t ) ; input_inner ! { $ parser $ ( $ r ) * } } ; }
#[macro_export]
macro_rules ! read_value { ( $ parser : ident , ( $ ( $ t : tt ) ,* ) ) => { ( $ ( read_value ! ( $ parser , $ t ) ) ,* ) } ; ( $ parser : ident , [ $ t : tt ; $ len : expr ] ) => { ( 0 ..$ len ) . map ( | _ | read_value ! ( $ parser , $ t ) ) . collect ::< Vec < _ >> ( ) } ; ( $ parser : ident , chars ) => { read_value ! ( $ parser , String ) . chars ( ) . collect ::< Vec < char >> ( ) } ; ( $ parser : ident , usize1 ) => { read_value ! ( $ parser , usize ) - 1 } ; ( $ parser : ident , $ t : ty ) => { $ parser . next ::<$ t > ( ) . expect ( "Parse error" ) } ; }
use std::io;
use std::io::BufRead;
use std::str;
pub struct Parser<R> {
    reader: R,
    buf: Vec<u8>,
    pos: usize,
}
impl Parser<io::Empty> {
    pub fn from_str(s: &str) -> Parser<io::Empty> {
        Parser {
            reader: io::empty(),
            buf: s.as_bytes().to_vec(),
            pos: 0,
        }
    }
}
impl<R: BufRead> Parser<R> {
    pub fn new(reader: R) -> Parser<R> {
        Parser {
            reader: reader,
            buf: vec![],
            pos: 0,
        }
    }
    pub fn update_buf(&mut self) {
        self.buf.clear();
        self.pos = 0;
        loop {
            let (len, complete) = {
                let buf2 = self.reader.fill_buf().unwrap();
                self.buf.extend_from_slice(buf2);
                let len = buf2.len();
                if len == 0 {
                    break;
                }
                (len, buf2[len - 1] <= 0x20)
            };
            self.reader.consume(len);
            if complete {
                break;
            }
        }
    }
    pub fn next<T: str::FromStr>(&mut self) -> Result<T, T::Err> {
        loop {
            let mut begin = self.pos;
            while begin < self.buf.len() && (self.buf[begin] <= 0x20) {
                begin += 1;
            }
            let mut end = begin;
            while end < self.buf.len() && (self.buf[end] > 0x20) {
                end += 1;
            }
            if begin != self.buf.len() {
                self.pos = end;
                return str::from_utf8(&self.buf[begin..end]).unwrap().parse::<T>();
            } else {
                self.update_buf();
            }
        }
    }
}
#[allow(unused_macros)]
macro_rules ! debug { ( $ ( $ a : expr ) ,* ) => { eprintln ! ( concat ! ( $ ( stringify ! ( $ a ) , " = {:?}, " ) ,* ) , $ ( $ a ) ,* ) ; } }
#[doc = " https://github.com/hatoo/competitive-rust-snippets"]
const BIG_STACK_SIZE: bool = true;
#[allow(dead_code)]
fn main() {
    use std::thread;
    if BIG_STACK_SIZE {
        thread::Builder::new()
            .stack_size(32 * 1024 * 1024)
            .name("solve".into())
            .spawn(solve)
            .unwrap()
            .join()
            .unwrap();
    } else {
        solve();
    }
}
trait SEGImpl {
    type Elem: Clone;
    fn id() -> Self::Elem;
    // node value 0, node value 1 -> node value
    fn op(x: &Self::Elem, y: &Self::Elem) -> Self::Elem;
    // propagate lazy value upward
    fn up(l: usize, r: usize, e: Self::Elem) -> Self::Elem;
    /// current value, lazy value -> new value, child's lazy value
    fn down(cur: Self::Elem, lazy_val: Self::Elem) -> (Self::Elem, Self::Elem);
    // current lazy value, propagated lazy value -> new lazy value
    fn lazy_op(cur: &Self::Elem, propagated: &Self::Elem) -> Self::Elem;
}
struct SEG<T: SEGImpl> {
    n: usize,
    node: Vec<T::Elem>,
    lazy: Vec<Option<T::Elem>>,
}

impl <T: SEGImpl> SEG<T> {
    fn new(n: usize) -> SEG<T> {
        let mut m = 1;
        while m < n { m *= 2; }
        SEG {
            n: m,
            node: vec![T::id(); m*2-1],
            lazy: vec![None; m*2-1],
        }
    }
    fn eval(&mut self, k: usize, l: usize, r: usize) {
        if let Some(lzv) = self.lazy[k].clone() {
            let cur_val = self.node[k].clone();
            let (new_val, child_lzv) = T::down(cur_val, lzv);
            self.node[k] = new_val;
            // propagate the lazy value to its children
            if r - l > 1 {
                self.lazy[k*2+1] = match self.lazy[k*2+1].clone() {
                    Some(x) => Some(T::lazy_op(&x, &child_lzv)),
                    None => Some(child_lzv.clone()),
                };
                self.lazy[k*2+2] = match self.lazy[k*2+2].clone() {
                    Some(x) => Some(T::lazy_op(&x, &child_lzv)),
                    None => Some(child_lzv.clone()),
                };
            }
            self.lazy[k] = None;
        }
    }
    fn do_update(&mut self, a: usize, b: usize, x: T::Elem, k: usize, l: usize, r: usize) {
        self.eval(k,l,r);

        if b <= l || r <= a {
            return;
        }

        if a <= l && r <= b {
            self.lazy[k] = match self.lazy[k].clone() {
                Some(a) => Some(T::lazy_op(&a, &T::up(l,r,x))),
                None => Some(T::up(l,r,x)),
            };
            self.eval(k,l,r);
        }

        else {
            self.do_update(a,b,x.clone(),2*k+1,l,(l+r)/2);
            self.do_update(a,b,x.clone(),2*k+2,(l+r)/2,r);
            self.node[k] = T::op(&self.node[2*k+1],&self.node[2*k+2]);
        }
    }
    fn update(&mut self, a: usize, b: usize, x: T::Elem) {
        let n = self.n;
        self.do_update(a,b,x,0,0,n)
    }
    fn do_query(&mut self, a: usize, b: usize, k: usize, l: usize, r: usize) -> T::Elem {
        if r <= a || b <= l {
            return T::id();
        }

        self.eval(k,l,r);

        if a <= l && r <= b {
            return self.node[k].clone();
        } else {
            let vl = self.do_query(a,b,k*2+1,l,(l+r)/2);
            let vr = self.do_query(a,b,k*2+2,(l+r)/2,r);
            return T::op(&vl,&vr);
        }
    }
    fn query(&mut self, a: usize, b: usize) -> T::Elem {
        let n = self.n;
        self.do_query(a,b,0,0,n)
    }
}
struct Impl;
impl SEGImpl for Impl {
    type Elem = i64;
    fn id() -> Self::Elem {
        0
    }
    fn op(x: &Self::Elem, y: &Self::Elem) -> Self::Elem {
        x.clone() + y.clone()
    }
    fn up(l: usize, r: usize, e: Self::Elem) -> Self::Elem {
        e * (r - l) as i64
    }
    fn down(cur: Self::Elem, lazy_val: Self::Elem) -> (Self::Elem, Self::Elem) {
        (cur+lazy_val, lazy_val/2)
    }
    fn lazy_op(x: &Self::Elem, y: &Self::Elem) -> Self::Elem {
        x.clone() + y.clone()
    }
}
fn solve() {
    input! {
        new_stdin_parser = parser,
        n: usize, q: usize,
    }

    let mut seg: SEG<Impl> = SEG::new(n);

    for _ in 0..q {
        input! {
            parser = parser,
            com: usize,
        }
        if com == 0 {
            input! {
                parser = parser,
                s: usize, t: usize, x: i64,
            }
            seg.update(s-1,t,x);
        } else {
            input! {
                parser = parser,
                i: usize, j: usize,
            }
            println!("{}", seg.query(i-1,j));
        }
    }
}