Cod sursa(job #2421100)

Utilizator cyg_TheoPruteanu Theodor cyg_Theo Data 14 mai 2019 11:50:05
Problema Range minimum query Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.79 kb
#include <bits/stdc++.h>

using namespace std;

const int MAXN = 1e5 + 1;
const int MAXLOG = 20;

int rmq[MAXLOG][MAXN];
int logg[MAXN];

int main() {
	#ifdef BLAT
		freopen("input", "r", stdin);
	#else
		freopen("rmq.in", "r", stdin);
		freopen("rmq.out", "w", stdout);
	#endif

	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);

	int n, m;
	cin >> n >> m;
	for(int i = 1; i <= n; ++i) {
		cin >> rmq[0][i];
	}

	for(int i = 2; i <= n; ++i) logg[i] = 1 + logg[i>>1];

	for(int i = 1; i < MAXLOG; ++i) {
		for(int j = 1; j + (1<<i-1) <= n; ++j) {
			rmq[i][j] = min(rmq[i-1][j], rmq[i-1][j+(1<<i-1)]);
		}
	}

	while(m--) {
		int a, b;
		cin >> a >> b;
		int d = b - a + 1;
		d = logg[d];

		cout << min(rmq[d][a], rmq[d][b-(1<<d)+1]) << '\n';
	}
	return 0;
}