Cod sursa(job #664832)

Utilizator tomaAndrei Toma toma Data 20 ianuarie 2012 22:11:52
Problema Datorii Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.93 kb
#include<stdio.h>
#include<vector>
#define Nmax 15001

using namespace std;

int poz,val,S,x,y,type,N,M,i,j;
vector<int> H(4*Nmax);

void Update(int nod,int st,int dr)
{
	if (st==dr)
	{
		H[nod]-=val;
		return;
	}
	int m=(st+dr)/2;
	
	if (m>=poz) Update(nod<<1,st,m);
		else Update((nod<<1)+1,m+1,dr);
	
	H[nod]=H[nod<<1]+H[(nod<<1)+1];
}

void Query(int nod,int st,int dr)
{
	if (x<=st&&dr<=y)
	{
		S+=H[nod];
		return;
	}
	
	int m=(st+dr)/2;
	
	if (x<=m) Query(nod<<1,st,m);	
	if (y>m) Query((nod<<1)+1,m+1,dr);
}

int main()
{
	freopen("datorii.in","r",stdin);
	freopen("datorii.out","w",stdout);
	
	scanf("%d%d",&N,&M);
	for (i=1;i<=N;++i)
	{
		scanf("%d",&val);
		poz=i;
		val*=-1;
		Update(1,1,N);
	}
	
	for (i=1;i<=M;++i)
	{
		scanf("%d%d%d",&type,&x,&y);
		if (!type)
		{
			poz=x;
			val=y;
			Update(1,1,N);
		}
		else
		{
			S=0;
			Query(1,1,N);
			printf("%d\n",S);
		}
	}
}