# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
963595 | Trisanu_Das | Garden (JOI23_garden) | C++17 | 0 ms | 0 KiB |
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;
#define vt vector
#define size(x) (int((x).size()))
#define all(x) begin(x), end(x)
#define REP(a,b,c,d) for(int a=(b);(d)>0?a<=(c):a>=(c);a+=(d))
#define FOR(a,b,c) REP(a,b,c,1)
#define ROF(a,b,c) REP(a,b,c,-1)
struct DSU {
vector<int> uf;
DSU(int n):uf(vector<int>(n,-1)){}
int find(int i){
return uf[i]<0?i:uf[i]=find(uf[i]);
}
bool unite(int a,int b){
if((a=find(a))==(b=find(b)))
return false;
if(uf[a]>uf[b])
swap(a,b);
uf[a]+=uf[b];
uf[b]=a;
return false;
}
int sz(int i){
return -uf[find(i)];
}
};
signed main() {
#ifndef DEBUG
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
#endif
int N,M,D;
cin>>N>>M>>D;
vector<bool> x_need(D),y_need(D);
for(int i = 0; i < N; i++){
int x,y;
cin>>x>>y;
x_need[x]=y_need[y]=true;
}
vector<int> R(D,-1);
vector<vector<int>> change(D);
while(M--){
int x,y;
cin>>x>>y;
if(!y_need[y]){
R[y]=max(R[y],x);
change[x].push_back(y);
}
}
int ans=D*D;
for(int l = 0; l < D; l++){
vector<vector<int>> can_kill(D);
vector<bool> deleted(D);
DSU uf(D);
int gap=0;
auto insert=[&](int i){
deleted[i]=true;
for(int k:{-1,1}){
int j=(i+k+D)%D;
if(deleted[j]) uf.unite(i,j);
}
gap=max(gap,uf.sz(i));
};
for(int i = 0; i < D; i++){
if (R[i]>=0) can_kill[R[i]].push_back(i);
else if(!y_need[i]) insert(i);
}
int need=accumulate(all(x_need),0);
for(int d = 1; d <= D; d++){
int r=(l+d-1)%D;
need-=x_need[r];
for(int i:can_kill[r]) insert(i);
if(!need) ans=min(ans,d*(D-gap));
}
for(int i:change[l]) R[i]=l;
cout<<ans<<'\n';
}