Cod sursa(job #1018894)

Utilizator hotfixVasile Pantelescu hotfix Data 30 octombrie 2013 06:33:16
Problema Datorii Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.08 kb
#include <fstream>
using namespace std;

int main()
{
	const char* file_name = "datorii.in";
	const char* file_out = "datorii.out";
	int N, M;

	ifstream ifs(file_name);
	ifs>>N>>M;
	int *a = new int[N + 1];
	int* op = new int[M + 1];
	int* X = new int[M + 1];
	int* Y = new int[M + 1];
	unsigned long long* sums = new unsigned long long[M + 1];
	
	//read the vector
	for (int n = 1; n <= N; n++) ifs>>a[n];
	
	//read the operations
	for (int m = 1; m <= M; m++) ifs>>op[m]>>X[m]>>Y[m];
		
	//close the files
	ifs.close();

	//read the operations one by one and process them
	for (int m = 1; m <= M; m++)
	{
		int op_type = op[m], Xm = X[m], Ym = Y[m];
		
		//process the operations
		if (op_type == 0) a[Xm] -= Ym;
		else //(op_type == 1)
		{
			sums[m] = 0;
			for (int n = Xm; n <= Ym; n++) sums[m] += a[n];
		}
	}

	ofstream ofs(file_out);
	//write the results to the file
	for (int m = 1; m <= M; m++) if (op[m] == 1) ofs<<sums[m]<<"\n";
	ofs.close();
	//release the memory
	delete[] a;	
	delete[] op; delete[] X; delete[] Y; delete[] sums;

	return 0;
}