From 7bdcaf5f2c8e3c1726b1f89e8e1d2d2e21928b54 Mon Sep 17 00:00:00 2001 From: Baoshuo Date: Sat, 29 Oct 2022 09:26:29 +0800 Subject: [PATCH] =?UTF-8?q?P3834=20=E3=80=90=E6=A8=A1=E6=9D=BF=E3=80=91?= =?UTF-8?q?=E5=8F=AF=E6=8C=81=E4=B9=85=E5=8C=96=E7=BA=BF=E6=AE=B5=E6=A0=91?= =?UTF-8?q?=202?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://www.luogu.com.cn/record/91986713 --- Luogu/P3834/P3834.cpp | 105 +++++++++++++++++++++--------------------- 1 file changed, 52 insertions(+), 53 deletions(-) diff --git a/Luogu/P3834/P3834.cpp b/Luogu/P3834/P3834.cpp index f42f7d03..30802c03 100644 --- a/Luogu/P3834/P3834.cpp +++ b/Luogu/P3834/P3834.cpp @@ -4,88 +4,87 @@ using std::cin; using std::cout; -using std::endl; +const char endl = '\n'; -const int N = 200005; +const int N = 2e5 + 5; -int n, m, l, r, k, a[N]; -std::vector nums; - -// Helpers -int find(int); - -// Segment Tree +int n, m, a[N]; int cnt, root[N]; -int build(int, int); -int insert(int, int, int, int); -int query(int, int, int, int, int); - -int main() { - cin >> n >> m; - for (int i = 1; i <= n; i++) { - cin >> a[i]; - nums.push_back(a[i]); - } - std::sort(nums.begin(), nums.end()); - nums.erase(std::unique(nums.begin(), nums.end()), nums.end()); - root[0] = build(0, nums.size() - 1); - for (int i = 1; i <= n; i++) { - root[i] = insert(root[i - 1], 0, nums.size() - 1, find(a[i])); - } - while (m--) { - cin >> l >> r >> k; - cout << nums[query(root[r], root[l - 1], 0, nums.size() - 1, k)] << endl; - } - return 0; -} - -// === Helpers === - -inline int find(int x) { - return std::lower_bound(nums.begin(), nums.end(), x) - nums.begin(); -} - -// === Segment Tree === +std::vector nums; struct node { int l, r, c; - - node() - : l(0), - r(0), - c(0) {} } tr[N << 5]; int build(int l, int r) { int p = ++cnt; + if (l == r) return p; + int mid = l + r >> 1; + tr[p].l = build(l, mid); tr[p].r = build(mid + 1, r); + return p; } int insert(int p, int l, int r, int x) { int q = ++cnt; tr[q] = tr[p]; + if (l == r) { tr[q].c++; return q; } + int mid = l + r >> 1; - if (x <= mid) { - tr[q].l = insert(tr[p].l, l, mid, x); - } else { - tr[q].r = insert(tr[p].r, mid + 1, r, x); - } + + if (x <= mid) tr[q].l = insert(tr[q].l, l, mid, x); + else tr[q].r = insert(tr[q].r, mid + 1, r, x); + tr[q].c = tr[tr[q].l].c + tr[tr[q].r].c; + return q; } -int query(int q, int p, int l, int r, int k) { +int query(int p, int q, int l, int r, int x) { if (l == r) return l; - int c = tr[tr[q].l].c - tr[tr[p].l].c; + int c = tr[tr[p].l].c - tr[tr[q].l].c; int mid = l + r >> 1; - if (k <= c) return query(tr[q].l, tr[p].l, l, mid, k); - return query(tr[q].r, tr[p].r, mid + 1, r, k - c); + + if (x <= c) return query(tr[p].l, tr[q].l, l, mid, x); + return query(tr[p].r, tr[q].r, mid + 1, r, x - c); +} + +int main() { + std::ios::sync_with_stdio(false); + cin.tie(nullptr); + + cin >> n >> m; + + for (int i = 1; i <= n; i++) { + cin >> a[i]; + + nums.emplace_back(a[i]); + } + + std::sort(nums.begin(), nums.end()); + nums.erase(std::unique(nums.begin(), nums.end()), nums.end()); + + root[0] = build(0, nums.size() - 1); + + for (int i = 1; i <= n; i++) { + root[i] = insert(root[i - 1], 0, nums.size() - 1, std::lower_bound(nums.begin(), nums.end(), a[i]) - nums.begin()); + } + + while (m--) { + int l, r, k; + + cin >> l >> r >> k; + + cout << nums.at(query(root[r], root[l - 1], 0, nums.size() - 1, k)) << endl; + } + + return 0; }