1174 区间中最大的数
dmax[i][j]表示区间[i,i+j<<2)
1 #include <iostream> 2 #include <stdio.h> 3 using namespace std; 4 int n,q,dmax[10010][25],a[10010]; 5 void init(){ 6 for(int i = 1; i <= n; i ++){ 7 dmax[i][0] = a[i]; 8 } 9 for(int j = 1; (1<<j) <= n; j ++){ 10 for(int i = 1; i+(1<<j)-1<= n; i ++){ 11 dmax[i][j] = max(dmax[i][j-1],dmax[(1<<(j-1))+i][j-1]); 12 } 13 } 14 } 15 int getValue(int l, int r){ 16 int k = 0; 17 while(1<<(k+1) <= (r-l+1))k++; 18 return max(dmax[l][k],dmax[r-(1<<k)+1][k]); 19 } 20 int main(){ 21 scanf("%d",&n); 22 for(int i = 1; i <= n; i ++){ 23 scanf("%d",&a[i]); 24 } 25 init(); 26 scanf("%d",&q); 27 while(q--){ 28 int l, r; 29 scanf("%d%d",&l,&r); 30 printf("%d ",getValue(++l,++r)); 31 } 32 return 0; 33 }