[HDU5443]The Water Problem

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5443

一道ST表的板子题。ST表可以用于满足幂等律$x\times x=x$的运算,如最大最小值和gcd,因为其对重叠现象不敏感,时间复杂度$O(nlogn)$,常数小于线段树,用得不多。

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int bin[10],len[1010];
int Max[1010][10];
int main() {
    bin[0] = 1;
    len[1] = 0;
    for (int i = 1; i < 10; i++) bin[i] = bin[i - 1] << 1;
    for (int i = 2; i <= 1000; i++) len[i] = len[i >> 1] + 1;
    int T;
    scanf("%d", &T);
    while (T--) {
        int n;
        scanf("%d", &n);
        for (int i = 1; i <= n; i++) scanf("%d", &Max[i][0]);
        for (int k = 1; k < 10; k++)
            for (int i = 1; i + bin[k] <= n + 1; i++)
                Max[i][k] = max(Max[i][k - 1], Max[i + bin[k - 1]][k - 1]);
        int q;
        scanf("%d", &q);
        while (q--) {
            int l, r;
            scanf("%d%d", &l, &r);
            int k;
            for (int i = 0; i < 10; i++) {
                if (r - l + 1 < bin[i]) break;
                k = i;
            }
            printf("%d\n", max(Max[l][k], Max[r - bin[k] + 1][k]));
        }
    }
    return 0;
}

 

 

 

 

posted @ 2020-07-15 02:42  halfrot  阅读(115)  评论(0编辑  收藏  举报