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::cmp;
use std::collections::HashMap;
use std::io::*;
use std::str::FromStr;
use std::string::String;

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();

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

#[derive(Eq, PartialEq, Clone, Debug)]
pub struct Rev<T>(pub T);

impl<T: PartialOrd> PartialOrd for Rev<T> {
    fn partial_cmp(&self, other: &Rev<T>) -> Option<std::cmp::Ordering> {
        other.0.partial_cmp(&self.0)
    }
}

impl<T: Ord> Ord for Rev<T> {
    fn cmp(&self, other: &Rev<T>) -> std::cmp::Ordering {
        other.0.cmp(&self.0)
    }
}

struct SegmentTree<Monoid, Operator> {
    size: usize,
    node: Vec<Monoid>,
    f: Operator,
    e: Monoid,
}

impl<Monoid, Operator> SegmentTree<Monoid, Operator>
where
    Operator: Fn(Monoid, Monoid) -> Monoid,
    Monoid: Copy,
{
    pub fn new(n: usize, f: Operator, e: Monoid) -> Self {
        let mut size = 1;
        while size < n {
            size *= 2
        }

        SegmentTree {
            size: size,
            node: vec![e; size * 2],
            f: f,
            e: e,
        }
    }

    pub fn update(&mut self, idx: usize, x: Monoid) {
        let mut k = idx + self.size - 1;
        self.node[k] = x;
        while k > 0 {
            k = (k - 1) / 2;
            self.node[k] = (self.f)(self.node[2 * k + 1], self.node[2 * k + 2]);
        }
    }

    pub fn query(&self, a: usize, b: usize) -> Monoid {
        return self._query(a, b, 0, 0, self.size);
    }

    fn _query(&self, a: usize, b: usize, idx: usize, l: usize, r: usize) -> Monoid {
        if r <= a || b <= l {
            return self.e;
        }

        if a <= l && r <= b {
            return self.node[idx];
        }

        let xl: Monoid = self._query(a, b, 2 * idx + 1, l, (l + r) / 2);
        let xr: Monoid = self._query(a, b, 2 * idx + 2, (r + l) / 2, r);

        return (self.f)(xl, xr);
    }
}

fn main() {
    let (_, q): (usize, usize) = (read(), read());
    let mut seg = SegmentTree::<i32, _>::new(100_000, |x, y| x + y, 0);

    for _ in 0..q {
        let (com, x, y): (usize, usize, usize) = (read(), read(), read());

        if com == 0 {
            let k = seg.query(x, x + 1);
            seg.update(x, k + y as i32);
        } else {
            println!("{}", seg.query(x, y + 1));
        }
    }
}