제출 #952245

#제출 시각아이디문제언어결과실행 시간메모리
952245arbuzickGarden (JOI23_garden)C++17
45 / 100
3030 ms18612 KiB
#include <bits/stdc++.h> using namespace std; void solve() { int n, m, d; cin >> n >> m >> d; vector<int> p(n), q(n); for (int i = 0; i < n; ++i) { cin >> p[i] >> q[i]; } vector<int> r(m), s(m); for (int i = 0; i < m; ++i) { cin >> r[i] >> s[i]; } int ans = d * d; if (m <= 8) { vector<int> p_nw, q_nw; for (int i = 0; i < n; ++i) { p_nw.push_back(p[i]); p_nw.push_back(p[i] + d); q_nw.push_back(q[i]); q_nw.push_back(q[i] + d); } sort(p_nw.begin(), p_nw.end()); sort(q_nw.begin(), q_nw.end()); for (int mask = 0; mask < (1 << m); ++mask) { int mnx = d, mny = d; for (int i = 0; i < d; ++i) { int posx = lower_bound(p_nw.begin(), p_nw.end(), i) - p_nw.begin(); int rx = p_nw[posx + n - 1] + 1; for (int j = 0; j < m; ++j) { if (mask & (1 << j)) { if (i <= r[j]) { rx = max(rx, r[j] + 1); } else { rx = max(rx, r[j] + d + 1); } } } mnx = min(mnx, rx - i); int posy = lower_bound(q_nw.begin(), q_nw.end(), i) - q_nw.begin(); int ry = q_nw[posy + n - 1] + 1; for (int j = 0; j < m; ++j) { if (!(mask & (1 << j))) { if (i <= s[j]) { ry = max(ry, s[j] + 1); } else { ry = max(ry, s[j] + d + 1); } } } mny = min(mny, ry - i); } ans = min(ans, mnx * mny); } } else { vector<int> pp = p; sort(pp.begin(), pp.end()); vector<int> inds(m); iota(inds.begin(), inds.end(), 0); sort(inds.begin(), inds.end(), [&](int i, int j) { int ri = r[i] + 1, rj = r[j] + 1; return ri < rj; }); int pos = 0; for (int lx = 0; lx < d; ++lx) { int rx = pp[n - 1] + 1; int pos_nw = lower_bound(pp.begin(), pp.end(), lx) - pp.begin(); if (pos_nw != 0) { pos_nw--; rx = max(rx, pp[pos_nw] + d + 1); } vector<int> ll(d, -1), rr(d, d); set<int> used; vector<int> cnt(d); for (int i = 0; i < n; ++i) { cnt[q[i]]++; if (cnt[q[i]] == 1) { auto it = used.lower_bound(q[i]); if (it != used.end()) { ll[*it] = q[i]; rr[q[i]] = *it; } if (it != used.begin()) { it--; rr[*it] = q[i]; ll[q[i]] = *it; } used.insert(q[i]); } } for (int i = 0; i < m; ++i) { cnt[s[i]]++; if (cnt[s[i]] == 1) { auto it = used.lower_bound(s[i]); if (it != used.end()) { ll[*it] = s[i]; rr[s[i]] = *it; } if (it != used.begin()) { it--; rr[*it] = s[i]; ll[s[i]] = *it; } used.insert(s[i]); } } int len_pr = 0, len_suff = 0, mx_diff = 0; for (int i = 0; i < d; ++i) { if (cnt[i] > 0) { if (ll[i] == -1) { len_pr = max(len_pr, i); } else { mx_diff = max(mx_diff, i - ll[i] - 1); } if (rr[i] == d) { len_suff = max(len_suff, d - i - 1); } } } for (int i = pos; i < m; ++i) { ans = min(ans, (rx - lx) * (d - max(len_pr + len_suff, mx_diff))); if (lx <= r[inds[i]]) { rx = max(rx, r[inds[i]] + 1); } else { rx = max(rx, r[inds[i]] + d + 1); } cnt[s[inds[i]]]--; if (cnt[s[inds[i]]] == 0) { if (ll[s[inds[i]]] != -1) { rr[ll[s[inds[i]]]] = rr[s[inds[i]]]; if (rr[ll[s[inds[i]]]] == d) { len_suff = max(len_suff, d - ll[s[inds[i]]] - 1); } else { mx_diff = max(mx_diff, rr[ll[s[inds[i]]]] - ll[s[inds[i]]] - 1); } } if (rr[s[inds[i]]] != d) { ll[rr[s[inds[i]]]] = ll[s[inds[i]]]; if (ll[rr[s[inds[i]]]] == -1) { len_pr = max(len_pr, rr[s[inds[i]]]); } } } } for (int i = 0; i < pos; ++i) { ans = min(ans, (rx - lx) * (d - max(len_pr + len_suff, mx_diff))); if (lx <= r[inds[i]]) { rx = max(rx, r[inds[i]] + 1); } else { rx = max(rx, r[inds[i]] + d + 1); } cnt[s[inds[i]]]--; if (cnt[s[inds[i]]] == 0) { if (ll[s[inds[i]]] != -1) { rr[ll[s[inds[i]]]] = rr[s[inds[i]]]; if (rr[ll[s[inds[i]]]] == d) { len_suff = max(len_suff, d - ll[s[inds[i]]] - 1); } else { mx_diff = max(mx_diff, rr[ll[s[inds[i]]]] - ll[s[inds[i]]] - 1); } } if (rr[s[inds[i]]] != d) { ll[rr[s[inds[i]]]] = ll[s[inds[i]]]; if (ll[rr[s[inds[i]]]] == -1) { len_pr = max(len_pr, rr[s[inds[i]]]); } } } } ans = min(ans, (rx - lx) * (d - max(len_pr + len_suff, mx_diff))); while (pos < m && r[inds[pos]] == lx) { pos++; } } } cout << ans << '\n'; } signed main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int t = 1; // cin >> t; while (t--) { solve(); } return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...