Cod sursa(job #2886860)

Utilizator IvanAndreiIvan Andrei IvanAndrei Data 8 aprilie 2022 14:53:27
Problema Datorii Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.91 kb
#include <fstream>

using namespace std;

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

#define zeros(x)((x ^ (x - 1)) & x)

const int max_size = 15e3 + 1;

int a[max_size], aib[max_size], n;

void update (int x, int val)
{
    for (int i = x; i <= n; i += zeros(i))
    {
        aib[i] += val;
    }
}

int compute (int x)
{
    int rez = 0;
    for (int i = x; i > 0; i -= zeros(i))
    {
        rez += aib[i];
    }
    return rez;
}

int main ()
{
    int t;
    in >> n >> t;
    for (int i = 1; i <= n; i++)
    {
        in >> a[i];
        update(i, a[i]);
    }
    while (t--)
    {
        int op, x, y;
        in >> op >> x >> y;
        if (op == 1)
        {
            out << compute(y) - compute(x - 1) << '\n';
        }
        else
        {
            update(x, -y);
        }
    }
    in.close();
    out.close();
    return 0;
}