Cod sursa(job #3152699)

Utilizator AndPitAndreeaPiticar AndPit Data 26 septembrie 2023 12:54:45
Problema Range minimum query Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.58 kb
#include <fstream>
#include <vector>
#include <cmath>
using namespace std;
ifstream f("rmq.in");
ofstream g("rmq.out");
const int nMax = 1e5+1;
const int logN = 19;
int st[nMax][logN];
int main() {
	int n, q;
	f >> n >> q;
	for (int i = 0; i < n; ++i)
		f >> st[i][0];
	for (int i = 1; i < logN; ++i) {
		for (int j = 0; j <= n - (1 << i); ++j) {
			st[j][i] = min(st[j][i - 1], st[j + (1 << (i - 1))][i - 1]);
		}
	}
	while (q--) {
		int a, b;
		f >> a >> b;
		a--; b--;
		int k = log2(b - a + 1);
		g << min(st[a][k], st[b - (1 << k) + 1][k]) << '\n';
	}
	return 0;
}