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 INF = 0x3f3f3f3f;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, m, d; cin >> n >> m >> d;
vector<int> p(n), q(n), r(m), s(m);
for (int i = 0; i < n; i++) cin >> p[i] >> q[i];
for (int i = 0; i < m; i++) cin >> r[i] >> s[i];
vector<bool> type_1(d, false);
for (int i = 0; i < n; i++) type_1[q[i]] = true;
vector<int> last(d, -1);
for (int i = 0; i < m; i++) if (!type_1[s[i]]) last[s[i]] = max(last[s[i]], r[i]);
vector< set<int> > type_2(d, set<int>());
for (int i = 0; i < d; i++) if (last[i] != -1) type_2[last[i]].insert(i);
vector< vector<int> > by_r(d);
for (int i = 0; i < m; i++) by_r[r[i]].push_back(s[i]);
vector<int> v, cnt(d, 0);
for (int i = 0; i < n; i++) v.push_back(q[i]), cnt[p[i]]++;
for (int i = 0; i < m; i++) v.push_back(s[i]);
sort(v.begin(), v.end());
v.erase(unique(v.begin(), v.end()), v.end());
int ans = INF, mx_b = -INF;
for (int i = 1; i < (int) v.size(); i++) mx_b = max(mx_b, v[i] - v[i - 1]);
if ((int) v.size() >= 2) mx_b = max(mx_b, v[0] - v.back() + d);
vector<int> smaller_1(d, -1), larger_1(d, -1);
for (int i = 0; i < (int) v.size(); i++) {
smaller_1[v[i]] = v[(i + 1) % (int) v.size()];
larger_1[v[((i - 1) + (int) v.size()) % (int) v.size()]] = v[i];
}
for (int l = 0; l < d; l++) {
int mx = mx_b, cnt_1 = 0, left = (int) v.size();
vector<int> larger = larger_1, smaller = smaller_1;
for (int r = l; r < l + d; r++) {
int tmp_r = r; r %= d;
cnt_1 += cnt[r];
for (auto &x : type_2[r]) {
mx = max(mx, (larger[x] - smaller[x] + d) % d);
smaller[larger[x]] = smaller[x];
larger[smaller[x]] = larger[x];
if (--left <= 1) mx = d;
}
r = tmp_r;
if (cnt_1 == n) ans = min(ans, (r - l + 1) * (d - mx + 1));
}
for (auto &x : by_r[l]) {
if (type_1[x]) continue;
type_2[last[x] % d].erase(x);
last[x] = l + d;
type_2[last[x] % d].insert(x);
}
}
cout << ans << '\n';
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... |