Cod sursa(job #3171748)

Utilizator AnSeDraAndrei Sebastian Dragulescu AnSeDra Data 19 noiembrie 2023 15:28:53
Problema Datorii Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.69 kb
#include <fstream>

using namespace std;

const int Nmax = 15000;

int n, poz, val;
int v[Nmax];

int aint[4 * Nmax];

void build(int nod, int st, int dr){
    if(st == dr){
        aint[nod] = v[st];
        return;
    }

    int mid = (st + dr) / 2;

    build(2 * nod, st, mid);
    build(2 * nod + 1, mid + 1, dr);

    aint[nod] = aint[2 * nod] + aint[2 * nod + 1];
}

void update(int nod, int st, int dr){
    if(st == dr && st == poz){
        aint[nod] -= val;
        return;
    }

    int mid = (st + dr) / 2;

    if(poz <= mid){
        update(2 * nod, st, mid);
    }
    else{
        update(2 * nod + 1, mid + 1, dr);
    }

    aint[nod] = aint[2 * nod] + aint[2 * nod + 1];
}

int query(int nod, int st, int dr, int a, int b){
    if(st == a && dr == b){
        return aint[nod];
    }

    int mid = (st + dr) / 2;

    if(b <= mid){
        return query(2 * nod, st, mid, a, b);
    }
    else if(mid < a){
        return query(2 * nod + 1, mid + 1, dr, a, b);
    }
    else{
        return query(2 * nod, st, mid, a, mid) + query(2 * nod + 1, mid + 1, dr, mid + 1, b);
    }
}

int main(){
    ifstream fin("datorii.in");
    ofstream fout("datorii.out");

    int m, a, b;
    bool type;

    fin >> n >> m;
    for(int i = 0; i < n; i++){
        fin >> v[i];
    }

    build(1, 0, n - 1);

    for(int i = 0; i < m; i++){
        fin >> type;

        if(type == 0){
            fin >> poz >> val;
            poz--;

            update(1, 0, n - 1);
        }
        else{
            fin >> a >> b;
            a--; b--;

            fout << query(1, 0, n - 1, a, b) << '\n';
        }
    }
    return 0;
}