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::io;
use std::io::prelude::*;
use std::iter::repeat;

struct SumSegTree {
    n: usize,
    nodes: Vec<i32>,
}

impl SumSegTree {
    fn init(n: usize) -> SumSegTree {
        let n = 1 << f64::log2(n as f64).ceil() as usize;
        SumSegTree {
            n,
            nodes: repeat(0).take(2 * n - 1).collect(),
        }
    }

    fn update1(&mut self, idx: usize) {
        self.nodes[idx] = self.nodes[2 * idx + 1] + self.nodes[2 * idx + 2];
    }

    fn update_rec(&mut self, idx: usize) {
        self.update1(idx);
        if idx > 0 {
            self.update_rec((idx - 1) / 2);
        }
    }

    fn add(&mut self, i: usize, x: i32) {
        let ni = self.n + i - 1;
        self.nodes[ni] += x;
        if self.n > 1 {
            self.update_rec((ni - 1) / 2);
        }
    }

    fn get_sum(&self, i: usize, j: usize, ni: usize, l: usize, r: usize) -> i32 {
        if i <= l && r <= j {
            self.nodes[ni]
        } else if r <= i || j <= l {
            0
        } else {
            self.get_sum(i, j, 2 * ni + 1, l, (l + r) / 2)
                + self.get_sum(i, j, 2 * ni + 2, (l + r) / 2, r)
        }
    }
}

fn main() {
    let stdin = io::stdin();
    let mut lines = stdin.lock().lines();

    let line0 = lines.next().unwrap().unwrap();
    let n = line0.split_whitespace().next().unwrap().parse().unwrap();

    let mut mst = SumSegTree::init(n);

    for l in lines.map(Result::unwrap) {
        let mut words = l.split_whitespace();
        let com = words.next().unwrap().parse().unwrap();
        let x: usize = words.next().unwrap().parse().unwrap();
        let y: usize = words.next().unwrap().parse().unwrap();
        match com {
            0 => mst.add(x - 1, y as i32),
            1 => println!("{}", mst.get_sum(x - 1, y, 0, 0, mst.n)),
            _ => (),
        }
    }
}