Cod sursa(job #371740)

Utilizator c912046Mihaila Stefan c912046 Data 6 decembrie 2009 18:07:41
Problema Datorii Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.49 kb
#include <fstream>
#include <cmath>

//FIX THIS STUPID THING ALREADY. HOW COMPLICATED CAN IT FUCKING BE?

int main ()
{
	std::ifstream f_in("datorii.in");
	std::ofstream f_out("datorii.out");
	
	unsigned int N, M, vect[15000], buckets[123], i;
	
	f_in >> N >> M;
	memset(buckets, 0, sizeof(buckets));
	
	//unsigned int bucketsize = static_cast<unsigned int>(std::sqrt(N));
	unsigned int bucketsize = 1;
	for (; bucketsize*bucketsize <= N; ++bucketsize);
	--bucketsize;
	
	
	for (i=0; i<N; ++i) { f_in >> vect[i]; buckets[i/bucketsize] += vect[i]; }
	
	for (i=0; i<M; ++i) {
		unsigned int op, a, b;
		f_in >> op >> a >> b;
		switch (op) {
		case 0:
			--a;
			vect[a] -= b;
			buckets[a/bucketsize] -= b;
			break;
		case 1:
			--a; --b;
			int startbucket = a/bucketsize, endbucket = b/bucketsize;
			if (a%bucketsize) ++startbucket;
			if (b%bucketsize != bucketsize-1) --endbucket;
			
			int startpoint = startbucket*bucketsize,endpoint=(endbucket+1)*bucketsize -1;
			unsigned int sum=0, j;
			//are there any full buckets?
			if (startbucket <= endbucket && startpoint <= b && endpoint >= a && endpoint <= b) {
				// yes
				unsigned int j;
				for (j=a; j<startpoint; ++j) sum += vect[j];
				for (j=startbucket; j<=endbucket; ++j) sum += buckets[j];
				for (j=endpoint+1; j<=b; ++j) sum += vect[j];
			}
			else {
				for (j=a; j<=b; ++j) sum += vect[j];
			}
			
			f_out << sum << '\n';
			break;
		}
	}
	
	f_in.close();
	f_out.close();
	return 0;
}