Pagini recente » Cod sursa (job #2815335) | Cod sursa (job #2756402) | Cod sursa (job #2116986) | Cod sursa (job #1958518) | Cod sursa (job #2719760)
#include <fstream>
using namespace std;
const int maxx=100005;
const int maxlog=15;
ifstream fin("rmq.in");
ofstream fout("rmq.out");
int n,t,mat[maxlog][maxx],v[maxx];
void citire(int mat[][maxx],int &n, int &t)
{int i,j;
fin>>n>>t;
for(i=1; i<=n; i++)
{
fin>>mat[0][i];
}
}
void calcul(int mat[][maxx], int n, int v[])
{ int i,j;
v[0]=v[1]=0;
for(i=2; i<=n; i++)
{
v[i]=v[i>>1]+1;
}
for(i=1; i<=v[n]; i++)
for(j=1; j+(1<<(i-1)) <=n; j++)
mat[i][j]= min(mat[i-1][j], mat[i-1][j+(1<<(i-1))]);
}
int main()
{int x,y,z;
citire(mat,n,t);
calcul(mat,n,v);
while(t>0)
{
fin>>x>>y;
z=v[y-x+1];
fout<<min(mat[z][x], mat[z][y-(1<<z)+1]) <<'\n';
t--;
}
fin.close();
fout.close();
return 0;
}