Pagini recente » Cod sursa (job #2061959) | Cod sursa (job #1644542) | Borderou de evaluare (job #1567422) | Cod sursa (job #1253723) | Cod sursa (job #3241992)
#include <fstream>
#include <iostream>
using namespace std;
int n,qr,mat[1000000][20],pw=1;
int main()
{
ifstream fin("rmq.in");
ofstream fout("rmq.out");
fin>>n>>qr;
for(int i=0;i<n;i++)
fin>>mat[i][0];
for(int j=1;(1<<j)<=n;j++){
for(int i=0;i<n-(1<<j)+1;i++)
{
mat[i][j]=min(mat[i][j-1],mat[i+pw][j-1]);
cout << mat[i][j]<< " ";
}
cout << endl;
pw*=2;
}
for(int i=0;i<qr;i++){
int st,dr,k=1;
fin>>st>>dr;
st--;
dr--;
pw=2;
while(pw<=dr-st+1){
pw*=2;
k++;
}
pw/=2;
k--;
cout << pw<< " "<<k << endl;
fout<<min(mat[st][k],mat[dr+1-pw][k])<< endl;
}
fin.close();
fout.close();
return 0;
}