Cod sursa(job #2377789)

Utilizator IulianOleniucIulian Oleniuc IulianOleniuc Data 11 martie 2019 09:35:48
Problema Datorii Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.79 kb
#include <fstream>
#define NMAX 15010

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

int n, q;
int aib[NMAX];

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

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

int query(int pos) {
    int sum = 0;
    for (int i = pos; i >= 1; i -= lsb(i))
        sum += aib[i];
    return sum;
}

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

    for (int it = 0; it < q; it++) {
        int t, x, y;
        fin >> t >> x >> y;

        if (!t)
            update(x, -y);
        else
            fout << query(y) - query(x - 1) << '\n';
    }

    fout.close();
    return 0;
}