返回顶部

模板 - ST表

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

inline int read() {
    char c = getchar();
    int x = 0, f = 1;
    while(c < '0' || c > '9') {
        if(c == '-')
            f = -1;
        c = getchar();
    }
    while(c >= '0' && c <= '9') {
        x = x * 10 + c - '0';
        c = getchar();
    }
    return x * f;
}

static const int MAXLOGN = 17;
static const int MAXN = 100000;
int logn[MAXN + 5];
int f[MAXN + 5][MAXLOGN + 1];

inline void init1() {
    logn[1] = 0;
    for(int i = 2; i <= MAXN; i++)
        logn[i] = logn[i / 2] + 1;
}

inline void init2(int n) {
    for(int i = 1; i <= n; i++)
        f[i][0] = read();
    for(int j = 1; j <= MAXLOGN; j++)
        for(int i = 1; i + (1 << j) - 1 <= n; i++)
            f[i][j] = max(f[i][j - 1], f[i + (1 << (j - 1))][j - 1]);
}

inline int range_max(int l, int r) {
    int s = logn[r - l + 1];
    return max(f[l][s], f[r - (1 << s) + 1][s]);
}

int main() {
    int n = read(), m = read();
    st.init1();
    st.init2(n);
    for(int i = 1; i <= m; i++) {
        int l = read(), r = read();
        printf("%d\n", st.range_max(l, r));
    }
    return 0;
}
posted @ 2019-10-27 15:50  Inko  阅读(...)  评论(...编辑  收藏