Cod sursa(job #2570032)

Utilizator FrostfireMagirescu Tudor Frostfire Data 4 martie 2020 14:46:16
Problema Datorii Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.83 kb
#include <iostream>
#include <fstream>
#define NMAX 15000

using namespace std;

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

int n, q, aib[NMAX+10];

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

int query(int poz)
{   int ans = 0;
    while(poz)
        {   ans += aib[poz];
            int lsb = poz & (-poz);
            poz -= lsb;
        }
    return ans;
}

int main()
{
    f >> n >> q;
    for(int i=1; i<=n; i++)
        {   int x;
            f >> x;
            update(i, x);
        }
    while(q--)
        {   int type, a, b;
            f >> type >> a >> b;
            if(!type) update(a, -b);
            else g << query(b) - query(a-1) << '\n';
        }
    return 0;
}