Submission #828855

#TimeUsernameProblemLanguageResultExecution timeMemory
828855LoboGarden (JOI23_garden)C++17
100 / 100
1368 ms16896 KiB
#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++) {
        for(int y = 0; y < 2*d; y++) endhere[y].clear();
        int deltax = inf;
        bool atv0 = 1;
        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] == 0 && vals[x].size() == 0){
                if(x != 0) join(find(x-1),find(x));
                else atv0 = 0;
            }

            if(hasp[x] == 0 && vals[x].size()) {
                endhere[vals[x].back()].pb(x);
            }
            deltax = min(deltax,d+1-dsz[find(x)]);
        }

        int qtdq = 0;
        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));
                else atv0 = 0;
                deltax = min(deltax,d+1-dsz[find(x)]);
            }
            if(qtdq != n) continue;

            if(atv0 == 0) deltax = min(deltax,d-(dsz[find(d-1)]-1)-dsz[find(0)]);
            else deltax = min(deltax,d-(dsz[find(d-1)]-1));
            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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...