Pagini recente » Cod sursa (job #1225814) | Cod sursa (job #595598) | Cod sursa (job #2200425) | Cod sursa (job #857776) | Cod sursa (job #2754469)
#include <bits/stdc++.h>
using namespace std;
ifstream f("rmq.in");
ofstream g("rmq.out");
class SegmentTree {
public:
SegmentTree(int sz) {
size = sz;
data = new int[sz];
tree = new int[4 * sz];
for (int i = 0; i < 4 * sz; i++) {
tree[i] = 1000000;
}
}
int size;
int *data, *tree;
int build(int i, int j, int si = 0) {
if (i == j) {
tree[si] = data[j];
return data[j];
}
int m = (i + j) / 2;
tree[si] = min(build(i, m, 2 * si + 1), build(m + 1, j, 2 * si + 2));
return tree[si];
}
int getMin(int i, int j, int qi, int qj, int si = 0) {
if (i > j || qj < i || j < qi)
return 1000000;
if (qi <= i && j <= qj)
return tree[si];
int m = (i + j) / 2;
return min(getMin(i, m, qi, qj, 2 * si + 1), getMin(m + 1, j, qi, qj, 2 * si + 2));
}
};
int N, M;
int main() {
f >> N >> M;
SegmentTree st(N);
for (int i = 0; i < N; i++) {
f >> st.data[i];
}
st.build(0, N - 1);
for (; M--;) {
int i, j;
f >> i >> j;
g << st.getMin(0, N - 1, i - 1, j - 1) << '\n';
}
return 0;
}