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 JAVA (judge verdict: Accepted)

import java.util.*;

class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        
        int n, q;
        int com, x, y;
        RSQ dat;

        n = sc.nextInt();
        q = sc.nextInt();

        dat = new RSQ(n);

        for (int i = 0; i < q; i++) {
            com = sc.nextInt();
            x = sc.nextInt();
            y = sc.nextInt();

            x--;
            if (com == 0) {
                dat.update(x, dat.query(x, x + 1) + y);
            }
            else {
                System.out.println(dat.query(x, y));
            }
        }
    }
}

class RSQ extends SegmentTree<Integer> {
    public RSQ(int size) {
        super(size, 0);
    }

    @Override
    protected Integer operator(Integer x, Integer y) {
        return x + y;
    }
}

/**
 * モノイドを乗せられるセグメントツリーの抽象クラス<br>
 * operator メソッドをオーバライドすることで二項演算を定義する<br>
 * データの1点更新と区間に対する質問クエリをO(log(N))で行う
 */
abstract class SegmentTree<T> {

    /** 単位元 */
    protected T e;

    protected int n;
    protected ArrayList<T> tree;

    
    abstract protected T operator(T x, T y);

    /**
     * 要素eで埋められた要素数nmemb個のセグ木を生成
     * @param nmemb 要素数
     * @param e 初期要素(単位元であること)
     */
    public SegmentTree(int nmemb, T e) {
        this.e = e;
        
        n = 1;
        while (n < nmemb) {
            n *= 2;
        }

        tree = new ArrayList<T>(2 * n - 1);
        for (int i = 0; i < 2 * n - 1; i++) {
            tree.add(e);
        }
    }

    /**
     * データを更新する.<br>
     * O(log(N))
     * @param k データの位置
     * @param dat 更新データ
     */
    public void update(int k, T dat) {
        k += n - 1;

        tree.set(k, dat);

        while(k > 0) {
            k = (k - 1) / 2;
            tree.set(k, operator(tree.get(k * 2 + 1), tree.get(k * 2 + 2)));
        }
    }
    
    /**
     * [l, r)区間の二項演算の結果を返す.<br>
     * O(log(N))
     * @param l 区間の左
     * @param r 区間の右
     * @return 演算結果
     */
    public T query(int l, int r) {
        l += n - 1;
        r += n - 1;

        T d = e;
        while (l < r) {
            if (l % 2 == 0) {
                d = operator(d, tree.get(l));
                l++;
            }
            if (r % 2 == 0) {
                r--;
                d = operator(d, tree.get(r));
            }

            l = (l - 1) / 2;
            r = (r - 1) / 2;
        }

        return d;
    }

    /**
     * デバッグ情報の出力
     */
    public void debugPrint() {
        int cnt = 1;
        int i = 0;

        System.err.printf("n:%d tree.size():%d\n", n, tree.size());

        while (i < 2 * n - 1) {
            for (int j = 0; j < cnt; i++, j++) {
                System.err.printf(" [%d]: ", i);
                System.err.print(tree.get(i));
            }
            System.err.println("");
            
            cnt *= 2;
        }
    }
}