Cod sursa(job #2806222)

Utilizator namesurname01Name Surname namesurname01 Data 22 noiembrie 2021 14:21:48
Problema Datorii Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.36 kb
#include <cstdio>
#include <iostream>

using namespace std;
FILE* f, * g;

int x, arbore[1 << 15], poz, a, b, sol;

void cauta(int nod, int st, int dr)
{
    if (a <= st && dr <= b)
        sol += arbore[nod];
    else
    {
        int mij = (st + dr) / 2;
        if (a <= mij)
            cauta(2 * nod, st, mij);
        if (mij < b)
            cauta(2 * nod + 1, mij + 1, dr);
    }
}
void update(int nod, int st, int dr)
{
    int mij;
    if (st == dr)
        arbore[nod] += x;
    else
    {
        mij = (st + dr) / 2;
        if (poz <= mij)
            update(2 * nod, st, mij);
        else
            update(2 * nod + 1, mij + 1, dr);
        arbore[nod] = arbore[2 * nod] + arbore[2 * nod + 1];
    }
}
int main()
{
    f = fopen("datorii.in", "r");
    g = fopen("datorii.out", "w");
    int m, n;
    fscanf(f, "%d %d", &n, &m);
    for (int i = 1;i <= n;++i)
    {
        fscanf(f, "%d", &x);
        poz = i;
        update(1, 1, n);
    }
    int p;
    for (int i = 1;i <= m;++i)
    {
        fscanf(f, "%d %d %d", &p, &a, &b);
        if (p == 1)
        {
            sol = 0;
            cauta(1, 1, n);
            fprintf(g, "%d\n", sol);
        }
        else
        {
            poz = a, x = b * (-1);
            update(1, 1, n);

        }
    }
    fclose(f);
    fclose(g);
    return 0;
}