Cod sursa(job #805865)

Utilizator dm1sevenDan Marius dm1seven Data 1 noiembrie 2012 12:37:05
Problema Arbori indexati binar Scor 40
Compilator cpp Status done
Runda Arhiva educationala Marime 3.73 kb
#include <iostream>
#include <fstream>
using namespace std;

//#include "../utils/PerformanceTimer.h"

//int e_aib_v3()
int main()
{	
	//PerformanceTimer timer;
	//timer.init();

	const char operations_codes[3] = {0, 1, 2};
	char in_file[] = "aib.in";
	char out_file[] = "aib.out";

	const int MAX_N = 100000;

	int N, M;

	int A[MAX_N + 1];
	ifstream ifs(in_file);
	ofstream ofs(out_file);
	ifs>>N>>M;
	for (int i = 1; i <= N; i++) ifs>>A[i];
	for (int m = 1; m <= M; m++) 
	{		
		int operation_id, a, b;
		ifs>>operation_id>>a;
		if (operation_id != operations_codes[2])
		{
			ifs>>b;
		}

		int sum;
		//execute the operation
		switch (operation_id)
		{
		case 0://operations_codes[0]:
			A[a] += b;
			break;
		case 1://operations_codes[1]:
			sum = 0;
			for (int i = a; i <= b; i++) sum += A[i];
			ofs<<sum<<"\n";
			break;
		case 2://operations_codes[2]:
			sum = 0;
			int i = 0;
			while (sum < a) sum += A[++i];
			if (sum == a) ofs<<i<<"\n";
			else ofs<<-1<<"\n";
			break;
		}
	}	
	ifs.close();	
	ofs.close();
	
	//cout<<timer.getElapsedTime()<<endl;

	return 0;
}

/*
//int e_aib_v1()
int main()
{	
	//PerformanceTimer timer;
	//timer.init();

	const char operations_codes[3] = {0, 1, 2};
	char in_file[] = "aib.in";
	char out_file[] = "aib.out";

	const int MAX_N = 100000;
	const int MAX_M = 150000;

	int N, M;

	//int A[MAX_N + 1];
	int* A;
	//Operation operations[MAX_M + 1];
	Operation* operations;
	ifstream ifs(in_file);
	ifs>>N>>M;
	//allocate memory
	A = new int[N+1];
	operations = new Operation[M+1];
	for (int i = 1; i <= N; i++) ifs>>A[i];
	for (int m = 1; m <= M; m++) 
	{		
		ifs>>operations[m].id>>operations[m].a;
		if (operations[m].id != operations_codes[2])
		{
			ifs>>operations[m].b;
		}
	}
	ifs.close();	

	ofstream ofs(out_file);
	//perform all operations and write the results to file
	for (int m = 1; m <= M; m++)
	{
		int a = operations[m].a;
		int b = operations[m].b;
		int sum = 0;

		switch (operations[m].id)
		{
		case 0://operations_codes[0]:
			A[a] += b;
			break;
		case 1://operations_codes[1]:
			sum = 0;
			for (int i = a; i <= b; i++) sum += A[i];
			ofs<<sum<<"\n";
			break;
		case 2://operations_codes[2]:
			sum = 0;
			int i = 0;
			while (sum < a) sum += A[++i];
			if (sum == a) ofs<<i<<"\n";
			else ofs<<-1<<"\n";
			break;
		}
	}
	ofs.close();

	//cout<<timer.getElapsedTime()<<endl;

	//release the memory
	delete[] A;
	delete[] operations;

	return 0;
}*/

/*
int e_aib_v2()
//int main()
{	
	PerformanceTimer timer;
	timer.init();

	const char operations_codes[3] = {0, 1, 2};
	char in_file[] = "aib.in";
	char out_file[] = "aib.out";

	const int MAX_N = 100000;
	const int MAX_M = 150000;

	int N, M;

	int S[MAX_N + 1];//integral sums
	S[0] = 0;
	Operation operations[MAX_M + 1];
	ifstream ifs(in_file);
	ifs>>N>>M;
	ifs>>S[1];
	for (int i = 2; i <= N; i++) {ifs>>S[i]; S[i] += S[i-1];}
	for (int m = 1; m <= M; m++) 
	{		
		ifs>>operations[m].id>>operations[m].a;
		if (operations[m].id != operations_codes[2])
		{
			ifs>>operations[m].b;
		}
	}
	ifs.close();	

	ofstream ofs(out_file);
	//perform all operations and write the results to file
	for (int m = 1; m <= M; m++)
	{
		int a = operations[m].a;
		int b = operations[m].b;
		int sum = 0;

		switch (operations[m].id)
		{
		case 0://operations_codes[0]:
			for (int i = a; i <= N; i++) S[i] += b;
			break;
		case 1://operations_codes[1]:
			ofs<<S[b] - S[a-1]<<"\n";
			break;
		case 2://operations_codes[2]:
			int i = 0;
			while (S[i] < a) i++;
			if (S[i] == a) ofs<<i<<"\n";
			else ofs<<-1<<"\n";
			break;
		}
	}
	ofs.close();

	cout<<timer.getElapsedTime()<<endl;

	return 0;
}
*/