Cod sursa(job #3161016)

Utilizator zetef3Dediu Stefan zetef3 Data 25 octombrie 2023 15:29:23
Problema Datorii Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.27 kb
#include <fstream>

using namespace std;

const int NMAX=15e3;
const int NAINT=4*NMAX;

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

int n,m,t,a,b;
int aint[NAINT];

void build(int p, int st, int dr) {
    if (st==dr) {
        f >> aint[p];
        return;
    }

    int m=(st+dr)/2, fs=2*p, fd=2*p+1;
    build(fs, st,m);
    build(fd, m+1,dr);

    aint[p]=aint[fs]+aint[fd];
}

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

    int m=(st+dr)/2, fs=2*p, fd=2*p+1;
    int s=0;

    if (a<=m) s+=query(fs, st,m, a,b);
    if (b>m)  s+=query(fd, m+1,dr, a,b);

    return s;
}

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

    int m=(st+dr)/2, fs=2*p, fd=2*p+1;

    if (poz<=m) update(fs, st,m, poz,val);
    else        update(fd, m+1,dr, poz,val);

    aint[p]=aint[fs]+aint[fd];
}

int main()
{
    f >> n >> m;
    build(1,1,n);

    for (int i=1;i<=m;i++) {
        f >> t >> a >> b;

        if (t==0) {
            //update v[a]-=b;
            update(1,1,n,a,b);
        } else {
            //query suma
            g << query(1,1,n,a,b) << '\n';
        }
    }
    return 0;
}