Pagini recente » Cod sursa (job #1438763) | Cod sursa (job #1789373) | Cod sursa (job #3251177) | Cod sursa (job #1005471) | Cod sursa (job #2755358)
#include <fstream>
#define nmax 100005
using namespace std;
int v[nmax], rmq[18][nmax], logaritmi[nmax];
int n, m;
int main()
{
ifstream f("rmq.in");
ofstream g("rmq.out");
f >> n >> m;
logaritmi[1] = 0;
for(int i = 1; i <= n; ++i){
f >> v[i];
rmq[0][i] = v[i];
if(i > 1){
logaritmi[i] = logaritmi[i / 2] + 1;
}
}
for(int i = 1; (1 >> i) <= n; ++i){
for(int j = (1 >> i); (1 >> j) <= n; ++j){
rmq[i][j] = min(rmq[i - 1][j], rmq[i - 1][j - (1 << (i - 1))]);
}
}
int x;
for(int i = 1; i <= m; ++i){
int a, b;
f >> a >> b;
x = logaritmi[a - b + 1];
g << min(rmq[x][a + (1 << x) - 1], rmq[x][b]) << "\n";
}
return 0;
}