Cod sursa(job #1682382)

Utilizator aaron72Armand Ioan Anusca Popa aaron72 Data 10 aprilie 2016 11:19:22
Problema Datorii Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.88 kb
#include <bits/stdc++.h>
#define nmax 15005

using namespace std;

int aib[nmax],n,m;

ofstream fout("datorii.out");

inline void Update1(int p,int x)
{
    while(p<=n)
    {
        aib[p]+=x;
        p+=(p&(-p));
    }
}

inline void Update2(int p,int x)
{
    while(p<=n)
    {
        aib[p]-=x;
        p+=(p&(-p));
    }
}

inline int Query(int p)
{
    int s=0;
    while(p>0)
    {
        s+=aib[p];
        p-=(p&(-p));
    }
    return s;
}

inline void Solve(int x,int y)
{
    fout<<Query(y)-Query(x-1)<<"\n";
}

inline void Input()
{
    int i,x,y,opt;
    ifstream fin("datorii.in");
    fin>>n>>m;
    for(i=1;i<=n;i++)
        {
            fin>>x;
            Update1(i,x);
        }
    for(i=1;i<=m;i++)
    {
        fin>>opt>>x>>y;
        if(opt==0) Update2(x,y);
        else Solve(x,y);
    }
    fin.close();
    fout.close();
}

int main()
{
    Input();
    return 0;
}