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)

trait Monoid {
    type T: Clone;
    fn id() -> Self::T;
    fn op(a: &Self::T, b: &Self::T) -> Self::T;
}

#[derive(Debug)]
struct SegmentTree<M: Monoid> {
    n: usize,
    vec: Vec<M::T>,
}

impl<M: Monoid> SegmentTree<M> {
    fn new(n: usize) -> Self {
        let n = n.next_power_of_two();
        SegmentTree {
            n: n,
            vec: vec![M::id().clone(); 2 * n - 1],
        }
    }

    // vec[k] = a
    fn update(&mut self, k: usize, a: M::T) {
        let mut k = k + self.n - 1;
        self.vec[k] = a;
        self.up(k);
    }

    // vec[k] = op(vec[k], a)
    fn add(&mut self, k: usize, a: M::T) {
        let mut k = k + self.n - 1;
        self.vec[k] = M::op(&self.vec[k], &a);
        self.up(k);
    }

    fn up(&mut self, mut k: usize) {
        while k > 0 {
            k = (k - 1) / 2;
            self.vec[k] = M::op(&self.vec[2 * k + 1], &self.vec[2 * k + 2]);
        }
    }

    fn query_sub(&self, a: usize, b: usize, k: usize, l: usize, r: usize) -> M::T {
        if r <= a || b <= l {
            M::id()
        } else if a <= l && r <= b {
            self.vec[k].clone()
        } else {
            M::op(
                &self.query_sub(a, b, k * 2 + 1, l, (l + r) / 2),
                &self.query_sub(a, b, k * 2 + 2, (l + r) / 2, r),
            )
        }
    }

    // op [a, b)
    fn query(&self, a: usize, b: usize) -> M::T {
        self.query_sub(a, b, 0, 0, self.n)
    }

    // vec[k]
    fn get(&self, k: usize) -> M::T {
        self.vec[k + self.n - 1].clone()
    }
}

struct SUM;
impl Monoid for SUM {
    type T = i64;
    fn id() -> Self::T {
        0_i64
    }
    fn op(a: &Self::T, b: &Self::T) -> Self::T {
        *a + *b
    }
}

use std::io::*;
use std::str::FromStr;

fn main() {
    let n: usize = read();
    let q: usize = read();
    let mut seg: SegmentTree<SUM> = SegmentTree::new(n);
    for _ in 0..q {
        let com: usize = read();
        let x: usize = read();
        let y: usize = read();
        if com == 0 {
            seg.add(x - 1, y as i64);
        } else {
            println!("{}", seg.query(x - 1, y));
        }
    }
}

fn read<T: FromStr>() -> T {
    let stdin = stdin();
    let stdin = stdin.lock();
    let token: String = stdin
        .bytes()
        .map(|c| c.unwrap() as char)
        .skip_while(|c| c.is_whitespace())
        .take_while(|c| !c.is_whitespace())
        .collect();
    token.parse().ok().unwrap()
}