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;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int MAXN = 5e5;
const int MAXD = 5000;
const ll INF = 1e18;
int N, M, D;
vector<int> B[MAXD+10];
int P[MAXD+10], Q[MAXD*2+10];
vector<pii> V[MAXD+10];
set<int> C[MAXD*2+10];
struct Data
{
int L[MAXD+10], R[MAXD+10], cnt=0, val=0;
vector<int> V;
void init()
{
for(int i=0; i<D; i++) L[i]=-1, R[i]=-1;
V.clear(); cnt=0; val=0;
}
void insert(int x)
{
L[x]=R[x]=x;
V.push_back(x);
cnt++;
}
void init2()
{
for(int i=1; i<D; i++) if(L[i]!=i) L[i]=L[i-1];
if(L[0]!=0) L[0]=L[D-1];
for(int i=1; i<D; i++) if(L[i]!=i) L[i]=L[i-1];
for(int i=D-2; i>=0; i--) if(R[i]!=i) R[i]=R[i+1];
if(R[D-1]!=D-1) R[D-1]=R[0];
for(int i=D-2; i>=0; i--) if(R[i]!=i) R[i]=R[i+1];
sort(V.begin(), V.end());
for(int i=0; i+1<V.size(); i++) val=max(val, V[i+1]-V[i]);
if(!V.empty()) val=max(val, V[0]+D-V.back());
}
int FindL(int x)
{
if(x==-1) return FindL(D-1);
if(x==L[x]) return x;
return L[x]=FindL(L[x]);
}
int FindR(int x)
{
if(x==D) return FindR(0);
if(x==R[x]) return x;
return R[x]=FindR(R[x]);
}
void Delete(int x)
{
cnt--;
assert(L[x]==x); assert(R[x]==x);
int p=FindL(x-1), q=FindR(x+1);
if(p<q) val=max(val, q-p);
else val=max(val, q+D-p);
L[x]=x-1; if(x==0) L[x]=D-1;
R[x]=x+1; if(x==D-1) R[x]=0;
}
int query()
{
if(cnt<=1) return 1;
else return D-val+1;
}
}DB;
ll ans=INF;
int main()
{
scanf("%d%d%d", &N, &M, &D);
for(int i=1; i<=N; i++)
{
int y, x;
scanf("%d%d", &x, &y);
P[x]=1; Q[y]=1; Q[y+D]=1;
}
for(int i=1; i<=M; i++)
{
int y, x;
scanf("%d%d", &x, &y);
if(P[x]) continue;
B[x].push_back(y);
}
for(int i=0; i<D; i++)
{
if(B[i].empty()) continue;
sort(B[i].begin(), B[i].end());
B[i].erase(unique(B[i].begin(), B[i].end()), B[i].end());
V[0].push_back({B[i].back(), i+1});
int t=B[i].back();
for(auto it : B[i])
{
V[it+1].push_back({t, -i});
V[it+1].push_back({it+D, i+1});
t=it+D;
}
}
for(int i=0; i<D; i++)
{
DB.init();
for(int j=0; j<D; j++) if(P[j]) DB.insert(j);
int val=i;
for(int j=D+i-1; j>=i; j--) if(Q[j]) { val=j; break; }
for(auto [y, x] : V[i])
{
if(x>0) C[y].insert(x-1);
else C[y].erase(-x);
}
for(int j=val; j<D+i; j++) for(auto it : C[j]) DB.insert(it);
DB.init2();
for(int j=val; j<D+i; j++)
{
for(auto it : C[j]) DB.Delete(it);
ans=min(ans, (ll)DB.query()*(j-i+1));
}
}
printf("%lld\n", ans);
}
Compilation message (stderr)
garden.cpp: In member function 'void Data::init2()':
garden.cpp:47:25: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
47 | for(int i=0; i+1<V.size(); i++) val=max(val, V[i+1]-V[i]);
| ~~~^~~~~~~~~
garden.cpp: In function 'int main()':
garden.cpp:86:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
86 | scanf("%d%d%d", &N, &M, &D);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~
garden.cpp:90:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
90 | scanf("%d%d", &x, &y);
| ~~~~~^~~~~~~~~~~~~~~~
garden.cpp:96:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
96 | scanf("%d%d", &x, &y);
| ~~~~~^~~~~~~~~~~~~~~~
# | 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... |