Cod sursa(job #2552480)

Utilizator BAlexandruBorgovan Alexandru BAlexandru Data 20 februarie 2020 21:19:19
Problema Datorii Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.17 kb
#include <fstream>

using namespace std;

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

int n, m, i, j, x;
int tip, val, poz;
int a, b, suma;
int s[32786];

void update(int nod, int st, int dr)
{
    if (st == dr)
    {
        s[nod] += val;
        return;
    }

    int mij = (st + dr) / 2;
    if (poz <= mij)
        update(nod*2, st, mij);
    else
        update(nod*2+1, mij+1, dr);

    s[nod] = s[nod*2] + s[nod*2+1];
}

void query(int nod, int st, int dr)
{
    if (a <= st  &&  dr <= b)
    {
        suma += s[nod];
        return;
    }

    int mij = (st + dr) / 2;
    if (mij >= a)
        query(nod*2, st, mij);
    if (mij < b)
        query(nod*2+1, mij+1, dr);
}

int main()
{
    f >> n >> m;
    for (i=1; i<=n; i++)
    {
        f >> x;
        val = x,  poz = i;
        update(1, 1, n);
    }

    for (i=1; i<=m; i++)
    {
        f >> tip >> a >> b;
        if (tip == 0)
        {
            poz = a,  val = -b;
            update(1, 1, n);
        }
        else
        {
            suma = 0;
            query(1, 1, n);
            g << suma << "\n";
        }
    }

    return 0;
}