Cod sursa(job #2774988)

Utilizator cezar_titianuTitianu Cezar cezar_titianu Data 13 septembrie 2021 19:47:39
Problema Datorii Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.88 kb
#include <fstream>
#include <cmath>
#include <vector>

std::vector <int> aib;

void update(int pos, int val) {
	while (pos < aib.size()) {
		aib[pos] -= val;
		pos += (pos & -pos);
	}
}

int query(int pos) {
	int ans = 0;
	while (pos) {
		ans += aib[pos];
		pos &= pos - 1;
	}
	return ans;
}

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