Cod sursa(job #1863169)

Utilizator alexmisto342Turdean Alexandru alexmisto342 Data 30 ianuarie 2017 19:23:22
Problema Datorii Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.78 kb
#include <fstream>
#include <vector>
#include <cstring>
using namespace std;
ifstream fin("datorii.in");
ofstream fout("datorii.out");
int n,m,i,j,maxi,idx,v[100005],aib[100005];
void update(int idx, int val)
{
    while(idx <= n)
    {
        aib[idx] += val;
        idx += idx & -idx;
    }
}
int query(int idx)
{
    int x = 0;
    while(idx)
    {
        x += aib[idx];
        idx -= idx & -idx;
    }
    return x;
}
int main ()
{
    fin >> n >> m;
    for(i = 1; i <= n; i++)
    {
        fin >> v[i];
        update(i, v[i]);
    }
    for(i = 1; i <= m; i++)
    {
        int a,b;
        fin >> j >> a >> b;
        if(j == 0)
            update(a, -b);
        else
            fout<<query(b) - query(a-1)<<'\n';
    }
    return 0;
}