Problem:
Range Sum Query

Write a program which manipulates a sequence A = {a1, a2, . . . , an} with the following operations:

add(i, x): add x to ai.

getSum(s, t): print the sum of as, as+1,...,at.

Note that the initial values of ai (i = 1, 2, . . . , n) are 0.

Input

n q
com1 x1 y1
com2 x2 y2
...
comq xq yq

In the first line, n (the number of elements in A) and q (the number of queries) are given. Then, q queries are given where com represents the type of queries. '0' denotes add(xi, yi) and '1' denotes getSum(xi, yi).

Output

For each getSum operation, print the sum in a line.

Constraints

1 ≤ n ≤ 100000

1 ≤ q ≤ 100000

If comi is 0, then 1 ≤ xi ≤ n, 0 ≤ yi ≤ 1000.

If comi is 1, then 1 ≤ xi ≤ n, 1 ≤ yi ≤ n.

Sample Input 1

3 5
0 1 1
0 2 2
0 3 3
1 1 2
1 2 2

Sample Output 1

3
2

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 {}

use std::rc::Rc;

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

struct Node<T: Monoid> {
    data: T,
    left: Link<T>,
    right: Link<T>,
}

impl<T: Monoid> Node<T> {
    fn new(data: T) -> Self {
        Node { data: data, left: None, right: None }
    }
    fn build(l: usize, r: usize) -> Self {
        if l + 1 >= r { Node::new(T::identity()) }
        else {
            Node {
                data: T::identity(),
                left: Some(Rc::new(Node::build(l, (l + r) >> 1))),
                right: Some(Rc::new(Node::build((l + r) >> 1, r))),
            }
        }
    }
    fn update(&self, i: usize, x: T, l: usize, r: usize) -> Self {
        assert!(l <= i && i < r);
        if i == l && i + 1 == r { Node::new(x) }
        else if l <= i && i < ((l + r) >> 1) {
            let left = Some(Rc::new(self.left.as_ref().unwrap().update(i, x, l, (l + r) >> 1)));
            let right = self.right.clone();
            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() }),
                left: left,
                right: right,
            }
        }
        else {
            let left = self.left.clone();
            let right = Some(Rc::new(self.right.as_ref().unwrap().update(i, x, (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() }),
                left: left,
                right: right,
            }
        }
    }
    fn fold(&self, a: usize, b: usize, l: usize, r: usize) -> T {
        if a <= l && r <= b { 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), None => T::identity() }
                .op(& match self.right.as_ref() { Some(n) => n.fold(a, b, (l + r) >> 1, r), None => T::identity() })
        }
    }
}

impl<T: Monoid> Drop for Node<T> {
    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 PersistentSegmentTree<T: Monoid> {
    root: Node<T>,
    sz: usize,
}

impl<T: Monoid> PersistentSegmentTree<T> {
    pub fn new(n: usize) -> Self {
        Self { root: Node::build(0, n), sz: n }
    }
    pub fn update(&self, i: usize, x: T) -> Self {
        Self { root: self.root.update(i, x, 0, self.sz), sz: self.sz }
    }
    pub fn fold(&self, l: usize, r: usize) -> T {
        self.root.fold(l, r, 0, self.sz)
    }
}

#[derive(Clone)]
struct Am(usize);

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

impl Unital for Am {
    fn identity() -> Self { Am(0) }
}

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 = PersistentSegmentTree::<Am>::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 => {
                let new_val = seg.fold(x[1] - 1, x[1]).op(&Am(x[2] as usize));
                seg = seg.update(x[1] - 1, new_val);
            }
            _ => {
                println!("{}", seg.fold(x[1] - 1, x[2]).0);
            }
        }
    }
}