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 C++11 (judge verdict: Accepted)

#include <bits/stdc++.h>
#define ll long long
#define INF 1000000005
#define MOD 1000000007
#define EPS 1e-10
#define rep(i,n) for(int i=0;i<(int)(n);++i)
#define rrep(i,n) for(int i=(int)(n)-1;i>=0;--i)
#define srep(i,s,t) for(int i=(int)(s);i<(int)(t);++i)
#define each(a,b) for(auto& (a): (b))
#define all(v) (v).begin(),(v).end()
#define len(v) (int)(v).size()
#define zip(v) sort(all(v)),v.erase(unique(all(v)),v.end())
#define cmx(x,y) x=max(x,y)
#define cmn(x,y) x=min(x,y)
#define fi first
#define se second
#define pb push_back
#define show(x) cout<<#x<<" = "<<(x)<<endl
#define spair(p) cout<<#p<<": "<<p.fi<<" "<<p.se<<endl
#define sar(a,n) cout<<#a<<":";rep(pachico,n)cout<<" "<<a[pachico];cout<<endl
#define svec(v) cout<<#v<<":";rep(pachico,v.size())cout<<" "<<v[pachico];cout<<endl
#define svecp(v) cout<<#v<<":";each(pachico,v)cout<<" {"<<pachico.first<<":"<<pachico.second<<"}";cout<<endl
#define sset(s) cout<<#s<<":";each(pachico,s)cout<<" "<<pachico;cout<<endl
#define smap(m) cout<<#m<<":";each(pachico,m)cout<<" {"<<pachico.first<<":"<<pachico.second<<"}";cout<<endl

using namespace std;

typedef pair<int,int> P;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<double> vd;
typedef vector<P> vp;
typedef vector<string> vs;

const int MAX_N = 100005;

template<typename T> class SQRTdecomposition{
public:
    vector<T> data, backet;
    int sz,backet_size,backet_cnt;
    SQRTdecomposition(vector<T>& v) : data(v){
        sz = (int)data.size();
        backet_size = (int)sqrt(sz);
        backet_cnt = (sz + backet_size - 1) / backet_size;
        backet.resize(backet_cnt);
        rep(i,sz){
            backet[i / backet_size] += data[i];   //例えば和を持たせる処理
        }
    }
    void update(int l, int k){
        data[l] += k;
        backet[l / backet_size] += k;
    }
    T query(int l, int r){    //[l.r)
        T res = 0;
        // 同じバケット内にある
        if(l / backet_size == (r-1) / backet_size){
            for(int i = l; i < r; i++){
                res += data[i];
            }
        }else{
            // 完全に被っているバケット
            for(int i = (l + backet_size - 1) / backet_size; i < r / backet_size; i++){
                res += backet[i];
            }
            // 中途半端な左側のバケット
            if(l % backet_size){
                for(int i = l; i < backet_size*(l / backet_size + 1); i++){
                    res += data[i];
                }
            }
            // 中途半端な右側のバケット
            if(r % backet_size){
                for(int i = r - r % backet_size; i < r; i++){
                    res += data[i];
                }
            }
        }
        return res;
    }
};

int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n,q;
    cin >> n >> q;
    vi a(n,0);
    SQRTdecomposition<int> sr(a);
    rep(i,q){
        int a,b,c;
        cin >> a >> b >> c;
        if(a == 0){
            sr.update(b-1,c);
        }else{
            cout << sr.query(b-1,c) << "\n";
        }
    }
    return 0;
}