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)

/* algebra */

pub trait Magma: Sized + Clone {
  fn op(&self, rhs: &Self) -> Self;
}

pub trait Associative: Magma {}

pub trait Unital: Magma {
  fn identity() -> Self;
}

pub trait Monoid: Magma + Associative + Unital {}

impl<T: Magma + Associative + Unital> Monoid for T {}

pub trait Effector: Monoid {
    type Target;
    fn effect(&self, t: &Self::Target) -> Self::Target;
}

use std::rc::Rc;

type Link<T, E> = Option<Rc<Node<T, E>>>;

struct Node<T: Monoid, E: Effector<Target=T>> {
    data: T,
    eff: E,
    left: Link<T, E>,
    right: Link<T, E>,
}

impl<T: Monoid, E: Effector<Target=T>> Node<T, E> {
    fn new(data: T) -> Self {
        Node { data: data, eff: E::identity(), left: None, right: None }
    }
    fn build(l: usize, r: usize) -> Self {
        if l + 1 >= r { Node::new(T::identity()) }
        else {
            let left = Some(Rc::new(Node::<T, E>::build(l, (l + r) >> 1)));
            let right = Some(Rc::new(Node::<T, E>::build((l + r) >> 1, r)));
            Node {
                data: match left.as_ref() { Some(n) => n.data.clone(), None => T::identity() }
                      .op(& match right.as_ref() { Some(n) => n.data.clone(), None => T::identity() }),
                eff: E::identity(),
                left: left,
                right: right,
            }
        }
    }

    fn effect_range(&self, a: usize, b: usize, new_eff: E, l: usize, r: usize, fold_eff: E) -> Self {
        if a <= l && r <= b {
            let eff = fold_eff.op(&new_eff);
            Node {
                data: eff.effect(&self.data),
                eff: self.eff.op(&eff),
                left: self.left.clone(),
                right: self.right.clone(),
            }
        }
        else if r <= a || b <= l {
            Node {
                data: fold_eff.effect(&self.data),
                eff: self.eff.op(&fold_eff),
                left: self.left.clone(),
                right: self.right.clone(),
            }
        }
        else {
            let left = Some(Rc::new(self.left.as_ref().unwrap().effect_range(a, b, new_eff.clone(), l, (l + r) >> 1, self.eff.op(&fold_eff))));
            let right = Some(Rc::new(self.right.as_ref().unwrap().effect_range(a, b, new_eff.clone(), (l + r) >> 1, r, self.eff.op(&fold_eff))));
            Node {
                data: match left.as_ref() { Some(n) => n.data.clone(), None => T::identity() }
                      .op(& match right.as_ref() { Some(n) => n.data.clone(), None => T::identity() }),
                eff: E::identity(),
                left: left,
                right: right,
            }
        }
    }

    fn fold(&self, a: usize, b: usize, l: usize, r: usize, eff: E) -> T {
        if a <= l && r <= b { eff.effect(&self.data.clone()) }
        else if r <= a || b <= l { T::identity() }
        else {
            match self.left.as_ref() { Some(n) => n.fold(a, b, l, (l + r) >> 1, self.eff.op(&eff)), None => T::identity() }
                .op(& match self.right.as_ref() { Some(n) => n.fold(a, b, (l + r) >> 1, r, self.eff.op(&eff)), None => T::identity() })
        }
    }
}

impl<T: Monoid, E: Effector<Target=T>> Drop for Node<T, E> {
    fn drop(&mut self) {
        if let Some(left) = self.left.take() {
            if let Ok(_) = Rc::try_unwrap(left) {}
        }
        if let Some(right) = self.right.take() {
            if let Ok(_) = Rc::try_unwrap(right) {}
        }
    }
}


pub struct PersistentLazySegmentTree<T: Monoid, E: Effector<Target=T>> {
    root: Node<T, E>,
    sz: usize,
}

impl<T: Monoid, E: Effector<Target=T>> PersistentLazySegmentTree<T, E> {
    pub fn new(n: usize) -> Self {
        Self { root: Node::build(0, n), sz: n }
    }
    pub fn effect_range(&self, l: usize, r: usize, eff: E) -> Self {
        Self { root: self.root.effect_range(l, r, eff, 0, self.sz, E::identity()), sz: self.sz }
    }
    pub fn fold(&self, l: usize, r: usize) -> T {
        self.root.fold(l, r, 0, self.sz, E::identity())
    }
}

#[derive(Clone, Debug)]
struct Sm(usize, usize);

impl Magma for Sm {
    fn op(&self, right: &Self) -> Self { Sm(self.0 + right.0, self.1 + right.1) }
}
impl Associative for Sm {}
impl Unital for Sm {
    fn identity() -> Self { Sm(0, 1) }
}

#[derive(Clone, Debug)]
struct Aq(usize);

impl Magma for Aq {
    fn op(&self, right: &Self) -> Self {
        Aq(self.0 + right.0)
    }
}
impl Associative for Aq {}
impl Unital for Aq {
    fn identity() -> Self { Aq(0) }
}
impl Effector for Aq {
    type Target = Sm;
    fn effect(&self, t: &Self::Target) -> Self::Target {
        Sm(t.0 + self.0 * t.1, t.1)
    }
}

fn main() {
    let mut s = String::new();
    std::io::stdin().read_line(&mut s).unwrap();
    let v:Vec<usize> = s.trim().split_whitespace()
        .map(|e|e.parse().unwrap()).collect();
    let (n,m) = (v[0] , v[1]);
    let mut seg = PersistentLazySegmentTree::new(n);
    for _ in 0..m {
        let mut t = String::new();
        std::io::stdin().read_line(&mut t).unwrap();
        let x:Vec<usize> = t.trim().split_whitespace()
            .map(|e|e.parse().unwrap()).collect();
        match x[0] {
            0 => {
                seg = seg.effect_range(x[1] - 1, x[2], Aq(x[3]));
            }
            _ => {
                println!("{}", seg.fold(x[1] - 1, x[2]).0);
            }
        }
    }
}