Pagini recente » Cod sursa (job #1829030) | Istoria paginii runda/vvvvvv | Cod sursa (job #1667846) | Cod sursa (job #1873489) | Cod sursa (job #2134155)
#include <cmath>
#include <fstream>
#include <vector>
using namespace std;
constexpr int kInf = (1 << 25);
using Matrix = vector<vector<int>>;
inline Matrix BuildMatrix(int rows, int cols, int init_val)
{
return Matrix(rows, vector<int>(cols, init_val));
}
int Query(const Matrix &rmq, int x, int y)
{
auto len = y - x + 1;
auto exp = (int)log2(len);
return min(rmq[exp][x], rmq[exp][y - (1 << exp) + 1]);
}
int main()
{
ifstream fin("rmq.in");
ofstream fout("rmq.out");
int n, q;
fin >> n >> q;
auto rmq = BuildMatrix(log2(n) + 1, n, kInf);
for (int i = 0; i < n; ++i) {
fin >> rmq[0][i];
}
for (int i = 1; i <= log2(n); ++i) {
for (int j = 0; j + (1 << i) <= n; ++j) {
auto end = j + (1 << i);
rmq[i][j] = min(rmq[i - 1][j], rmq[i - 1][end - (1 << (i - 1))]);
}
}
for (int i = 0; i < q; ++i) {
int x, y;
fin >> x >> y;
auto res = Query(rmq, x - 1, y - 1);
fout << res << "\n";
}
return 0;
}