Cod sursa(job #1241337)

Utilizator diana97Diana Ghinea diana97 Data 13 octombrie 2014 12:31:57
Problema Datorii Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.27 kb
#include <iostream>
#include <fstream>

using namespace std;

ifstream f("datorii.in");
ofstream g("datorii.out");

const int NMAX = 15000 + 1;

int n, q;
int poz, valoare, a, b;
int sol;
int arb[4 * NMAX];

void update(int nod, int st, int dr) {
    if (st == dr) arb[nod] -= valoare;
    else {
        int m = (st + dr) / 2;
        if (poz <= m) update(2 * nod, st, m);
        else update(2 * nod + 1, m + 1, dr);
        arb[nod] = arb[2 * nod] + arb[2 * nod + 1];
    }
}

void query(int nod, int st, int dr) {
    if (a <= st && dr <= b) sol += arb[nod];
    else {
        int m = (st + dr) / 2;
        if (a <= m) query(2 * nod, st, m);
        if (m < b) query(2 * nod + 1, m + 1, dr);
    }
}

void citeste() {
    f >> n >> q;
    for (int i = 1; i <= n; i++) {
        f >> valoare;
        valoare *= -1;
        poz = i;
        update(1, 1, n);
    }
    //for (int i = 1; i <= 4 * n; i++) cout << arb[i] << ' ';
    //cout << endl;
}

void rezolva() {
    int Q;
    while(q--) {
        f >> Q >> a >> b;
        if (Q == 0) {
            valoare = b;
            poz = a;
            update(1, 1, n);
        }
        else {
            sol = 0;
            query(1, 1, n);
            g << sol << '\n';
        }
    }
}

int main() {
    citeste();
    rezolva();
    return 0;
}