Cod sursa(job #3134580)

Utilizator Maftei_TudorMaftei Tudor Maftei_Tudor Data 29 mai 2023 17:39:53
Problema Datorii Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.73 kb
#include <fstream>

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

int n, q, x, tip, a, b, aib[15001];

int lsb(int x) {
    return x & -x;
}

int query(int pos) {
    int ans = 0;

    while(pos) {
        ans += aib[pos];
        pos -= lsb(pos);
    }

    return ans;
}

void update(int pos, int val) {
    while(pos <= n) {
        aib[pos] += val;
        pos += lsb(pos);
    }
}

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

    while(q--) {
        fin >> tip >> a >> b;
        if(tip == 0)
            update(a, -b);
        else fout << query(b) - query(a-1) << '\n';
    }
    return 0;
}