ST表及RMQ区间最值查询模板

void ST(int n) {
    for (int i = 1; i <= n; i++)
        dp[i][0] = A[i];//A[]即需要查询的数组,dp规划行程ST表
    for (int j = 1; (1 << j) <= n; j++) {
        for (int i = 1; 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 = 0;
    while ((1 << (k + 1)) <= r - l + 1) k++;
    return max(dp[l][k], dp[r - (1 << k) + 1][k]);
}
posted @ 2021-07-15 14:40  cheems~  阅读(27)  评论(0编辑  收藏  举报