Pagini recente » Cod sursa (job #528820) | Cod sursa (job #299438) | Cod sursa (job #828809) | Cod sursa (job #1580840) | Cod sursa (job #1834147)
#include <fstream>
#include <iostream>
#include <vector>
const int NMAX = 23005;
using namespace std;
ifstream f("rmq.in");
ofstream g("rmq.out");
ofstream h("timpi.out");
int Q, x, y;
int M[NMAX][NMAX];
int A[NMAX];
int N;
vector <int> X;
vector <int> Y;
void preproces() {
for (int i = 0; i < N; ++i) {
M[i][i] = i;
}
for (int i = 0; i < N; ++i) {
for (int j = i + 1; j < N; ++j) {
if (A[M[i][j-1]] < A[j]) {
M[i][j] = M[i][j-1];
}
else {
M[i][j] = j;
}
}
}
}
int query(int i, int j) {
return M[i][j];
}
void citire() {
f >> N >> Q;
for (int i = 0; i < N; ++i) {
f >> A[i];
}
int x, y;
for (int i = 0; i < Q; ++i) {
f >> x >> y;
X.push_back(x);
Y.push_back(y);
}
}
void testare() {
clock_t t0, t;
float time_test;
for (int i = 0; i < Q; ++i) {
x = X[i];
y = Y[i];
g << A[query(x-1, y-1)] << '\n';
if (i % 100 == 0) {
t = clock() - t0;
time_test = ((float)t)/CLOCKS_PER_SEC * 1000;
}
}
}
int main() {
clock_t t0, t;
citire();
t0 = clock();
preproces();
t = clock() - t0;
float time_preprocess = ((float)t)/CLOCKS_PER_SEC * 1000;
testare();
return 0;
}