思路:
使用ST表
ST模板可参考MT3024 max=min
代码:
1.暴力9/10:
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
int n, m;
int a[N];
int main()
{ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);cin >> n >> m;for (int i = 1; i <= n; i++){cin >> a[i];}int l, r;while (m--){cin >> l >> r;int ans = 0x3f3f3f3f;for (int i = l; i <= r; i++){ans = min(ans, a[i]);}cout << ans << "\n";}return 0;
}
2.ST表
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10;
int n, m, a[N], mn[N][20], Lg[N], l, r;
void pre()
{Lg[1] = 0;for (int i = 2; i <= n; i++){Lg[i] = Lg[i >> 1] + 1;}
}
void ST_create()
{ // 创建ST表for (int i = 1; i <= n; i++){mn[i][0] = a[i];}for (int j = 1; j <= Lg[n]; j++){for (int i = 1; i <= n - (1 << j) + 1; i++){mn[i][j] = min(mn[i][j - 1], mn[i + (1 << (j - 1))][j - 1]);}}
}
int ST_qmin(int l, int r)
{ // ST表求minint k = Lg[r - l + 1];return min(mn[l][k], mn[r - (1 << k) + 1][k]);
}
int main()
{cin >> n >> m;for (int i = 1; i <= n; i++)cin >> a[i];pre();ST_create();while (m--){cin >> l >> r;cout << ST_qmin(l, r) << endl;}return 0;
}