Pagini recente » Cod sursa (job #1510598) | Cod sursa (job #1537963) | Cod sursa (job #106705) | Cod sursa (job #1613846) | Cod sursa (job #2864907)
// panda.cpp : This file contains the 'main' function. Program execution begins and ends there.
//
#include <fstream>
#include <math.h>
using namespace std;
int rmq[100005][30];
int v[100005];
int n;
ifstream cin("rmq.in");
ofstream cout("rmq.out");
void buildRmq(void) {
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++) {
rmq[i][j] = min(rmq[i][j - 1], rmq[i + (1 << (j - 1))][j - 1]);
}
}
}
int query(int L, int R) {
int floor = (int)log2(R - L + 1);
int ans = min(rmq[L][floor], rmq[R - (1 << floor) + 1][floor]);
return ans;
}
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