From afccf45cf0da08e92cde3416ec9b49868ae43eb4 Mon Sep 17 00:00:00 2001 From: Baoshuo Date: Thu, 9 Jun 2022 09:40:21 +0800 Subject: [PATCH] =?UTF-8?q?P3960=20[NOIP2017=20=E6=8F=90=E9=AB=98=E7=BB=84?= =?UTF-8?q?]=20=E5=88=97=E9=98=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://www.luogu.com.cn/record/77127364 --- Luogu/P3960/P3960.cpp | 78 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 Luogu/P3960/P3960.cpp diff --git a/Luogu/P3960/P3960.cpp b/Luogu/P3960/P3960.cpp new file mode 100644 index 00000000..3156ddd2 --- /dev/null +++ b/Luogu/P3960/P3960.cpp @@ -0,0 +1,78 @@ +#include +#include + +using std::cin; +using std::cout; +const char endl = '\n'; + +const int N = 3e5 + 5; + +struct node { + int l, r, d; + + node() + : l(0), r(0), d(0) {} +} tr[N << 4]; + +int n, m, q, max, cnt, root[N]; +std::vector r[N]; + +int query(int u, int l, int r, int x) { + if (l == r) return l; + + int mid = l + r >> 1; + int c = mid - l + 1 - tr[tr[u].l].d; + + if (x <= c) return query(tr[u].l, l, mid, x); + return query(tr[u].r, mid + 1, r, x - c); +} + +void remove(int &u, int l, int r, int x) { + if (!u) u = ++cnt; + tr[u].d++; + + if (l == r) return; + + int mid = l + r >> 1; + if (x <= mid) remove(tr[u].l, l, mid, x); + else remove(tr[u].r, mid + 1, r, x); +} + +long long row(int x, int y) { + int p = query(root[x], 1, max, y); + remove(root[x], 1, max, p); + return p < m ? 1ll * (x - 1) * m + p : r[x][p - m]; +} + +long long col(int y) { + int p = query(root[0], 1, max, y); + remove(root[0], 1, max, p); + return p <= n ? 1ll * p * m : r[0][p - n - 1]; +} + +int main() { + std::ios::sync_with_stdio(false); + cin.tie(nullptr); + + cin >> n >> m >> q; + + max = std::max(n, m) + q; + + for (int i = 1, x, y; i <= q; i++) { + cin >> x >> y; + + long long p; + if (y == m) { // ζœ€εŽδΈ€εˆ— + p = col(x); + r[0].push_back(p); + } else { + p = row(x, y); + r[0].push_back(p); + r[x].push_back(col(x)); + } + + cout << p << endl; + } + + return 0; +}