Cod sursa(job #3315115)

Utilizator itachi_uchihaDarius itachi_uchiha Data 12 octombrie 2025 14:49:31
Problema Datorii Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.37 kb
#include <iostream>
#include <fstream>
using namespace std;
ifstream fin("datorii.in");
ofstream fout("datorii.out");

int v[15001];
int aint[4 * 15001];
int n,m,a,b,operatie, s;

void build(int nod, int st, int dr) {
    if (st == dr) {
        aint[nod] = v[st];
        return ;
    }
    int mijloc = (st + dr) / 2;
    build(2 * nod, st, mijloc);
    build(2 * nod + 1, mijloc + 1, dr);
    aint[nod] = aint[2*nod] + aint[2*nod+1];
}

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

    int mijloc = (st + dr) / 2;
    if (a <= mijloc) {
        update(nod * 2, st, mijloc);
    }
    else {
        update(nod * 2 + 1, mijloc + 1, dr);
    }
    aint[nod] = aint[2*nod] + aint[2 *nod+1];
 }

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

    int mijloc = (st + dr) / 2;
    if (a <= mijloc) {
        query(nod * 2, st, mijloc);
    }
    if (b > mijloc) {
        query(nod * 2 + 1, mijloc + 1, dr);
    }
}

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

    build(1,1,n);


    for (int i = 1; i<=m; i++) {
        fin >> operatie >> a >> b;
        if (operatie == 1) {
            query(1,1,n);
            fout << s << '\n';
            s = 0;
        }
        else {

            update(1,1,n);
        }
    }
}