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>
#define ll int
#define str string
#define ins insert
#define ld long double
#define pb push_back
#define pf push_front
#define pof pop_front()
#define pob pop_back()
#define lb lower_bound
#define ub upper_bound
#define endl "\n"
#define fr first
#define sc second
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define sz size()
#define vll vector<ll>
#define bc back()
#define arr array
#define pll vector<pair<ll,ll>>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set tree<ll, null_type,less_equal<ll>, rb_tree_tag,tree_order_statistics_node_update>
template<class S,class T>
bool chmin(S &a,const T &b) {
return a>b?(a=b)==b:false;
}
template<class S,class T>
bool chmax(S &a,const T &b) {
return a<b?(a=b)==b:false;
}
//void fre(string s){freopen((s+".in").c_str(),"r",stdin);freopen((s+".out").c_str(),"w",stdout);}
void start(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
const ll inf=1e9;
const ll mod=1e9+7;
const ll N=5e3+7;
const ld eps=1e-9;
vector<pll> vx(N);
vector<deque<ll>> y(N);
bool must[N];
ll lst[N];
ll l[N],r[N];
vll a,b,c,e;
ll n,m,d;
ll dif,ddif;
ll dis(ll l,ll r){
if(l==r)return d-1;
if(l>r)return r+d-l-1;
return r-l-1;
}
vll v;
void init(){
for(ll i=0;i<d;i++){
if(y[i].sz || must[i])v.pb(i);
}
v.pb(v[0]);
for(ll i=1;i<v.sz;i++){
chmax(ddif,dis(v[i-1],v[i]));
}
}
void reset(){
dif=ddif;
for(ll i=0;i<v.sz;i++){
if(i>0)l[v[i]]=v[i-1];
if(i<v.sz-1)r[v[i]]=v[i+1];
}
}
ll L,R;
void er(ll a){
L=l[a],R=r[a];
r[L]=R;
l[R]=L;
chmax(dif,dis(L,R));
}
void solve(){
ll i,j;
cin>>n>>m>>d;
vector<pair<ll,ll>> v(n);
deque<ll> need;
// inputa
a.resize(n+5);
b.resize(n+5);
c.resize(m+5);
e.resize(m+5);
for(i=1;i<=n;i++) cin>>v[i-1].fr>>v[i-1].sc;
sort(all(v));
for(i=1;i<=n;i++){a[i]=v[i-1].fr,b[i]=v[i-1].sc;}
v.resize(m);
for(i=0;i<m;i++)cin>>v[i].fr>>v[i].sc;
sort(all(v));
for(i=1;i<=m;i++){c[i]=v[i-1].fr,e[i]=v[i-1].sc;}
// assign stuff
for(i=1;i<=n;i++){
need.pb(a[i]);
must[b[i]]=1;
vx[a[i]].pb({b[i],1});
}
vll id;
for(i=1;i<=m;i++){
if(must[e[i]])continue;
y[e[i]].pb(c[i]);
vx[c[i]].pb({e[i],0});
}
for(i=0;i<d;i++) sort(all(y[i]));
set<pair<ll,ll>> x;
for(i=0;i<d;i++){
if(y[i].sz)x.ins({y[i].bc,i});
}
x.ins({need.bc,-1});
ll ans=d*d;
init();
for(ll l=0;l<d;l++){
reset();
auto r=x.begin();
bool f=0;
while(r!=x.end()){
if(r->sc<0) f=1;
else er(r->sc);
if(f){
//cout<<l<<' '<<r->fr<<endl;
//cout<<d<<' '<<dif<<endl<<endl;
chmin(ans,(d-dif)*(r->fr-l+1));
}
r++;
}
for(auto i : vx[l]){
if(i.sc==1){
x.erase(x.find({need.bc,-1}));
need.pb(need[0]+d);
need.pof;
x.ins({need.bc,-1});
}else{
x.erase(x.find({y[i.fr].bc,i.fr}));
y[i.fr].pb(y[i.fr][0]+d);
y[i.fr].pof;
x.ins({y[i.fr].bc,i.fr});
}
}
}
cout<<ans<<endl;
}
signed main(){
start();
ll t=1;
//cin>>t;
while(t--) solve();
return 0;
}
/*
3 1 5
2 2
3 2
2 2
3 2
10
*/
Compilation message (stderr)
garden.cpp: In function 'void init()':
garden.cpp:64:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
64 | for(ll i=1;i<v.sz;i++){
| ^
garden.cpp: In function 'void reset()':
garden.cpp:70:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
70 | for(ll i=0;i<v.sz;i++){
| ^
garden.cpp:72:13: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
72 | if(i<v.sz-1)r[v[i]]=v[i+1];
| ~^~~~~~~
garden.cpp: In function 'void solve()':
garden.cpp:83:10: warning: unused variable 'j' [-Wunused-variable]
83 | ll i,j;
| ^
# | 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... |