Cod sursa(job #3337991)

Utilizator Matei_SofroniSofroni Matei-Alexandru Matei_Sofroni Data 31 ianuarie 2026 09:40:39
Problema Datorii Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.42 kb
#include <fstream>

using namespace std;
ifstream fin("datorii.in");
ofstream fout("datorii.out");
int n, m, arbore[100005];
void update(int pozitie, int valoare, int st = 1, int dr = n, int nod = 1)
{
    if(st == dr)
    {
        arbore[nod] -= valoare;
        return;
    }
    int mijloc = (st+ dr) / 2;
    if(mijloc >= pozitie)
    {
        update(pozitie, valoare, st , mijloc, nod*2);
    }
    else
    {
        update(pozitie, valoare, mijloc + 1, dr, nod*2 +1);
    }
    arbore[nod] = arbore[nod * 2] + arbore[nod * 2 + 1];
}

int show_sum(int st_initial, int dr_initial, int st = 1, int dr = n, int nod = 1)
{
    if(st_initial <= st && dr <= dr_initial)
    {
        return arbore[nod];
    }
    int mijloc = (st + dr) / 2;
    int suma = 0;
    if(mijloc >= st_initial)
    {
        suma += show_sum(st_initial, dr_initial, st, mijloc, nod*2);
    }
    if(mijloc + 1 <= dr_initial)
    {
        suma += show_sum(st_initial, dr_initial, mijloc + 1, dr, nod * 2 + 1);
    }
    return suma;
}
int main()
{
    fin>>n>>m;
    int x;
    for(int i = 1; i<= n; i++)
    {
        fin>>x;
        update(i, -x);
    }
    int tip, a, b;
    for(int i = 1; i <= m ; i++)
    {
        fin >> tip >> a >> b;

        if(tip == 0)
        {
            update(a, b);
        }
        else
        {
            fout<<show_sum(a, b)<<'\n';
        }
    }
    return 0;
}