Cod sursa(job #1220279)

Utilizator Kerriganamihut Kerrigan Data 17 august 2014 00:25:37
Problema Datorii Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.45 kb
/******************************************************************************************
*           .--.																		  *
* ::\`--._,'.::.`._.--'/::			@author Ana M. Mihut	@course InfoArena Tryout	  *
* ::::. `  __::__ ' .:::::			@alias  LT-Kerrigan		@date   16.08.2014			  *
* ::::::-:.`'..`'.:-::::::			@link   http://infoarena.ro/problema/datorii		  *
* ::::::::\ `--' /::::::::			@detail	binary indexed trees					      *
*																						  *
*******************************************************************************************/

#include <iostream>
#include <fstream>
#include <algorithm>
#include <math.h>
using namespace std;

const long MAXSIZE = 15000;
long M;
int N, A[MAXSIZE], Tmp[MAXSIZE];


void shift(int a, int b){
	while (a <= M){
		Tmp[a] -= b;
		a = a + ((a ^ (a - 1))&a);
	}
}

long sum(int a){
	int s = 0;
	while (a>0) {
		s += Tmp[a];
		a = a - ((a ^ (a - 1))&a);
	}
	return s;
}

int main(){

	freopen("datorii.in", "r", stdin);
	freopen("datorii.out", "w", stdout);
	scanf("%ld%d", &M, &N);

	for (int i = 1; i <= M; i++){
		int aVal;
		scanf("%d", &aVal);
		shift(i, -1 * aVal);
	}
	
	for (int i = 0; i < N; i++){
		int type, TP, VQ;
		scanf("%d%d%d", &type, &TP, &VQ);
		
		switch (type) {
			case 0: shift(TP, VQ); break;
			case 1: printf("%ld\n", sum(VQ) - sum(TP - 1)); break;
		}
			
		//(type == 0) ?  shift(TP, VQ) : printf("%ld\n", sum(VQ) - sum(TP - 1));
	}
	return 0;
}