Cod sursa(job #396187)

Utilizator andrei.sfrentSfrent Andrei andrei.sfrent Data 14 februarie 2010 18:26:03
Problema Datorii Scor 80
Compilator c Status done
Runda Arhiva de probleme Marime 0.97 kb
#include <stdio.h>

#define DIMENSIUNE_ARBORE 32769

int A[DIMENSIUNE_ARBORE], n, m;

void update(int nod, int li, int ls, int poz, int val)
{
	if(li == ls) A[nod] += val;
	else
	{
		int m = (li + ls) / 2;
		
		if(poz <= m) update(2 * nod, li, m, poz, val);
		else update(2 * nod + 1, m + 1, ls, poz, val);

		A[nod] = A[2 * nod] + A[2 * nod + 1];
	}
}

int querry(int nod, int li, int ls, int a, int b)
{
	if(a <= li && ls <= b) return A[nod];
	else
	{
		int m = (li + ls) / 2;
		int si1 = 0, si2 = 0;
		if(a <= m) si1 = querry(2 * nod, li, m, a, b);
		if(b > m) si2 = querry(2 * nod + 1, m + 1, ls, a, b);
		return si1 + si2;
	}
}

int main()
{
	int i, op, x, y;

	freopen("datorii.in", "r", stdin);
	freopen("datorii.out", "w", stdout);
	
	scanf("%d%d", &n, &m);
	
	for(i = 1; i <= n; ++i)
	{
		scanf("%d", &x);
		update(1, 1, n, i, +x);
	}
	
	while(m--)
	{
		scanf("%d%d%d", &op, &x, &y);
		if(op == 0) update(1, 1, n, x, -y);
		else printf("%d\n", querry(1, 1, n, x, y));
	}

	return 0;
}