Cod sursa(job #2909270)

Utilizator andreimocianAndrei Mocian andreimocian Data 11 iunie 2022 00:01:38
Problema Datorii Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.52 kb
#include <iostream>
#include <fstream>

using namespace std;

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

int n, m;
int v[15005], aint[4 * 15005];

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

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

int Query(int nod, int st, int dr, int x, int y)
{
    if (st >= x && dr <= y)
    {
        return aint[nod];
    }
    int mij = (st + dr) / 2;
    int sol = 0;
    if (x <= mij)
        sol += Query(2 * nod, st, mij, x, y);
    if (y > mij)
        sol += Query(2 * nod + 1, mij + 1, dr, x, y);
    return sol;
}

void citire()
{
    int x, a, b;
    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 >> x >> a >> b;
        if (x == 0)
        {
            Update(1, 1, n, a, b);
        }
        else
        {
            fout << Query(1, 1, n, a, b) << "\n";
        }
    }
}

int main()
{
    citire();
    return 0;
}