From fcedca3fbd9a0d231a96e0848290f32800eb5f79 Mon Sep 17 00:00:00 2001 From: Baoshuo Date: Thu, 22 Dec 2022 21:09:31 +0800 Subject: [PATCH] =?UTF-8?q?P1527=20[=E5=9B=BD=E5=AE=B6=E9=9B=86=E8=AE=AD?= =?UTF-8?q?=E9=98=9F]=E7=9F=A9=E9=98=B5=E4=B9=98=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://www.luogu.com.cn/record/97791055 --- Luogu/P1527/P1527.cpp | 118 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 118 insertions(+) create mode 100644 Luogu/P1527/P1527.cpp diff --git a/Luogu/P1527/P1527.cpp b/Luogu/P1527/P1527.cpp new file mode 100644 index 00000000..2ab7dfd3 --- /dev/null +++ b/Luogu/P1527/P1527.cpp @@ -0,0 +1,118 @@ +#include +#include +#include +#include + +using std::cin; +using std::cout; +const char endl = '\n'; + +const int N = 505, + Q = 6e5 + 5; + +int n, q, ans[Q], tr[N][N]; +std::vector> qs; + +int lowbit(int x) { + return x & -x; +} + +void add(int x, int y, int v) { + for (int i = x; i <= n; i += lowbit(i)) { + for (int j = y; j <= n; j += lowbit(j)) { + tr[i][j] += v; + } + } +} + +int query(int x, int y) { + int res = 0; + + for (int i = x; i; i -= lowbit(i)) { + for (int j = y; j; j -= lowbit(j)) { + res += tr[i][j]; + } + } + + return res; +} + +void solve(const std::vector> &qs, long long l, long long r) { + if (qs.empty()) return; + + if (l == r) { + for (auto o : qs) { + if (std::get<5>(o)) { + ans[std::get<5>(o)] = l; + } + } + + return; + } + + long long mid = l + r >> 1; + std::vector> ql, qr; + + for (auto o : qs) { + int x, y, a, b, k, id; + + std::tie(x, y, a, b, k, id) = o; + + if (id) { + int t = query(a, b) - query(x - 1, b) - query(a, y - 1) + query(x - 1, y - 1); + + if (t >= k) { + ql.emplace_back(o); + } else { + qr.emplace_back(x, y, a, b, k - t, id); + } + } else { + if (k <= mid) { + add(x, y, 1); + ql.emplace_back(o); + } else { + qr.emplace_back(o); + } + } + } + + for (auto o : ql) { + int x, y, k, id; + + std::tie(x, y, std::ignore, std::ignore, k, id) = o; + + if (!id && k <= mid) add(x, y, -1); + } + + solve(ql, l, mid); + solve(qr, mid + 1, r); +} + +int main() { + std::ios::sync_with_stdio(false); + cin.tie(nullptr); + + cin >> n >> q; + + for (int i = 1; i <= n; i++) { + for (int j = 1, x; j <= n; j++) { + cin >> x; + + qs.emplace_back(i, j, 0, 0, x, 0); + } + } + + for (int i = 1, x, y, a, b, k; i <= q; i++) { + cin >> x >> y >> a >> b >> k; + + qs.emplace_back(x, y, a, b, k, i); + } + + solve(qs, std::numeric_limits::min(), std::numeric_limits::max()); + + for (int i = 1; i <= q; i++) { + cout << ans[i] << endl; + } + + return 0; +}