Pagini recente » Cod sursa (job #694978) | Cod sursa (job #3241051) | Cod sursa (job #1116226) | Cod sursa (job #3207485) | Cod sursa (job #3299961)
#include <fstream>
using namespace std;
class InParser {
private:
FILE *fin;
char *buff;
int sp;
char read_ch() {
++sp;
if (sp == 4096) {
sp = 0;
fread(buff, 1, 4096, fin);
}
return buff[sp];
}
public:
InParser(const char* nume) {
fin = fopen(nume, "r");
buff = new char[4096]();
sp = 4095;
}
InParser& operator >> (int &n) {
char c;
while (!isdigit(c = read_ch()) && c != '-');
int sgn = 1;
if (c == '-') {
n = 0;
sgn = -1;
} else {
n = c - '0';
}
while (isdigit(c = read_ch())) {
n = 10 * n + c - '0';
}
n *= sgn;
return *this;
}
InParser& operator >> (long long &n) {
char c;
n = 0;
while (!isdigit(c = read_ch()) && c != '-');
long long sgn = 1;
if (c == '-') {
n = 0;
sgn = -1;
} else {
n = c - '0';
}
while (isdigit(c = read_ch())) {
n = 10 * n + c - '0';
}
n *= sgn;
return *this;
}
};
const int MAXN = 1e5 + 5;
int rmq[MAXN][17];
int lg2[MAXN];
int32_t main()
{
InParser cin("rmq.in");
OutParser cout("rmq.out");
for(int i = 2; i < MAXN; i++)
lg2[i] = lg2[i / 2] + 1;
int n, m;
cin >> n >> m;
for(int i = 1; i <= n; i++)
{
cin >> rmq[i][0];
}
for(int j = 1; j < 17; j++)
{
for(int i = 1; i <= n; i++)
{
rmq[i][j] = min(rmq[i][j - 1], rmq[i + (1 << (j - 1))][j - 1]);
}
}
int l, r, w;
while(m--)
{
cin >> l >> r;
w = lg2[r - l + 1];
cout << min(rmq[l][w], rmq[r - (1 << w) + 1][w]) << '\n';
}
return 0;
}