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)

macro_rules! debug {
    ( $x:ident [ $i:expr ] ) => {
        if cfg!(debug_assertions) {
            println!("{}[{}] = {:?}", stringify!($x), $i, $x[$i]);
        }
    };
    ( $x:ident [ $i:expr ][ $j:expr ] ) => {
        if cfg!(debug_assertions) {
            println!("{}[{}][{}] = {:?}", stringify!($x), $i, $j, $x[$i][$j]);
        }
    };
    ( $x:expr ) => {
        if cfg!(debug_assertions) {
            println!("{}:{} {} = {:?}", line!(), column!(), stringify!($x), $x);
        }
    };
}

#[allow(non_snake_case)]
fn main() {
    use std::io::Write;
    let stdin = std::io::stdin();
    let mut stdin = my::io::InputReader::new(stdin.lock());
    let mut stdout = std::io::stdout();
    let mut stdout = std::io::BufWriter::new(stdout);

    let n: usize = stdin.read();
    let q: usize = stdin.read();
    let mut st = SegmentTree::new(n, |x, y| { x + y }, 0_i32);
    for _ in 0..q {
        let com: i32 = stdin.read();
        if com == 0 {
            let x: usize = stdin.read::<usize>() - 1;
            let y: i32 = stdin.read();
            let ax = st.query(x, x + 1);
            st.update(x, ax + y);
        } else {
            let x: usize = stdin.read::<usize>() - 1;
            let y: usize = stdin.read();
            stdout.write_fmt(format_args!("{}\n", st.query(x, y)));
        }
    }
}


pub fn clp(mut x: usize) -> usize {
    x -= 1;
    x = x | (x >>  1);
    x = x | (x >>  2);
    x = x | (x >>  4);
    x = x | (x >>  8);
    x = x | (x >> 16);
    x = x | (x >> 32);
    x + 1
}

struct SegmentTree<T, F> where
    T: Copy,
    F: Fn(T, T) -> T
{
    n: usize,
    f: F,
    unit: T,
    a: Vec<T>
}

impl<T, F> SegmentTree<T, F> where
    T: Copy,
    F: Fn(T, T) -> T
{
    pub fn new(m: usize, f: F, unit: T) -> SegmentTree<T, F> {
        let n = clp(m);
        SegmentTree {
            n: n,
            f: f,
            unit: unit,
            a: vec![unit; 2 * n - 1]
        }
    }

    pub fn update(&mut self, mut i: usize, x: T) {
        i += self.n - 1;
        self.a[i] = x;
        while i != 0 {
            i = (i - 1) / 2;
            self.a[i] = (self.f)(self.a[2 * i + 1], self.a[2 * i + 2]);
        }
    }

    fn _query(&self, query_first: usize, query_last: usize, node: usize, node_first: usize, node_last: usize) -> T {
        if (node_last <= query_first || query_last <= node_first) {
            self.unit
        } else if (query_first <= node_first && node_last <= query_last) {
            self.a[node]
        } else {
            let node_mid: usize = (node_first + node_last) / 2;
            let vl: T = self._query(query_first, query_last, node * 2 + 1, node_first, node_mid);
            let vr: T = self._query(query_first, query_last, node * 2 + 2, node_mid, node_last);
            (self.f)(vl, vr)
        }
    }

    pub fn query(&self, query_first: usize, query_last: usize) -> T {
        self._query(query_first, query_last, 0, 0, self.n)
    }
}

#[allow(unused_must_use)]
mod my {
    pub mod io {
        use std::io::{BufRead, ErrorKind, Result};
        use std::str::{FromStr, from_utf8};
        use std::fmt::Debug;

        pub fn read_word<R: BufRead>(r: &mut R, buf: &mut Vec<u8>)
                                              -> Result<usize> {
            let mut read = 0;
            loop {
                let (done, used) = {
                    let available = match r.fill_buf() {
                        Ok(n) => n,
                        Err(ref e) if e.kind() == ErrorKind::Interrupted => continue,
                        Err(e) => return Err(e)
                    };
                    match available.iter().position(|&x| x == b'\n' || x == b' ') {
                        Some(i) => {
                            buf.extend_from_slice(&available[..i]);
                            (true, i + 1)
                        }
                        None => {
                            buf.extend_from_slice(available);
                            (false, available.len())
                        }
                    }
                };
                r.consume(used);
                read += used;
                if done || used == 0 {
                    return Ok(read);
                }
            }
        }

        pub struct InputReader<R: BufRead> {
            reader: R,
            buf: Vec<u8>,
        }

        impl<R: BufRead> InputReader<R> {
            pub fn new(reader: R) -> InputReader<R> {
                InputReader {
                    reader: reader,
                    buf: Vec::<u8>::new(),
                }
            }

            pub fn read_word(&mut self) {
                read_word(&mut self.reader, &mut self.buf);
            }
            
            pub fn read<T>(&mut self) -> T
                where T: FromStr,
                      T::Err: Debug
            {
                self.buf.clear();
                self.read_word();
                from_utf8(&self.buf).unwrap().trim().parse().unwrap()
            }
        }
    }
}