Cod sursa(job #2307970)

Utilizator butasebiButa Gabriel-Sebastian butasebi Data 25 decembrie 2018 23:06:07
Problema Datorii Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.75 kb
#include <bits/stdc++.h>
using namespace std;
int n, m, i, value, p, a, b, aib[15005];
int LastBit(int x)
{
    return x & ( - x);
}
void update(int index, int value)
{
    for(int i = index; i <= n; i = i + LastBit(i))
        aib[i] = aib[i] + value;
}
int query(int index)
{
    int s = 0;
    for(int i = index; i > 0; i = i - LastBit(i))
        s = s + aib[i];
    return s;
}
int main()
{
    ifstream f("datorii.in");
    ofstream g("datorii.out");
    f >> n >> m;
    for(i = 1; i <= n ;i ++)
    {
        f >> value;
        update(i, value);
    }
    for(i = 1; i <= m; i ++)
    {
        f >> p >> a >> b;
        if(p == 0)update(a, -b);
        else g << query(b) - query(a - 1) << "\n";
    }
    return 0;
}