RMQ_ST表

int STMax[maxn][24]; 
int STMin[maxn][24]; 
int pwr[24];
void init(int n)
{
    int i, j;
    pwr[0]  = 1;
    for (i=1; i<24; ++i) pwr[i] = pwr[i-1] << 1;
    for (j=1; pwr[j]<=n; ++j) 
        for (i=1; i+pwr[j]-1<=n; ++i) {
            STMax[i][j] = max(STMax[i][j-1], STMax[i+pwr[j-1]][j-1]);
            STMin[i][j] = min(STMin[i][j-1], STMin[i+pwr[j-1]][j-1]);
        }
}

int query(int left, int right) {
    int x = int(log(double(right-left+1)) / log(2.0));
    return max(STMax[left][x], STMax[right-pwr[x]+1][x]) - min(STMin[left][x], STMin[right-pwr[x]+1][x]);
}

附上别人讲解的博客   https://www.cnblogs.com/YSFAC/p/7189571.html

                                    https://blog.csdn.net/jerry_wang119/article/details/79312519

 

posted @ 2018-07-25 09:21  过路人1998  阅读(127)  评论(0编辑  收藏  举报