Cod sursa(job #2767498)

Utilizator cezar_titianuTitianu Cezar cezar_titianu Data 6 august 2021 14:10:14
Problema Datorii Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.52 kb
#include <fstream>
#include <cmath>

int aint[60000];

int query(int left, int right, int nod, int ileft, int iright) {
    if (left == ileft && right == iright) {
        return aint[nod];
    }
    else {
        int mid = (left + right) / 2, ans = 0;
        if (ileft <= mid) {
            ans = query(left, mid, 2 * nod, ileft, std::min(iright, mid));
        }
        if (mid < iright) {
            ans += query(mid + 1, right, 2 * nod + 1, std::max(ileft, mid + 1), iright);
        }
        return ans;
    }
}

void update(int left, int right, int nod, int pos, int val) {
    if (left == right) {
        aint[nod] -= val;
    }
    else {
        int mid = (left + right) / 2;
        if (pos <= mid) {
            update(left, mid, 2 * nod, pos, val);
        }
        if (mid < pos) {
            update(mid + 1, right, 2 * nod + 1, pos, val);
        }
        aint[nod] = aint[2 * nod] + aint[2 * nod + 1];
    }
}

int main() {
    std::ifstream fin("datorii.in");
    std::ofstream fout("datorii.out");
    int nrn, nrm, cer, left, right, pos, val;
    fin >> nrn >> nrm;
    for (int index = 1; index <= nrn; index++) {
        fin >> val;
        update(1, nrn, 1, index, -val);
    }
    for (int index = 0; index < nrm; index++) {
        fin >> cer;
        if (cer) {
            fin >> left >> right;
            fout << query(1, nrn, 1, left, right) << '\n';
        }
        else {
            fin >> pos >> val;
            update(1, nrn, 1, pos, val);
        }
    }
}