Cod sursa(job #2812537)

Utilizator sanzianagrecuSanziana Grecu sanzianagrecu Data 4 decembrie 2021 17:35:32
Problema Range minimum query Scor 20
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.81 kb
#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;
}