Cod sursa(job #2085123)

Utilizator AlexandruabcdeDobleaga Alexandru Alexandruabcde Data 9 decembrie 2017 18:42:15
Problema Datorii Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.05 kb
#include <fstream>

using namespace std;
ifstream f ("datorii.in");
ofstream g ("datorii.out");
int n,m,i,arbore[15000*4+2],x,caz,T,V;
void update (int nod,int a, int b, int poz, int val)
{
    int mij;
    if (a==b) arbore[nod]+=val;
    else
    {
        mij=(a+b)/2;
        if (poz<=mij) update(2*nod,a,mij,poz,val);
        else update(2*nod+1,mij+1,b,poz,val);
        arbore[nod]=arbore[2*nod]+arbore[2*nod+1];
    }
}
int query (int nod, int a, int b, int qa, int qb)
{
    int rez1=0,rez2=0,mij;
    if (qa<=a && b<=qb) return arbore[nod];
    mij=(a+b)/2;
    if (qa<=mij) rez1=query(2*nod,a,mij,qa,qb);
    if (qb>mij) rez2=query(2*nod+1,mij+1,b,qa,qb);
    return rez1+rez2;
}
int main()
{
    f>>n>>m;
    for (i=1;i<=n;i++)
    {
        f>>x;
        update(1,1,n,i,x);
    }
    for (i=1;i<=m;i++)
    {
        f>>caz>>T>>V;
        if (caz==0)
        {
            V=V*-1;
            update(1,1,n,T,V);
        }
        else
        {
            g<<query(1,1,n,T,V)<<'\n';
        }
    }
    return 0;
}