This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
const int N = 500500;
const int D = 5005;
int n, m, d, p[N], q[N], x[N], y[N], cx[D], cy[D], sy[D], px[D], py[D], a[D][D];
vector<int> stupid[D];
queue<int> que[D];
int ans = 1e9, mn = 1e9;
int main() {
ios::sync_with_stdio(0); cin.tie(0);
cin >> n >> m >> d;
for (int i = 1; i <= n; i++) {
cin >> p[i] >> q[i];
cx[p[i]]++; cy[q[i]]++;
}
for (int i = 0; i < d; i++) {
int totx = 0, toty = 0;
for (int j = i; j < i + d; j++) {
totx += cx[j % d];
if (totx == n) {
px[i] = j - i + 1;
break;
}
}
for (int j = i; j < i + d; j++) {
toty += cy[j % d];
if (toty == n) {
py[i] = j - i + 1;
mn = min(mn, py[i]);
break;
}
}
}
// for (int i = 0; i < d; i++) cout << py[i] << ' ';
// cout << '\n';
for (int i = 1; i <= m; i++) {
cin >> x[i] >> y[i]; // long
sy[y[i]]++;
if (x[i] < d - 1) x[i] += d;
stupid[y[i]].push_back(x[i]);
}
for (int i = 0; i < d; i++) {
sort(stupid[i].begin(), stupid[i].end());
stupid[i].erase(unique(stupid[i].begin(), stupid[i].end()), stupid[i].end());
reverse(stupid[i].begin(), stupid[i].end());
for (auto& stu : stupid[i]) {
que[i].push(stu);
}
// cout << '\n';
}
for (int i = 0; i < d; i++) for (int j = 0; j < d; j++) a[i][j] = d;
for (int i = 0; i < d; i++) {
int tim = d, j = i;
while (tim--) {
j++;
if (j >= d) j -= d;
int di = i + d - j + 1;
if (i >= j) di = i - j + 1;
a[j][i] = min(a[((j - 1) % d + d) % d][i], max(py[j], di));
if (j == i) {
a[i][i] = d;
for (int k = 0; k < d; k++) {
di = i + d - k + 1;
if (i >= k) di = i - k + 1;
// cout << py[k] << ' ' << di << ' ' << k << ' ' << i << ' ' << '\n';
a[i][i] = min(a[i][i], max(py[k], di));
}
}
// cout << j << ' ' << i << ' ' << a[j][i] << '\n';
}
}
for (int i = d - 1; i >= 0; i--) {
vector<vector<int>> cnt(d * 2);
vector<int> ll(d, 0), rr(d, 0), hv;
int len = d, cc = 0;
for (int j = 0; j < d; j++) if (!que[j].empty() && que[j].front() >= 0) {
cnt[que[j].front()].push_back(j);
// cout << "cnt: " << que[j].front() << ' ' << j << '\n';
}
// for (int j = 0; j < (d << 1); j++) {
// cout << "pt " << j << '\n';
// for (auto& xx : cnt[j]) cout << xx << ' ';
// cout << '\n';
// }
for (int j = 0; j < d; j++) if (sy[j]) cc++, hv.push_back(j);
for (int j = 0; j < (int) hv.size(); j++) {
int k = (j + 1) % (int) hv.size();
rr[hv[j]] = hv[k]; ll[hv[k]] = hv[j];
// if (hv[k] == hv[j]) continue;
len = min(len, a[hv[k]][hv[j]]);
}
// cout << len << '\n';
int lb = i + px[i] - 1, deled = 0;
// cout << "new\n";
for (int j = i; j <= i + d - 1; j++) {
// cout << "testing: " << i << ' ' << j << '\n';
for (auto& ys : cnt[j]) {
int L = ll[ys], R = rr[ys];
ll[R] = L; rr[L] = R;
// cout << R << ' ' << L << '\n';
len = min(len, a[R][L]);
deled++;
if (deled >= cc) len = mn;
// cout << "del: " << ys << '\n';
// cout << "len: " << len << '\n';
// ans = min(ans, (max(lb, j) - i + 1) * len);
// if (j >= lb) cout << i << ' ' << j << ' ' << max(lb, j) - i + 1 << ' ' << len << '\n';
}
ans = min(ans, (max(lb, j) - i + 1) * len);
// cout << len << '\n';
// cout << ans << '\n';
}
for (int j = 0; j < d; j++) {
while (!que[j].empty() && que[j].front() >= i + d - 1) {
int t = que[j].front();
que[j].pop();
que[j].push(t - d);
}
}
}
cout << ans << '\n';
}
/*
4 1 5
1 4
4 1
1 1
4 4
0 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... |