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)

use std::fmt::Debug;
use std::cmp::*;
use std::io::{stdin, Read};
use std::str::FromStr;

//mod lib;

fn read<T: FromStr>() -> T {
    let stdin = stdin();
    let stdin = stdin.lock();
    let token: String = stdin
        .bytes()
        .map(|c| c.expect("failed to read char") as char)
        .skip_while(|c| c.is_whitespace())
        .take_while(|c| !c.is_whitespace())
        .collect();

    return token.parse().ok().expect("failed to parse token");
}

fn read_vec1<T: FromStr>(n: usize) -> Vec<T> {
    return (0..n).map(|_| read()).collect();
}

fn read_vec2<T: FromStr>(n: usize, m: usize) -> Vec<Vec<T>> {
    return (0..n).map(|_| read_vec1(m)).collect();
}

static MOD: i64 = 1_000_000_007;

fn add_by_mod(a: &i64, b: &i64) -> i64 {
    return (a % MOD + b % MOD) % MOD;
}

pub trait Monoid {
    type T: Clone + Debug;
    fn op(lhs: &Self::T, rhs: &Self::T) -> Self::T;
    fn identity() -> Self::T;
}

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

impl<M: Monoid> SegmentTree<M> {
    pub fn new(n: usize) -> SegmentTree<M> {
        let mut size = 1;
        while size < n {
            size *= 2;
        }

        SegmentTree {
            size: size,
            node: vec![M::identity(); size * 2],
        }
    }

    pub fn set(&mut self, a: usize, v: M::T) {
        self.node[a + self.size] = v;
    }

    pub fn build(&mut self) {
        for k in self.size - 1..0 {
            self.node[k] = M::op(&self.node[k * 2], &self.node[k * 2 + 1]);
        }
    }

    pub fn update(&mut self, a: usize, v: M::T) {
        let mut k = a + self.size;
        self.node[k] = v;
        while k > 1 {
            k >>= 1;
            self.node[k] = M::op(&self.node[k * 2], &self.node[k * 2 + 1]);
        }
    }

    pub fn query(&self, a: usize, b: usize) -> M::T {
        let mut vleft = M::identity();
        let mut vright = M::identity();
        let mut left = self.size + a;
        let mut right = self.size + b;

        while left < right {
            if (left & 1) == 1 {
                vleft = M::op(&vleft, &self.node[left]);
                left += 1;
            }

            if (right & 1) == 1 {
                right -= 1;
                vright = M::op(&self.node[right], &vright);
            }

            left >>= 1;
            right >>= 1;
        }

        return M::op(&vleft, &vright);
    }

    pub fn debug(&self) {
        println!("{:?}", self.node);
    }
}

#[derive(Clone)]
struct Sum;

impl Monoid for Sum {
    type T = i64;
    fn identity() -> Self::T {
        return 0;
    }

    fn op(a: &Self::T, b: &Self::T) -> Self::T {
        return a + b;
    }
}

fn main() {
    let (n, q) = (read::<usize>(), read::<usize>());

    let mut seg: SegmentTree<Sum> = SegmentTree::new(n);
    for _ in 0..q {
        let vec = read_vec1::<usize>(3);
        if vec[0] == 0 {
            let tmp = seg.query(vec[1] - 1, vec[1]) + vec[2] as i64;
            seg.update(vec[1] - 1, tmp);
        } else {
            println!("{}", seg.query(vec[1] - 1, vec[2]));
        }
    }
}