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;
struct DSU{
vector<int> v, next, prev;
vector<bool> zero;
int mx;
void init(int n){
v.assign(n, -1); mx = 0;
zero.assign(n, false);
next.resize(n); prev.resize(n);
for(int i = 0; i < n; i++){
next[i] = (i+1)%n;
prev[i] = (i+n-1)%n;
}
}
int get(int x){return v[x] < 0 ? x : v[x] = get(v[x]);}
void unite(int x, int y){
x = get(x); y = get(y);
if(x == y) return;
if(v[x] > v[y]) swap(x, y);
v[x] += v[y]; v[y] = x; mx = max(mx, -v[x]);
}
void setZero(int i){
zero[i] = true;
mx = max(mx, 1);
if(zero[prev[i]]) unite(i, prev[i]);
if(zero[next[i]]) unite(i, next[i]);
}
};
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
int N, M, D; cin >> N >> M >> D;
vector<pair<int, int>> A(N);
for(int i = 0; i < N; i++) cin >> A[i].first >> A[i].second;
vector<pair<int, int>> B(M);
for(int i = 0; i < M; i++) cin >> B[i].first >> B[i].second;
vector<set<int>> cols(D);
vector<int> mx(D, -1);
vector<int> rows(2*D, 0);
vector<vector<int>> updateRow(2*D);
for(int i = 0; i < N; i++){
cols[A[i].first].insert(1e9);
rows[A[i].second]++;
rows[A[i].second+D]++;
}
for(int i = 0; i < M; i++){
cols[B[i].first].insert(B[i].second);
updateRow[B[i].second].push_back(B[i].first);
}
int ans = D * D;
DSU initial; initial.init(D);
vector<set<int>> hasMx(2*D);
for(int i = 0; i < D; i++){
if(cols[i].empty()) initial.setZero(i);
else{
mx[i] = *cols[i].rbegin();
if(mx[i] != 1e9) hasMx[mx[i]].insert(i);
}
}
for(int startRow = 0; startRow < D; startRow++){
int lft = N;
DSU UF = initial;
for(int r = startRow; r < startRow + D; r++){
lft -= rows[r];
for(int col : hasMx[r]){
UF.setZero(col);
}
if(lft > 0) continue;
ans = min(ans, (r - startRow + 1) * (D - UF.mx));
}
for(int col : updateRow[startRow]){
if(mx[col] != 1e9) hasMx[mx[col]].erase(col);
cols[col].erase(startRow);
cols[col].insert(startRow+D);
mx[col] = *cols[col].rbegin();
if(mx[col] != 1e9) hasMx[mx[col]].insert(col);
}
}
cout << ans << "\n";
}
# | 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... |