Cod sursa(job #2074432)

Utilizator netfreeAndrei Muntean netfree Data 24 noiembrie 2017 16:47:52
Problema Datorii Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.83 kb
#include <bits/stdc++.h>

using namespace std;

ifstream fin ("datorii.in");
ofstream fout("datorii.out");

const int N_MAX = 15000 + 5;

int aib[2 * N_MAX];

int n, m;

void update (int poz, int val){
    while (poz <= n){
        aib[poz] += val;
        poz += (poz & -poz);
    }
}

int sum0 (int hi) {
    int sum = 0;
    while (hi > 0){
        sum += aib[hi];
        hi -= (hi & -hi);
    }
    return sum;
}

int sum (int lo, int hi){
    return sum0(hi) - sum0(lo-1);
}

int main()
{
    fin >> n >> m;

    for(int i = 1; i<=n; ++i){
        int nr; fin >> nr;
        update(i, nr);
    }

    while(m--){
        int a, b, c;
        fin >> a >> b >> c;

        if(a == 0){
            update(b, - c);
        }else
            fout << sum(b, c) << "\n";
    }

    return 0;
}