Cod sursa(job #743414)

Utilizator toniobFMI - Barbalau Antonio toniob Data 4 mai 2012 11:41:45
Problema Datorii Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.84 kb
#include <fstream>
using namespace std;

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

int n,m,aib[15007];

void update (int val, int poz) {
	while (poz <= n) {
		aib[poz] += val;
		poz += poz & (-poz);
	}
}

int suma (int poz) {
	int s = 0;
	while (poz) {
		s += aib[poz];
		poz &= poz - 1;
	}
	return s;
}

int bs (int val) {
	int i = 0,step = 1<<17;
	for (; step; step >>= 1) {
		if (i + step <= n && suma (i + step) < val) {
			i += step;
		}
	}
	return i+1;
}

int main () {
	in >> n >>m;
	for (int i = 1,x; i <= n; ++i) {
		in >> x;
		update (x,i);
	}
	for (int i = 1,tip,a,b; i <= m; ++i) {
		in >> tip;
		if (tip == 0) {
			in >> a >> b;
			update(-b,a);
			continue;
		}
		if (tip == 1) {
			in >> a >> b;
			out << suma (b) - suma (a - 1) << '\n';
			continue;
		}
	}
	return 0;
}