Cod sursa(job #2774307)

Utilizator cyg_dawidDavid Ghiberdic cyg_dawid Data 11 septembrie 2021 00:04:57
Problema Datorii Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.85 kb
#include <fstream>

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

int const nmax = 15005;
int aib[nmax];
int n;

void update (int x, int val, int tip) {
    val *= tip;
    for (int i = x; i <= n; i += (i ^ (i & (i - 1))))
        aib[i] += val;
}

int calc (int x) {
    int sum = 0;
    for (int i = x; i > 0; i -= (i ^ (i & (i - 1))))
        sum += aib[i];
    return sum;
}

int main() {
    int m;
    fin >> n >> m;
    for (int i = 1; i <= n; i++) {
        int x;
        fin >> x;
        update(i, x, 1);
    }
    for (int i = m; i; i--) {
        int tip;
        fin >> tip;
        if (tip == 0) {
            int a, b;
            fin >> a >> b;
            update(a, b, -1);
        } else {
            int a, b;
            fin >> a >> b;
            fout << calc(b) - calc(a - 1) << "\n";
        }
    }
    return 0;
}