Cod sursa(job #1045970)

Utilizator razvan2006razvan brezulianu razvan2006 Data 2 decembrie 2013 15:06:50
Problema Datorii Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.23 kb
#include<stdio.h>

int i, j, n, A[15001], arbSum[15001*4], Poz, sum, start, finish, m;

void update(int nod, int begin, int end);
void query(int nod, int begin, int end);

int main() {
	freopen("datorii.in", "rt", stdin);
	freopen("datorii.out", "wt", stdout);
	
	scanf("%d%d", &n, &m);
	
	for (int i = 1; i <= n; i++) scanf ("%d", &A[i]);
		
	for (int i = 1; i <= n; i++) {Poz = i; update(1, 1, n);}
	
	int op, x, y;
	for (int i = 0; i < m; i++) {
		scanf ("%d%d%d", &op, &x, &y);
	
		if (op == 0) {
			A[x] -= y; Poz = x;
			update (1, 1, n);
		} else {
			sum = 0;
			start = x; finish = y;
			query (1, 1, n);
			printf ("%d\n", sum);
		}
	}
		
	return 0;
}

void update (int nod, int begin, int end) {
	if (begin == end) {
		arbSum[nod] = A[Poz];
		return;
	}
	
	int mij = (begin + end) / 2;
	if (Poz <= mij) update (nod * 2, begin, mij);
	if (Poz > mij) update (nod * 2 + 1, mij + 1, end);

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

void query (int nod, int begin, int end) {
	if (start <= begin && end <= finish) {
		sum += arbSum[nod];
		return;
	}
	
	int mij = (begin + end) / 2;
	if (start <= mij) query (nod * 2, begin, mij);
	if (mij < finish) query (nod * 2 + 1, mij + 1, end);
}