Cod sursa(job #2617201)

Utilizator VladAlexandruAnghelache Vlad VladAlexandru Data 20 mai 2020 22:26:12
Problema Datorii Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.83 kb
#include <iostream>
#include<fstream>
#define zterm(x)((x^(x-1))&x)
using namespace std;
ifstream fin("datorii.in");
ofstream fout("datorii.out");
int a[15005];
int aib[15005];
void adauga(int st, int val)
{
    for(unsigned i=st;i<=15005;i += zterm(i))
    {
       aib[i]+=val;
    }


}

int suma(int st)
{
    int s=0;
    for(unsigned i=st;i>0;i-=zterm(i))
    {
        s+=aib[i];
    }
    return s;
}
int main()
{
    int n,m;
    fin>>n>>m;

    for(unsigned i=1;i<=n;i++)
    {
        int x;
        fin>>x;
        adauga(i,x);

    }
    while(m--)
    {
        bool x;
        unsigned y,z;
        fin>>x>>y>>z;
        if(x == 0)
        {
            adauga(y,-z);
        }
        else
        {
            fout<<suma(z)-suma(--y)<<"\n";
        }
    }


    return 0;
}