Cod sursa(job #644655)

Utilizator the@EyE@Postavaru Stefan the@EyE@ Data 7 decembrie 2011 11:23:14
Problema Datorii Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.65 kb
#include<stdio.h>
#define max(a,b) a>b?a:b

#define TSize 32769
#define buffSize 16384

using namespace std;

char buff[buffSize];

int T[TSize];
int sol,ind;

int arb[99999999],n,q;
bool ok=true;

/*inline int read(int &x)
{
    if(ok)
    {
        ok=false;
        fread(buff,1,buffSize,stdin);
        ind=0;
    }

    for(x=0;buff[ind]>='0'&&buff[ind]<='9';)
    {
        x*=10;
        x+=buff[ind]-'0';

        if(++ind==buffSize)
        {
            fread(buff,1,buffSize,stdin);
            ind=0;
        }
    }

    for(;buff[ind]<'0'||buff[ind]>'9';)
        if(++ind==buffSize)
        {
            fread(buff,1,buffSize,stdin);
            ind=0;
        }
}*/

void update(int nod,int st,int dr,int poz,int val)
{
	if(st==poz&&dr==poz)arb[nod]+=val;
	else
	{
		int m=(st+dr)/2;
		int nod2;
		if(poz<=m)
		{
			update(nod*2,st,m,poz,val);
			nod2=nod*2;
		}
		else 
		{
			nod2=nod*2+1;
			update(nod*2+1,m+1,dr,poz,val);
		}
		arb[nod]+=val;
	}
}

int getRest(int nod,int st,int dr,int a,int b)
{	
	if(a<=st&&dr<=b)return arb[nod];
	else
	{
		int x1=0,x2=0;
		int m=(st+dr)/2;
		if(a<=m)x1=getRest(nod*2,st,m,a,b);
		if(b>m)x2=getRest(nod*2+1,m+1,dr,a,b);
		return x1+x2;
	}
}


int main()
{
	freopen("datorii.in","r",stdin);
	freopen("datorii.out","w",stdout);
	scanf("%d %d\n",&n,&q);
//	read(n);
	//read(q);
	for(int i=1;i<=n;i++)
	{
		int x;
		scanf("%d",&x);
		//read(x);
		update(1,1,n,i,x);		
	}
	scanf("\n");
	for(int i=0;i<q;i++)
	{
		int cmd,a,b;
		scanf("%d %d %d\n",&cmd,&a,&b);
		//read(cmd);read(a);read(b);
		if(cmd==0)update(1,1,n,a,-b);
		else printf("%d\n",getRest(1,1,n,a,b));
	}
}