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 long long inf = (int) 1e9 + 10;
// #define int long long
#define dbl long double
#define endl '\n'
#define sc second
#define fr first
#define mp make_pair
#define pb push_back
#define all(x) x.begin(), x.end()
const int maxn = 1e4+10;
int n, m, d, hasp[maxn], hasq[maxn];
deque<int> vals[maxn];
vector<int> endhere[maxn];
int ds[maxn], dsz[maxn];
int find(int v) {
if(ds[v] == v) return v;
return ds[v] = find(ds[v]);
}
void join(int u, int v) {
u = find(u); v = find(v);
if(u == v) return;
if(dsz[u] < dsz[v]) swap(u,v);
ds[v] = u;
dsz[u]+= dsz[v];
}
void solve() {
cin >> n >> m >> d;
for(int i = 1; i <= n; i++) {
int x,y; cin >> x >> y;
hasp[x]++; hasp[x+d]++;
hasq[y]++; hasq[y+d]++;
}
for(int i = 1; i <= m; i++) {
int x,y; cin >> x >> y;
if(hasp[x]) continue;
vals[x].pb(y);
}
for(int x = 0; x < d; x++) sort(all(vals[x]));
int ans = inf;
for(int y0 = 0; y0 < d; y0++) {
set<int> pos;
for(int y = 0; y < 2*d; y++) endhere[y].clear();
int deltax = inf;
for(int x = 0; x < d; x++) {
ds[x] = x;
dsz[x] = 1;
while(vals[x].size() && vals[x].front() == y0-1) {
vals[x].pop_front();
vals[x].pb(y0-1+d);
}
if(hasp[x]) pos.insert(x);
else if(vals[x].size()) {
endhere[vals[x].back()].pb(x);
pos.insert(x);
}
else {
if(x != 0) join(find(x-1),find(x));
}
deltax = min(deltax,d+1-dsz[find(x)]);
}
int qtdq = 0;
for(auto it = pos.begin(); next(it) != pos.end(); it++) {
deltax = min(deltax,d+1-*next(it)+*it);
}
for(int y1 = y0; y1 < y0+d; y1++) {
qtdq+= hasq[y1];
for(auto x : endhere[y1]) {
if(x != 0) join(find(x-1),find(x));
deltax = min(deltax,d+1-dsz[find(x)]);
pos.erase(x);
auto it = pos.lower_bound(x);
if(it == pos.end() || it == pos.begin()) continue;
deltax = min(deltax,d+1+*prev(it)-*it);
}
if(qtdq != n) continue;
deltax = min(deltax,*pos.rbegin()-*pos.begin()+1);
if(find(0) != find(d-1)) deltax = min(deltax,d-dsz[find(d-1)]-dsz[find(0)]+2);
ans = min(ans,deltax*(y1-y0+1));
}
}
cout << ans << endl;
}
int32_t main() {
ios::sync_with_stdio(false); cin.tie(0);
// freopen("in.in", "r", stdin);
// freopen("out.out", "w", stdout);
int tt = 1;
// cin >> tt;
while(tt--) {
solve();
}
}
# | 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... |