标签:acm
#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 */
标签:acm
原文地址:http://blog.csdn.net/dojintian/article/details/46484019