#include <bits/stdc++.h>
using namespace std;
const int N = 1<<20;
const int LOG = 21;
int rmq[N][LOG];
int arr[N],n;
void preprocess(int *a, int n) {
int i,j;
for(i=1;i<=n;i++) rmq[i][0]=a[i];
for(j=1;(1<<j)<=n;j++) for(i=1;i+(1<<j)-1<=n;i++) rmq[i][j]=min(rmq[i][j-1],rmq[i+(1<<(j-1))][j-1]);
}
int query(int a, int b) {
int k=log2(b-a+1);
return min(rmq[a][k],rmq[b-(1<<k)+1][k]);
}
int main() {
n=5;
arr[1]=3;
arr[2]=5;
arr[3]=1;
arr[4]=5;
arr[5]=2;
preprocess(arr,5);
cout<<query(1,5)<<endl<<query(1,2)<<endl<<query(4,5)<<endl;
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7Cgpjb25zdCBpbnQgTiA9IDE8PDIwOwpjb25zdCBpbnQgTE9HID0gMjE7CgppbnQgcm1xW05dW0xPR107CmludCBhcnJbTl0sbjsKCnZvaWQgcHJlcHJvY2VzcyhpbnQgKmEsIGludCBuKSB7CglpbnQgaSxqOwoJZm9yKGk9MTtpPD1uO2krKykgcm1xW2ldWzBdPWFbaV07Cglmb3Ioaj0xOygxPDxqKTw9bjtqKyspIGZvcihpPTE7aSsoMTw8aiktMTw9bjtpKyspIHJtcVtpXVtqXT1taW4ocm1xW2ldW2otMV0scm1xW2krKDE8PChqLTEpKV1bai0xXSk7Cn0KCmludCBxdWVyeShpbnQgYSwgaW50IGIpIHsKCWludCBrPWxvZzIoYi1hKzEpOwoJcmV0dXJuIG1pbihybXFbYV1ba10scm1xW2ItKDE8PGspKzFdW2tdKTsKfQoKaW50IG1haW4oKSB7CgluPTU7CglhcnJbMV09MzsKCWFyclsyXT01OwoJYXJyWzNdPTE7CglhcnJbNF09NTsKCWFycls1XT0yOwoJcHJlcHJvY2VzcyhhcnIsNSk7Cgljb3V0PDxxdWVyeSgxLDUpPDxlbmRsPDxxdWVyeSgxLDIpPDxlbmRsPDxxdWVyeSg0LDUpPDxlbmRsOwoJcmV0dXJuIDA7Cn0=