https://www.luogu.org/problemnew/show/P3865
//注意预处理
1 #include <iostream> 2 #include <cstdio> 3 #include <algorithm> 4 #include <cmath> 5 #include <cctype> 6 #include <cstring> 7 using namespace std; 8 9 int n[100005], st[100005][21]; 10 inline int read() { 11 register int n, ch, f = 1; 12 n = 0, ch = getchar(); 13 while (!isdigit(ch)) {if (ch == ‘-‘) f = -1; ch = getchar();} 14 while (isdigit(ch)) n = (n << 3) + (n << 1) + ch - ‘0‘, ch = getchar(); 15 return n*f; 16 } 17 int logg[100005], gg[21]; 18 inline int RMQ(int l, int r){ 19 int k = logg[r-l+1];//log2(r-l+1); 20 return max(st[l][k], st[r-gg[k]+ 1][k]); 21 } 22 23 int main(void){ 24 int m = read(), Q = read(), t = 0; 25 for(int i = 1; i <= m; ++i){//预处理log2(m); 26 if (i >= (1 << (t+1)))t++; 27 logg[i] = t; 28 } 29 for(int i = 0; i <= 20; ++i) gg[i] = (1 << i); 30 for(int i = 1; i <= m; ++i) n[i] = read(), st[i][0] = n[i]; 31 int k = logg[m];//log2(m); 32 for(int j = 1; j <= k; ++j) 33 for(int i = 1; i + gg[j] - 1 <= m; ++i){ 34 st[i][j] = max(st[i][j-1], st[i + gg[j-1]][j-1]); 35 } 36 while(Q--){ 37 int l = read(), r = read(); 38 printf("%d\n", RMQ(l, r)); 39 } 40 return 0; 41 }