静态区间最值问题(RMQ)
ST表。
#include <iostream>
using namespace std;
int N, Q;
int D[(1<<17)][17];
int A[(1<<17)];
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+(1<<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 = 0;
while((1<<(k+1)) <= R - L + 1) k ++;
return min(D[L][k], D[R-(1<<k)+1][k]);
}
int main() {
int L, R;
cin >> N >> Q;
for(int i=1; i<=N; i++) cin >> A[i];
RMQ_INIT();
for(int i=1; i<=Q; i++) {
cin >> L >> R;
cout << RMQ(L, R) << ' ';
}
return 0;
}