Cod sursa(job #503938)

Utilizator PlayLikeNeverB4George Marcus PlayLikeNeverB4 Data 25 noiembrie 2010 20:59:40
Problema Datorii Scor 40
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.04 kb
//#include <fstream>
//using namespace std;
#include <iostream>
//ifstream fin("datorii.in");
//ofstream fout("datorii.out");

const int maxn=15001;
int i,N,M,cod,a,b,S,A[maxn],Arb[5*maxn];

void update(int k, int st, int dr)
{
	if(st==dr)
	{
		Arb[k]=A[st];
		return;
	}
	int m=st+(dr-st)/2;
	if(a<=m) update(2*k,st,m);
		else update(2*k+1,m+1,dr);
	Arb[k]=Arb[2*k]+Arb[2*k+1];
}

void caut(int k, int st, int dr)
{
	if(a<=st && dr<=b)
	{
		S+=Arb[k];
		return;
	}
	int m=st+(dr-st)/2;
	if(a<=m) caut(2*k,st,m);
	if(b>m) caut(2*k+1,m+1,dr);
}

int main()
{
	freopen("datorii.in","r",stdin);
	freopen("datorii.out","w",stdout);
	//fin >> N >> M;
	scanf("%d %d",&N,&M);
	for(i=1;i<=N;i++)
	{
		//fin >> A[i];
		scanf("%d",&A[i]);
		a=i; b=A[i];
		update(1,1,N);
	}
	for(i=1;i<=M;i++)
	{
		//fin >> cod >> a >> b;
		scanf("%d %d %d",&cod,&a,&b);
		if(cod==0)
		{
			A[a]-=b;
			update(1,1,N);
		}
		else // cod == 1
		{
			S=0;
			caut(1,1,N);
			//fout << S << endl;
			printf("%d\n",S);
		}
	}
}