Pagini recente » Cod sursa (job #2739237) | Cod sursa (job #1413434) | Cod sursa (job #1005473) | Cod sursa (job #3255523) | Cod sursa (job #2755370)
#include <bits/stdc++.h>
using namespace std;
int n, m;
int rmq[17][100001];
int logaritmi[100001];
int main()
{
ifstream f("rmq.in");
ofstream g("rmq.out");
f >> n >> m;
logaritmi[1] = 0;
for(int i = 1; i <= n; i++){
f >> rmq[0][i];
if(i > 1){
logaritmi[i] = logaritmi[i / 2] + 1;
}
}
for(int i = 1; (1 << i) <= n; i++){
for(int j = (1 << i); j <= n; j++){
rmq[i][j] = min(rmq[i - 1][j], rmq[i - 1][j - (1 << (i - 1))]);
}
}
int x, a, b;
for(int i = 1; i <= m; ++i)
{
f >> a >> b;
x = logaritmi[b - a + 1];
g << min(rmq[x][a + (1 << x) - 1], rmq[x][b]) << "\n";
}
return 0;
}