Pagini recente » Cod sursa (job #1694794) | Cod sursa (job #2058601) | Cod sursa (job #2450128) | Cod sursa (job #2506864) | Cod sursa (job #3225470)
#include <bits/stdc++.h>
using namespace std;
using pii = pair<int,int>;
using ll = long long;
#define pb push_back
ifstream fin("rmq.in");
ofstream fout("rmq.out");
const int N = 1e5+3;
int n, m, rmq[18][N], query(int,int);
int main()
{
fin.tie(0); fout.tie(0);
ios_base::sync_with_stdio(0);
fin >> n >> m;
for (int i = 1; i <= n; i++)
fin >> rmq[0][i];
for (int lg = 2, h = 1; lg <= n; lg *= 2, h++)
for (int i = 1; i + lg - 1 <= n; i++)
rmq[h][i] = min(rmq[h-1][i], rmq[h-1][i+lg/2]);
while (m--){
int x, y; fin >> x >> y;
if (x > y) swap(x, y);
fout << query(x, y) << '\n';
}
return 0;
}
int query(int l, int r){
int lg = r - l + 1;
int pw = 31 - __builtin_clz(lg);
return min(rmq[pw][l], rmq[pw][r-(1<<pw)+1]);
}
//17:35