Pagini recente » Cod sursa (job #2930761) | Cod sursa (job #2823528) | Cod sursa (job #280001) | Cod sursa (job #277986) | Cod sursa (job #805860)
Cod sursa(job #805860)
#include <iostream>
#include <fstream>
using namespace std;
//#include "../utils/PerformanceTimer.h"
struct Operation
{
int id;
int a;
int b;
};
//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];
Operation operations[MAX_M + 1];
ifstream ifs(in_file);
ifs>>N>>M;
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;
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;
}
*/