Cod sursa(job #1832063)

Utilizator crazylamaRiclea Andrei crazylama Data 19 decembrie 2016 12:50:38
Problema Datorii Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.65 kb
#include <fstream>
#include <vector>
using namespace std;

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

vector<int> arbInt, s;

void creeazaArbore(int nod, int st, int dr)
{
    if (st == dr)
    {
        arbInt[nod] = s[st];
        return;
    }
    int mij = st + (dr - st) / 2;
    creeazaArbore(nod * 2, st, mij);
    creeazaArbore(nod * 2 + 1, mij + 1, dr);
    arbInt[nod] = arbInt[nod * 2] + arbInt[nod * 2 + 1];
}

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

int queryArbore(int nod, int st, int dr, int x, int y)
{
    if (st >= x && dr <= y)
        return arbInt[nod];
    int mij = st + (dr - st) / 2;
    if (y <= mij)
        return queryArbore(nod * 2, st, mij, x, y);
    if (x > mij)
        return queryArbore(nod * 2 + 1, mij + 1, dr, x, y);
    return queryArbore(nod * 2, st, mij, x, y) +
           queryArbore(nod * 2 + 1, mij + 1, dr, x, y);
}

int main()
{
    int n, m;
    f >> n >> m;
    s.resize(n + 1);
    arbInt.resize(4 * n + 1);
    for (int i = 1; i <= n; ++i)
        f >> s[i];
    creeazaArbore(1, 1, n);
    for (int i = 1; i <= m; ++i)
    {
        int x, y, cod;
        f >> cod >> x >> y;
        if (cod == 0)
            modificaArbore(y, x, 1, 1, n);
        else
            g << queryArbore(1, 1, n, x, y) << "\n";
    }
    return 0;
}