Cod sursa(job #2881809)

Utilizator pifaDumitru Andrei Denis pifa Data 30 martie 2022 21:33:07
Problema Datorii Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.84 kb
#include <fstream>
#define zeros(x) ( (x ^ (x - 1)) & x )
using namespace std;

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

int v[150001], aib[150001];

int n, m;

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

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

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