Pagini recente » Cod sursa (job #2554755) | Cod sursa (job #1500753) | Cod sursa (job #3351321) | Cod sursa (job #1262626) | Cod sursa (job #2625706)
#include <fstream>
#include <vector>
#include <cmath>
using namespace std;
int V[100000], L[100000][17];
int main()
{
ifstream fin("rmq.in");
ofstream fout("rmq.out");
int n,m,k,a,b,p;
fin>>n>>m;
for(int i=0; i<n; i++)
{
fin>>k;
V[i]=k;
}
for(int i=0; i<n; i++)
{
L[i][0]=i;
}
for(int j=1; pow(2,j)<=n; j++)
{
for(int i=0; i+pow(2,j)<=n; i++)
{
if(V[L[i][j-1]] < V[L[i+(int)pow(2,j-1)][j-1]])
{
L[i][j]=L[i][j-1];
}
else
{
L[i][j]=L[i+(int)pow(2,j-1)][j-1];
}
}
}
for(int i=0; i<m; i++)
{
fin>>a>>b;
a--; b--;
p=log2(b-a+1);
if(V[L[a][p]]<V[L[b-(int)pow(2,p)+1][p]])
{
fout<<V[L[a][p]]<<'\n';
}
else
{
fout<<V[L[b-(int)pow(2,p)+1][p]]<<'\n';
}
}
return 0;
}