Cod sursa(job #603138)

Utilizator razyelxrazyelx razyelx Data 14 iulie 2011 18:41:52
Problema Datorii Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.84 kb
#include <fstream>

using namespace std;

const int N = 15001;
const int M = 100001;

int arb[N], n , m;

void update(int ind, int val){
	
	int poz = 0;
	
	while (ind <= n) {
		arb[ind] += val;
		
		while ( ! ( ind & (1<<poz))) poz++;
		
		ind += (1<<poz);
		poz++;
	}

}

int query(int ind){

	int poz = 0, s = 0;
	
	while (ind > 0){
		s += arb[ind];
		
		while ( ! ( ind & (1<<poz))) poz++;
		
		ind -= (1<<poz);
		poz++;
	}
	
	return s;

}

int main(){

	ifstream fin("datorii.in");
	ofstream fout("datorii.out");
	
	int x = 0, a, b;
	
	fin >> n >> m;
	
	for (int i = 1; i<=n; i++) {
		fin >> x;
		update(i, x);
	}
	
	for ( ; m; m--){
		fin >> x;

		if (x == 0) {
			fin >> a >> b;
			update(a, -b);			
		} else {
			fin >> a >> b;
			fout << query(b)- query(a-1) << "\n";
		}
	}
	
	return 0;
}