学了线段树回来打打暴力(不是)
时间复杂度 $O(nlogn)$, 常数比较大,需要吸一下氧才能AC
#pragma GCC optimize(3)
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 1000010;
struct T{
int l, r, mx, mi;
}tr[N * 4];
int n, k;
int a[N];
inline void pushup(int u) {
tr[u].mi = min(tr[u << 1].mi, tr[u << 1 | 1].mi);
tr[u].mx = max(tr[u << 1].mx, tr[u << 1 | 1].mx);
}
void build(int u, int l, int r) {
tr[u] = {l, r, a[l], a[l]};
if(l == r) return;
else {
int mid = l + r >> 1;
build(u << 1, l, mid), build(u << 1 | 1, mid + 1, r);
pushup(u);
}
}
int querymi(int u, int l, int r) {
if(tr[u].l >= l && tr[u].r <= r) return tr[u].mi;
else {
int mid = tr[u].l + tr[u].r >> 1;
int v = 1e9;
if(l <= mid) v = min(v, querymi(u << 1, l, r));
if(r > mid) v = min(v, querymi(u << 1 | 1, l, r));
return v;
}
}
int querymx(int u, int l, int r) {
if(tr[u].l >= l && tr[u].r <= r) return tr[u].mx;
else {
int mid = tr[u].l + tr[u].r >> 1;
int v = -1e9;
if(l <= mid) v = max(v, querymx(u << 1, l, r));
if(r > mid) v = max(v, querymx(u << 1 | 1, l, r));
return v;
}
}
int main()
{
scanf("%d%d", &n, &k);
for(int i = 1; i <= n; i++) scanf("%d", &a[i]);
build(1, 1, n);
for(int i = 1; i <= n - k + 1; i++) printf("%d%c", querymi(1, i, i + k - 1), " \n"[i == n - k + 1]);
for(int i = 1; i <= n - k + 1; i++) printf("%d ", querymx(1, i, i + k - 1));
return 0;
}
反正都要带一个 log,为什么不写 ST 表,简单好写常数小。
爆空间了O.o
当我没说。