POJ 3264 Balanced Lineup

题目链接:POJ 3264 Balanced Lineup

题目大意:
POJ 3264 Balanced Lineup

题解:
ST表模板。

#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;

int num[50005], minn[50005][50], maxn[50005][50], n, q;

void stPreWork() {
    for (int i = 1; i <= n; ++i) {
        minn[i][0] = num[i];
        maxn[i][0] = num[i];
    }
    for (int j = 1; (1 << j) <= n; ++j) {
        for (int i = 1; i + (1 << j) - 1 <= n; ++i) {
            minn[i][j] = min(minn[i][j - 1], minn[i + (1 << (j - 1))][j - 1]);
            maxn[i][j] = max(maxn[i][j - 1], maxn[i + (1 << (j - 1))][j - 1]);
        }
    }
}

int stQuery(int l, int r) {
    int k = 0;
    while (l + (1 << (k + 1)) - 1 <= r) {
        k++;
    }
    return max(maxn[l][k], maxn[r - (1 << k) + 1][k]) - min(minn[l][k], minn[r - (1 << k) + 1][k]);
}

int main() {
    scanf("%d%d", &n, &q);
    for (int i = 1; i <= n; ++i) {
        scanf("%d", &num[i]);
    }
    stPreWork();
    for (int i = 1, l, r; i <= q; ++i) {
        scanf("%d%d", &l, &r);
        printf("%d\n", stQuery(l, r));
    }
    return 0;
}
上一篇:继承


下一篇:ubuntu 12.04 rails server 时候报错 execjs