Cod sursa(job #2902200)

Utilizator andlftLefter Andrei andlft Data 15 mai 2022 20:54:51
Problema Datorii Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.75 kb
#include <iostream>
#include <fstream>

using namespace std;

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


int noduri[100000];

void inserare (int nod_curent, int s, int d, int poz, int elem)
{
    if(s > poz || d < poz)
    {
        return;
    }
    if(s == d)
    {
        noduri[nod_curent] = elem;
        return;
    }

        inserare(nod_curent*2, s, (s+d)/2, poz, elem);
        inserare(nod_curent*2 + 1, ((s+d)/2) + 1, d, poz, elem);
        noduri[nod_curent] = noduri[nod_curent*2]+ noduri[nod_curent*2 + 1];
}

void modificare(int nod_curent, int s, int d, int poz, int elem)
{
    if(s > poz || d < poz)
    {
        return;
    }
    if(s == d)
    {
        noduri[nod_curent] -= elem;
        return;
    }
    modificare(nod_curent*2, s, (s+d)/2, poz, elem);
    modificare(nod_curent*2 + 1, (s+d)/2 + 1, d, poz, elem);
    noduri[nod_curent] = noduri[nod_curent*2] + noduri[nod_curent*2 + 1];
}

int suma (int nod_curent, int s, int d, int int_s, int int_d)
{
    if(int_s > d || int_d < s)
    {
        return 0;
    }
    if(int_s <= s && int_d >= d)
    {
        return noduri[nod_curent];
    }
    return suma(nod_curent*2, s, (s+d)/2, int_s, int_d) + suma(nod_curent*2 + 1, (s+d)/2 + 1, d, int_s, int_d);
}

int main()
{
    int n, m, elem, op_code, par_1, par_2;
    fin >> n >> m;

    for (int i = 1; i <=n ; i++)
    {
        fin >> elem;
        inserare(1, 1, n, i, elem);
    }
    for (int i = 1; i<= m; i++)
    {
        fin>> op_code >> par_1 >> par_2;
        if(op_code)
        {
            fout<<suma(1, 1, n, par_1, par_2)<<"\n";

        }
        else
        {
            modificare(1, 1, n, par_1, par_2);
        }
    }

    return 0;
}