RMQ & ST表
题意:N 个数,M次询问,A到B区间内的最大数。
1 ≤ N,A,B ≤ 2e5,1 ≤ M ≤ 1e4
输入样例:
6
34 1 8 123 3 2
4
1 2
1 5
3 4
2 3
输出样例:
34
123
123
8
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 10, M = 18; int n, m, a[N], f[N][M]; int main() { cin >> n; for(int i = 1; i <= n; i ++) cin >> a[i]; for(int j = 0; j < M; j ++) for(int i = 1; i + (1 << j) - 1 <= n; i ++) { if(!j) f[i][j] = a[i]; else f[i][j] = max(f[i][j - 1], f[i + (1 << j - 1)][j - 1]); } cin >> m; while(m --) { int l, r; cin >> l >> r; int len = log(r - l + 1) / log(2); cout << max(f[l][len], f[r - (1 << len) + 1][len]) << endl; } }