Cod sursa(job #1007944)

Utilizator otilia_sOtilia Stretcu otilia_s Data 9 octombrie 2013 21:45:06
Problema Range minimum query Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.73 kb
#include <cstdio>
#include <algorithm>

using namespace std;
const int NMAX = 100002;
const int LGMAX = 20;

int M[LGMAX][NMAX];
int v[NMAX], n;

void rmq()
{
	for (int i = 1; i <= n; ++i)
		M[0][i] = v[i];
	
	for (int k = 1; (1 << k) <= n; ++k) 
		for (int i = 1; i <= n; ++i)
			M[k][i] = min(M[k-1][i], M[k - 1][i + 1 << (k - 1)]);
}

int main()
{
	int m;
	freopen("rmq.in","r",stdin);
	freopen("rmq.out","w",stdout);
	scanf("%d %d", &n, &m);
	for (int i = 1; i <= n; ++i)
		scanf("%d", &v[i]);
	
	rmq();
	
	for (int t = 0; t < m; ++t) {
		int x, y, k;
		scanf("%d %d", &x, &y);
		
		for (k = 0; x + (1 << k) - 1 <= y; ++k);
		--k;
		printf("%d\n", min(M[k][x], M[k][y - (1<<k) + 1]));
	}
	return 0;
}