Pagini recente » Cod sursa (job #3173563) | Cod sursa (job #2581106) | Cod sursa (job #2911281) | Cod sursa (job #1656477) | Cod sursa (job #3002236)
#include <iostream>
#include <fstream>
#include <cmath>
using namespace std;
ifstream f("rmq.in");
ofstream g("rmq.out");
int n,m;
int dp[100001][20];
int v[100001];
int main()
{
f >> n>>m;
for(int i =1;i<=n;i++)
{
f >> v[i];
dp[i][0] = i;
}
int puteremax = log2(n)+1;
for(int putere = 1;putere<=puteremax;putere++)
{
for(int start = 1;start<=n-(1<<putere)+1;start++)
{
if(v[dp[start][putere-1]]<v[dp[start+(1<<(putere-1))][putere-1]])
dp[start][putere] = dp[start][putere-1];
else
dp[start][putere] = dp[start+(1<<(putere-1))][putere-1];
}
}
for(int i = 1;i<=m;i++)
{
int start,fin;
f >> start>>fin;
int putere = log2(fin-start+1);
g << min(v[dp[start][putere]],v[dp[fin-(1<<putere)+1][putere]])<<'\n';
}
}