// ConsoleApplication2.cpp : This file contains the 'main' function. Program execution begins and ends there.
//
//trie
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <vector>
#include <fstream>
#define MAX_INT 0x3f3f3f3f;
using namespace std;
ifstream fin("rmq.in");
ofstream fout("rmq.out");
/*
struct trieNode {
int Min, left, right;
trieNode* leftNode, *rightNode;
trieNode() {
Min = 0;
left = 0;
right = 0;
leftNode = nullptr;
rightNode = nullptr;
//ctor
}
~trieNode() {
//dtor
}
void buildTree(trieNode* current, int left, int right) {
}
};
*/
int segTree1[1000005];
void constructTree(vector<int> input, int segTree[], int low, int high, int pos) {
if (low == high) {
segTree[pos] = input[low];
return;
}
int mid = (low + high) / 2;
constructTree(input, segTree, low, mid, 2 * pos + 1);
constructTree(input, segTree, mid + 1, high, 2 * pos + 2);
segTree[pos] = min(segTree[2 * pos + 1], segTree[2 * pos + 2]);
}
int rangeMinQuery(int segTree[], int low, int high, int qleft, int qright, int pos) {
if (qleft <= low && high <= qright)
return segTree[pos];
else {
if (qleft > high || qright < low) {
return MAX_INT;
}
int mid = (low + high) / 2;
return min(rangeMinQuery(segTree, low, mid, qleft, qright, 2 * pos + 1),
rangeMinQuery(segTree, mid + 1, high, qleft, qright, 2 * pos + 2));
}
}
vector<int>v1;
int main()
{
int n, m,a,b;
fin >> n >> m;
for (int i = 0; i < n; i++) {
fin >> a;
v1.push_back(a);
}
constructTree(v1, segTree1, 0, n - 1, 0);
for (int i = 0; i < m; i++) {
fin >> a >> b;
fout << rangeMinQuery(segTree1, 0, n - 1, a - 1, b - 1, 0) << '\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