Cod sursa(job #528813)

Utilizator feelshiftFeelshift feelshift Data 3 februarie 2011 15:11:44
Problema Datorii Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.09 kb
// http://infoarena.ro/problema/datorii
// rezolvata cu arbori indexati binar (http://infoarena.ro/aib)
#include <fstream>
using namespace std;

#define maxDays 15002
#define zeros(x) ( (x ^ (x - 1)) & x )

ifstream in("datorii.in");
ofstream out("datorii.out");

int days;
int AIB[maxDays];

void add(int where,int quantity);
int compute(int where);

int main() {
	int operations,value,day,left,right;
	bool interogation;

	in >> days >> operations;

	for(int currentDay=1;currentDay<=days;currentDay++) {
		in >> value;
		add(currentDay,value);
	}

	for(int i=1;i<=operations;i++) {
		in >> interogation;

		if(interogation) {	// se cere suma elementelor din intervalul (left,right)
			 in >> left >> right;
			 out << compute(right) - compute(left-1) << "\n";
		}
		else {
			in >> value >> day;
			add(day,-value);
		}
	}

	return (0);
}

void add(int where,int quantity) {
	for(int k=where;k<=days;k=k+zeros(k))
		AIB[k] += quantity;
}

int compute(int where) {
	int value = 0;

	for(int k=where;k>0;k=k-zeros(k))
		value = value + AIB[k];

	return value;
}