Cod sursa(job #2048859)

Utilizator SebastianPredescuIon1predescu SebastianPredescuIon1 Data 26 octombrie 2017 17:10:30
Problema Datorii Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.78 kb
#include <fstream>
#define ub(x) (x&(-x))

using namespace std;
int i,n,aib[15005],x,j,t,y,m,c;
ifstream f("datorii.in");
ofstream g("datorii.out");
void update(int poz,int val)
{
    for(i=poz;i<=n;i+=ub(i))
    {
        aib[i]+=val;
    }
}
void scad(int poz,int val)
{
    for(i=poz;i<=n;i+=ub(i))
    {
        aib[i]-=val;
    }
}
int sum(int poz)
{
    int s=0;
    for(i=poz;i>0;i-=ub(i))
    {
        s=s+aib[i];
    }
    return s;
}
int main()
{
    f>>n>>m;
    for(j=1;j<=n;j++)
    {
        f>>x;
        update(j,x);
    }
    for(j=1;j<=m;j++)
    {
        f>>c;
        f>>t>>y;
        if(c==0)
        {
            scad(t,y);
        }
        else if(c==1)
        {
            g<<sum(y)-sum(t-1)<<'\n';
        }
    }
}