#include <iostream> #include <cmath> #include <cstdio> using namespace std; #define maxn 100000 int a[maxn]; int n; int d[maxn][20]; void RMQ_init() { for(int i=1; i<=n; i++) d[i][0] = a[i]; for(int j=1; (1<<j)<=n; j++) for(int i=1; i+j-1<=n; i++) { d[i][j] = min(d[i][j-1], d[i+(1<<(j-1))][j-1]); } } int RMQ(int L, int R) { int k = (int)(log((double)R-L+1)/log(2.0)); return min(d[L][k], d[R-(1<<k)+1][k]); } int main() { cin>>n; for(int i=1; i<=n; i++) cin>>a[i]; RMQ_init(); int q; cin>>q; int l, r; while(q--) { cin>>l>>r; cout<<RMQ(l, r)<<endl; } return 0; } /* 10 1 6 5 4 8 7 5 4 3 3 100 */
时间: 2024-10-23 11:37:12