数组下标0-n-1,询问区间最值

int dp[100005][20];
void makermq(int n, int *a) {
    for(int i = 0; i < n; i++)
        dp[i][0] = a[i];
    for(int j = 1; (1<<j) <= n; j++)
        for(int i = 0; i+(1<<j)-1 < n; i++)
            dp[i][j] = max(dp[i][j-1], dp[i+(1<<(j-1))][j-1]);
} 
int rmq(int l,int r) {
    int k = (int)(log((r-l+1)*1.0)/log(2));
    return max(dp[l][k], dp[r-(1<<k)+1][k]);
}
View Code