st表模板

#include<cstdio>
#include<algorithm> 
#define MAXN 1000005
using namespace std;

//换底公式 loga(b) = logc(b) / logc(a)

int F[20][MAXN];
int log[MAXN]; 
int N,M;

int main(){
    
    log[0]=0;
    log[1]=0;
    
    
    scanf("%d %d",&N,&M);
    
    for(register int i=2;i<=N;++i)log[i]=log[i>>1]+1;
    
    for(register int i=1;i<=N;++i){
        scanf("%d",&F[0][i]);
    }
    
    for(register int j=1;j<=17;++j)
    for(register int i=1;i<=N;++i)
        F[j][i] = max(F[j-1][i],F[j-1][i+(1<<(j-1))]);
    
    
    
    int l,r;
    for(register int i=1;i<=M;++i){
        scanf("%d %d",&l,&r);
        int k = log[r-l+1];
        printf("%d\n",max(F[k][l],F[k][r-(1<<k)+1]));
    }
    return 0;
}
 
posted @ 2018-02-23 14:56  Neworld1111  阅读(132)  评论(0编辑  收藏  举报