Cod sursa(job #829935)

Utilizator alinaelenaFMI Colceag Alina alinaelena Data 6 decembrie 2012 02:28:08
Problema Datorii Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.3 kb
#include<cstdio>
using namespace std;
# define nmax 100001
int arb[4*nmax+1],s,op,poz,a,b,n,m,val;
 
int maxim2 (int a,int b)
{
    if (a<b)
        return b;
    return a;
}
 
 
 
void update (int nod,int st,int dr)
{
    int pivot;
    if (st==dr)
        {arb[nod]=arb[nod]+val; return ;}
     
        pivot=(st+dr)/2;
        if (poz<=pivot) update(2*nod,st,pivot);
        else
            update(2*nod+1,pivot+1,dr);
        arb[nod]=arb[2*nod]+arb[2*nod+1];
     
}
 
 
void query (int nod,int st,int dr)
{
    if ((a<=st)&&(dr<=b))
    {   s=s+arb[nod]; return;}
     
    int pivot=(st+dr)/2;
     if (a<=pivot) query(2*nod,st,pivot);
     if (pivot<b) query(2*nod+1,pivot+1,dr);
     
}
 
 
int main()
{
    freopen("datorii.in","r",stdin);
    freopen("datorii.out","w",stdout);
    scanf("%d%d",&n,&m);
    int x,i;
    for (i=1;i<=n;++i)
        {
            scanf("%d",&x);
            poz=i; val=x;
            update(1,1,n);
    }
     
    for (i=1;i<=m;++i)
    {
        scanf("%d %d %d",&op,&a,&b);
         
        if (op==1)
        {
           s=0;
            query(1,1,n);
            printf("%d\n",s);
        }
        else
        {
            poz=a; val=-b;
            update(1,1,n);
        }
             
    }
     
}