Cod sursa(job #1900720)

Utilizator nicu_serteSerte Nicu nicu_serte Data 3 martie 2017 16:04:57
Problema Datorii Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.69 kb
#include <fstream>
using namespace std;
ifstream fin("datorii.in");
ofstream fout("datorii.out");
#define nmax 15001
#define zeros(x) ((x^(x-1))&x)
int n, arb[nmax];
void update(int poz, int x)
{
    int i;
    for(i=poz; i<=n; i+=zeros(i))
        arb[i]+=x;
}
int calc(int poz)
{
    int s=0, i;
    for(i=poz; i>0; i-=zeros(i))
        s+=arb[i];
    return s;
}
int main()
{
    int m, i, op, x, y;
    fin>>n>>m;
    for(i=1; i<=n; i++)
    {
        fin>>x;
        update(i, x);
    }
    for(i=1; i<=m; i++)
    {
        fin>>op>>x>>y;
        if(op==0)
            update(x, -y);
        else
            fout<<calc(y)-calc(x-1)<<'\n';
    }
    return 0;
}