题目描述
输入一串数字,给你 M 个询问,每次询问就给你两个数字 X,Y,要求你说出 X 到 Y这段区间内的最大数。
输入格式
第一行两个整数 N,M 表示数字的个数和要询问的次数;
接下来一行为 N 个数;
接下来 M 行,每行都有两个整数 X,Y。
输出格式
输出共 M 行,每行输出一个数。
样例
样例输入
10 2
3 2 4 5 6 8 1 2 9 7
1 4
3 8
样例输出
5
8
数据范围与提示
对于全部数据,1≤N≤10^5,1≤M≤10^6,1≤X≤Y≤N,数字不超过 C/C++
的 int
范围。
#include<cstdio> #include<iostream> #include<cstring> #include<string> using namespace std; const long long maxn=100010; int n, m; int llog[maxn], f[maxn][20]; inline void qread(int &x){x = 0;int ch = getchar();while(ch < '0' || ch > '9') ch =getchar();while(ch >='0' && ch <= '9') x = 10 * x + ch - 48, ch = getchar(); } int main(void) {qread(n), qread(m);for(int i=2; i<=n; ++i)llog[i] = llog[i / 2] + 1;for(int i=1; i<=n; ++i)qread(f[i][0]); for(int j=1; j <= llog[n]; ++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]);while(m--){int x, y;qread(x), qread(y);int s = llog[y - x + 1];printf("%d\n", max(f[x][s], f[y - (1 << s) + 1][s]));} }
思路:
ST表模板题一道,写出模板就好。
5,1≤M≤106,1≤X≤Y≤N。数字不超过 C/C++
的 int
范围。