Cod sursa(job #2753163)

Utilizator cezar.balutaCezar Baluta cezar.baluta Data 21 mai 2021 12:28:56
Problema Datorii Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.91 kb
#include<iostream>
#include <fstream>

using namespace std;

int aib[15000], n,m;

void update(int poz, int val)
{
    while (poz <= n)
    {
        aib[poz] += val;
        poz += (poz & (-poz));
    }
}

int sum(int poz)
{
    int s = 0;
    while (poz > 0)
    {
        s += aib[poz];
        poz -= (poz & (-poz));
    }
    return s;
}

int main()
{
    bool tip;
    ifstream in("datorii.in");
    ofstream out("datorii.out");
    in >> n >> m;
    for (int i = 1; i <= n; ++i)
    {
        int val;
        in >> val;
        update(i, val);
    }
    while (m--)
    {
        in >> tip;
        if (tip)
        {
            int a, b;
            in >> a >> b;
            out << sum(b) - sum(a-1) << '\n';
        }
        else
        {
            int poz, val;
            in >> poz >> val;
            update(poz, -val);
        }
    }
    return 0;
}