Cod sursa(job #2901517)

Utilizator robertanechita1Roberta Nechita robertanechita1 Data 13 mai 2022 23:20:52
Problema Datorii Scor 40
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.31 kb
#include <bits/stdc++.h>

using namespace std;

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

int a[50000], b[50000], n;

void Build(int nod, int st, int dr)
{
    if(st == dr)
    {
        a[nod] = b[st];
        return;
    }
    int mij = (st + dr) / 2;
    Build(2 * nod, st, mij);
    Build(2 * nod + 1, mij + 1, dr);
    a[nod] = a[2 * nod] + a[2 * nod + 1];
}

void Update(int nod, int st, int dr, int p, int x)
{
    if(st == dr)
    {
        a[nod] -= x;
        return;
    }
    int mij = (st + dr) / 2;
    if(p <= mij)
        Update(2 * nod, st, mij, p, x);
    else
        Update(2 * nod + 1, mij + 1, dr, p, x);
    a[nod] = a[2 * nod] + a[2 * nod + 1];
}

int Query(int nod, int st, int dr, int x, int y)
{
    if(x > dr || y < st)
        return 0;
    if( x <= st && dr <= y )
        return a[nod];

    int mij = (st + dr) / 2;
    return Query(2 * nod, st, mij, x, y) + Query(2 * nod + 1, mij + 1, dr, x, y);

}

int main()
{
    int m, task, x, y;
    fin >> n >> m;
    for(int i = 1; i <= n; i++)
        fin >> b[i];
    Build(1, 1, n);
    for(int i = 1; i <= m; i++)
    {
        fin >> task >> x >> y;
        if(task == 1)
            fout << Query(1, 1, n, x, y) << "\n";
        else Update(1, 1, n, x, y);
    }
    return 0;
}