Cod sursa(job #946528)

Utilizator Mihai22eMihai Ionut Enache Mihai22e Data 4 mai 2013 18:23:13
Problema Datorii Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.05 kb
#include <fstream>
using namespace std;

const int MAX_N = 15002;

int N, M;
int A[MAX_N];

inline void Update(int pos, int val){
    int C = 0;
    while(pos <= N){
        A[pos] += val;
        while(!(pos & (1 << C)))
            ++C;
        pos += (1 << C);
        ++C;
    }
}

inline int Query(int pos){
    int C = 0, Sum = 0;
    while(pos > 0){
        Sum += A[pos];
        while(!(pos & (1 << C)))
            ++C;
        pos -= (1 << C);
        ++C;
    }

    return Sum;
}

int main(){
    ifstream f("datorii.in");
    ofstream g("datorii.out");

    f >> N >> M;
    for(int i = 1, val; i <= N; ++i){
        f >> val;
        Update(i, val);
    }

    for(int q = 1, t; q <= M; ++q){
        f >> t;

        if(t == 0){
            int day, val;
            f >> day >> val;
            Update(day, -val);
        }
        else{
            int a, b;
            f >> a >> b;
            g << Query(b) - Query(a-1) << '\n';
        }
    }

    f.close();
    g.close();

    return 0;
}