Files
python/TangDou/AcWing/Section/1068_ST_Test.cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

42 lines
1.0 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <bits/stdc++.h>
using namespace std;
const int N = 100010;
int a[N];
int n, q;
/*ST跳表:快速查询区间内的极大极小值*/
int st1[N][40], st2[N][40];
void build() {
for (int i = 1; i <= n; i++) st1[i][0] = a[i], st2[i][0] = a[i];
for (int j = 1; (1 << j) <= n; j++)
for (int i = 1; (i + (1 << j) - 1) <= n; i++) {
st1[i][j] = max(st1[i][j - 1], st1[i + (1 << (j - 1))][j - 1]);
st2[i][j] = min(st2[i][j - 1], st2[i + (1 << (j - 1))][j - 1]);
}
}
int queryMax(int x, int y) {
int t = log2(y - x + 1);
return max(st1[x][t], st1[y - (1 << t) + 1][t]);
}
int queryMin(int x, int y) {
int t = log2(y - x + 1);
return min(st2[x][t], st2[y - (1 << t) + 1][t]);
}
/*
5
50 60 45 39 78
2 5
答案应该是45
*/
signed main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
build();
int x, y;
scanf("%d %d", &x, &y);
cout << queryMax(x, y) << endl;
cout << queryMin(x, y) << endl;
}