Cod sursa(job #2864890)

Utilizator justin.stoicaJustin Stoica justin.stoica Data 8 martie 2022 12:12:11
Problema Range minimum query Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.46 kb
// panda.cpp : This file contains the 'main' function. Program execution begins and ends there.
//

#include <iostream>
using namespace std;
int rmq[100005][30];
int v[100005];
int n;
void buildRmq() {
	for (int i = 0; i < n; i++)
		rmq[i][0] = v[i];
	for (int j = 1; (1 << j) <= n; j++) {
		for (int i = 0; i + (1 << j) - 1 < n; i++) {
			if (rmq[i][j - 1] > rmq[i + (1 << (j - 1))][j - 1]) {
				rmq[i][j] = rmq[i][j - 1];
			}
			else
				rmq[i][j] = rmq[i + (1 << (j - 1))][j - 1];
		}
	}
}
int query(int L, int R) {
	int floor = (int)log2(R - L + 1);
	return max(rmq[L][floor], rmq[R - (1 << floor) + 1][floor]);
}
int main()
{
	cin >> n;
	int m;
	cin >> m;
	for (int i = 0; i < n; i++)
		cin >> v[i];
	buildRmq();
	while (m--) {
		int x, y;
		cin >> x >> y;
		cout << query(x - 1, y - 1) << '\n';
	}
}

// Run program: Ctrl + F5 or Debug > Start Without Debugging menu
// Debug program: F5 or Debug > Start Debugging menu

// Tips for Getting Started: 
//   1. Use the Solution Explorer window to add/manage files
//   2. Use the Team Explorer window to connect to source control
//   3. Use the Output window to see build output and other messages
//   4. Use the Error List window to view errors
//   5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project
//   6. In the future, to open this project again, go to File > Open > Project and select the .sln file