From 01b221bea124525273440cf05ca71f2f26296c6b Mon Sep 17 00:00:00 2001 From: Ren Baoshuo Date: Wed, 2 Dec 2020 10:09:33 +0800 Subject: [PATCH] =?UTF-8?q?P1886=20=E6=BB=91=E5=8A=A8=E7=AA=97=E5=8F=A3=20?= =?UTF-8?q?/=E3=80=90=E6=A8=A1=E6=9D=BF=E3=80=91=E5=8D=95=E8=B0=83?= =?UTF-8?q?=E9=98=9F=E5=88=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit R43006059 --- problem/P1886/P1886.cpp | 91 ++++++++++++++++++++++++++++++----------- 1 file changed, 68 insertions(+), 23 deletions(-) diff --git a/problem/P1886/P1886.cpp b/problem/P1886/P1886.cpp index 6e7a4e08..c202ebb3 100644 --- a/problem/P1886/P1886.cpp +++ b/problem/P1886/P1886.cpp @@ -3,35 +3,80 @@ using namespace std; struct node { - int key, val; -} a[1000005]; + int l, r; + int min, max; -int n, k; -deque q; + node() { + l = r = 0; + min = 0x3f3f3f3f; + max = -0x3f3f3f3f; + } + node(int _l, int _r) { + l = _l; + r = _r; + min = 0x3f3f3f3f; + max = -0x3f3f3f3f; + } +} tr[1000005 << 2]; +int n, k, a[1000005]; + +void pushup(int u) { + tr[u].max = max(tr[u << 1].max, tr[u << 1 | 1].max); + tr[u].min = min(tr[u << 1].min, tr[u << 1 | 1].min); +} + +void build(int u, int l, int r) { + tr[u] = node(l, r); + if (l == r) { + tr[u].max = tr[u].min = a[l]; + return; + } + int mid = l + r >> 1; + build(u << 1, l, mid); + build(u << 1 | 1, mid + 1, r); + pushup(u); +} + +pair query(int u, int l, int r) { + if (l <= tr[u].l && r >= tr[u].r) { + return make_pair(tr[u].max, tr[u].min); + } + int mid = tr[u].l + tr[u].r >> 1; + pair ans, tmp; + ans.first = -0x3f3f3f3f; + ans.second = 0x3f3f3f3f; + if (l <= mid) { + tmp = query(u << 1, l, r); + ans.first = max(ans.first, tmp.first); + ans.second = min(ans.second, tmp.second); + } + if (r > mid) { + tmp = query(u << 1 | 1, l, r); + ans.first = max(ans.first, tmp.first); + ans.second = min(ans.second, tmp.second); + } + return ans; +} int main() { - cin >> n >> k; + scanf("%d%d", &n, &k); for (int i = 1; i <= n; i++) { - a[i].key = i; - cin >> a[i].val; + scanf("%d", &a[i]); } - // 最小值 - for (int i = 1; i <= n; i++) { - while (!q.empty() && a[i].val <= q.front().val) q.pop_front(); - q.push_front(a[i]); - while (q.back().key <= i - k) q.pop_back(); - if (i >= k) cout << q.back().val << ' '; + build(1, 1, n); + vector ans_min, ans_max; + for (int i = 1; i + k - 1 <= n; i++) { + pair tmp = query(1, i, i + k - 1); + ans_max.push_back(tmp.first); + ans_min.push_back(tmp.second); } - q.clear(); - cout << endl; - // 最大值 - for (int i = 1; i <= n; i++) { - while (!q.empty() && a[i].val >= q.front().val) q.pop_front(); - q.push_front(a[i]); - while (q.back().key <= i - k) q.pop_back(); - if (i >= k) cout << q.back().val << ' '; + for (int i = 0; i < ans_min.size(); i++) { + printf("%d ", ans_min[i]); } - q.clear(); - cout << endl; + printf("\n"); + for (int i = 0; i < ans_max.size(); i++) { + printf("%d ", ans_max[i]); + } + printf("\n"); return 0; }