Cod sursa(job #1027106)

Utilizator thewildnathNathan Wildenberg thewildnath Data 12 noiembrie 2013 13:37:25
Problema Datorii Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.19 kb
/*#include<stdio.h>

int n,m,s,poz,val,x,y;
int v[60002];


void update(int l,int r,int x)
{
    if(l==r)
    {
        v[x]+=val;
        return;
    }
    int med=(l+r)/2;
    if(poz<=med)
        update(l,med,2*x);
    else
        update(med+1,r,2*x+1);
    v[x]=v[2*x]+v[2*x+1];
}


void query(int l,int r,int x)
{
    if(x<=l && r<=y)
    {
        s+=v[x];
        return;
    }
    int med=(l+r)/2;
    if(x<=med)
        query(l,med,2*x);
    if(med<y)
        query(med+1,r,2*x+1);
}


int main()
{
    freopen("datorii.in","r",stdin);
    freopen("datorii.out","w",stdout);
    int i,t;
    scanf("%d%d",&n,&m);
    for(i=1;i<=n;++i)
    {
        poz=i;
        scanf("%d",&val);
        update(1,n,1);
    }
    for(i=1;i<=m;++i)
    {
        scanf("%d%d%d",&t,&x,&y);
        if(t==0)
        {
            val=-y;
            poz=x;
            update(1,n,1);
        }
        else
        {
            s=0;
            query(1,n,1);
            printf("%d\n",s);
        }
    }

    return 0;
}
*/

#include<stdio.h>

int n,m,v[4000102],poz,val,st,sf;

inline void update(int l,int r,int x)
{
    if(l==r)
    {
        v[x]+=val;
        return;
    }
    int med=(l+r)/2,p=x+x;
    if(poz<=med)
        update(l,med,p);
    else
        update(med+1,r,p+1);
    v[x]=v[p]+v[p+1];
}

inline void query(int l,int r,int x)
{
    if(st<=l && r<=sf)
    {
        val+=v[x];
        return;
    }
    int med=(l+r)/2,p=x+x;
    if(st<=med)
        query(l,med,p);
    if(med<sf)
        query(med+1,r,p+1);
}

int main()
{
    freopen("datorii.in","r",stdin);
    freopen("datorii.out","w",stdout);
    int i,x,t,a,b;
    scanf("%d%d",&n,&m);
    for(i=1;i<=n;++i)
    {
        scanf("%d",&x);
        poz=i;
        val=x;
        update(1,n,1);
    }

    while(m--)
    {
        scanf("%d",&t);
        if(t==1)
        {
            scanf("%d%d",&st,&sf);
            val=0;
            query(1,n,1);
            printf("%d\n",val);
        }
        else
        {
            scanf("%d%d",&poz,&val);
            val=-val;
            update(1,n,1);
        }
    }

    return 0;
}