Pagini recente » Cod sursa (job #2888588) | Cod sursa (job #3147555) | Cod sursa (job #1351526) | Cod sursa (job #2405699) | Cod sursa (job #2812537)
#include <fstream>
// solutie 1: O(n^2) - programare dinamica
using namespace std;
ifstream cin("rmq.in");
ofstream cout("rmq.out");
#define NMAX 10001
int minim[NMAX][NMAX], n, a[NMAX];
void RMQ() {
int i, j;
for (i = 1; i <= n; ++ i)
minim[i][i] = a[i];
for (i = 1; i <= n; ++ i)
for (j = i + 1; j <= n; ++ j)
if (minim[i][j - 1] < a[j])
minim[i][j] = minim[i][j - 1];
else
minim[i][j] = a[j];
}
int main() {
int m;
cin >> n >> m;
for (int i = 1; i <= n; ++ i)
cin >> a[i];
RMQ();
/*for (int i = 0; i < n; ++ i, cout << '\n')
for (int j = 0; j < n; ++ j, cout << '\n')
cout << i << ' ' << j << ':' << minim[i][j];
*/
for (int st, dr, i = 1; i <= m; ++ i) {
cin >> st >> dr;
cout << minim[st][dr] << '\n';
}
return 0;
}