Cod sursa(job #3235842)

Utilizator stefanrotaruRotaru Stefan-Florin stefanrotaru Data 22 iunie 2024 16:32:37
Problema Datorii Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.82 kb
#include <fstream>

using namespace std;

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

int n, m, caz, a, b, AIB[15005], val;

int chestie(int i)
{
    return (i & (-i));
}

void update(int poz, int val)
{
    while (poz <= n) {
        AIB[poz] += val;
        poz += chestie(poz);
    }
}

int query(int poz, int sum = 0)
{
    sum = 0;

    while (poz) {
        sum += AIB[poz];
        poz -= chestie(poz);
    }

    return sum;
}

int main()
{
    f >> n >> m;

    for (int i = 1; i <= n; ++i) {
        f >> val;

        update(i, val);
    }

    while (m--) {
        f >> caz >> a >> b;

        if (caz == 0) {
            update(a, -b);
        }
        else if (caz == 1) {
            g << query(b) - query(a - 1) << '\n';
        }
    }

    return 0;
}