이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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;
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;
});
vector<int> ll_val(d, -1), rr_val(d, d);
set<int> used;
vector<int> cnt_val(d);
for (int i = 0; i < n; ++i) {
cnt_val[q[i]]++;
if (cnt_val[q[i]] == 1) {
auto it = used.lower_bound(q[i]);
if (it != used.end()) {
ll_val[*it] = q[i];
rr_val[q[i]] = *it;
}
if (it != used.begin()) {
it--;
rr_val[*it] = q[i];
ll_val[q[i]] = *it;
}
used.insert(q[i]);
}
}
for (int i = 0; i < m; ++i) {
cnt_val[s[i]]++;
if (cnt_val[s[i]] == 1) {
auto it = used.lower_bound(s[i]);
if (it != used.end()) {
ll_val[*it] = s[i];
rr_val[s[i]] = *it;
}
if (it != used.begin()) {
it--;
rr_val[*it] = s[i];
ll_val[s[i]] = *it;
}
used.insert(s[i]);
}
}
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);
}
auto ll = ll_val, rr = rr_val, cnt = cnt_val;
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);
}
}
}
vector<int> cnt_nw(d);
vector<int> inds_nw;
for (int i = pos; i < m; ++i) {
cnt_nw[s[inds[i]]]++;
if (cnt_nw[s[inds[i]]] == cnt[s[inds[i]]]) {
inds_nw.push_back(i);
}
}
for (int i = 0; i < pos; ++i) {
cnt_nw[s[inds[i]]]++;
if (cnt_nw[s[inds[i]]] == cnt[s[inds[i]]]) {
inds_nw.push_back(i);
}
}
for (auto i : inds_nw) {
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);
}
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 time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |