Cod sursa(job #469172)

Utilizator borsoszalanBorsos Zalan borsoszalan Data 6 iulie 2010 18:45:39
Problema Datorii Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.11 kb
#include <stdio.h>

#define nmax 15005
#define mmax 100005
#define pmax nmax<<4

int n, m, x, y, z, S, a [nmax], arb [pmax];


void cnstr (int st, int dr, int nod)
{
	if (st == dr)
	{
		arb [nod]=a [st];
		return;
	}
	int m=(st+dr)>>1;
	cnstr (st, m, nod<<1);
	cnstr (m+1, dr, (nod<<1)+1);
	arb [nod]=arb [nod<<1]+arb [(nod<<1)+1];
}

void update (int st, int dr, int nod)
{
	if (st == dr)
	{
		arb [nod]-=z;
		return;
	}
	int m=(st+dr)>>1;
	if (y <= m)
		update (st, m, nod<<1);
	if (y > m)
		update (m+1, dr, (nod<<1)+1);	
	arb [nod]=arb [nod<<1]+arb [(nod<<1)+1];
}

void query (int st, int dr, int nod)
{
	if (st >= y && dr <= z)
	{
		S+=arb [nod];
		return;
	}
	int m=(st+dr)>>1;
	if (y <= m) query (st, m, nod<<1);
       	if (z > m) query (m+1, dr, (nod<<1)+1);	
}

int main ()
{
	freopen ("datorii.in", "r", stdin);
	freopen ("datorii.out", "w", stdout);
	int i;
	scanf ("%d%d", &n, &m);
	for (i=1; i <= n; ++i) scanf ("%d", &a [i]);
	cnstr (1, n, 1);
	for (i=1; i <= m; ++i)
	{
		scanf ("%d%d%d", &x, &y, &z);
		if (x == 0)
			update (1, n, 1);
		else
		{
			S=0;
			query (1, n, 1);
			printf ("%d\n", S);
		}
	}
	return 0;
}