#include<bits/stdc++.h>
#define pb push_back
#define fi first
#define se second
#define mp make_pair
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef long double ld;
template <typename T> bool chkmin(T &x,T y){return x>y?x=y,1:0;}
template <typename T> bool chkmax(T &x,T y){return x<y?x=y,1:0;}
ll readint(){
ll x=0,f=1; char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
int n,tot;
int a[600005],b[600005],c[600005],s[2200005][2],t[2200005][2],d[2200005][4],xs[2200005],id[600005];
int sum[24000000][4],tag[24000000][2],val[24000000][2];
void pushdown(int id){
for(int x=0;x<2;x++){
if(tag[id][x]==0) continue;
sum[id<<1][x*2]+=tag[id][x];
sum[id<<1][x*2+1]+=tag[id][x];
sum[id<<1|1][x*2]+=tag[id][x];
sum[id<<1|1][x*2+1]+=tag[id][x];
tag[id<<1][x]+=tag[id][x];
tag[id<<1|1][x]+=tag[id][x];
tag[id][x]=0;
}
}
void build(int id,int l,int r){
tag[id][0]=0;
tag[id][1]=0;
if(l==r){
val[id][0]=val[id][1]=0;
sum[id][0]=s[l][0];
sum[id][1]=t[l][0];
sum[id][2]=s[l][1];
sum[id][3]=t[l][1];
return;
}
int mid=(l+r)/2;
build(id<<1,l,mid);
build(id<<1|1,mid+1,r);
for(int x=0;x<2;x++){
sum[id][x*2]=max(sum[id<<1][x*2],sum[id<<1|1][x*2]);
sum[id][x*2+1]=min(sum[id<<1][x*2+1],sum[id<<1|1][x*2+1]);
val[id][x]=max({val[id<<1][x],val[id<<1|1][x],sum[id<<1|1][x*2]-sum[id<<1][x*2+1]});
}
}
void change(int id,int l,int r,int ql,int qr,int v){
if(ql<=l&&r<=qr){
sum[id][0]+=v;
sum[id][1]+=v;
sum[id][2]-=v;
sum[id][3]-=v;
tag[id][0]+=v;
tag[id][1]-=v;
if(l!=r) pushdown(id);
return;
}
pushdown(id);
int mid=(l+r)/2;
if(qr<=mid) change(id<<1,l,mid,ql,qr,v);
else if(ql>mid) change(id<<1|1,mid+1,r,ql,qr,v);
else change(id<<1,l,mid,ql,qr,v),change(id<<1|1,mid+1,r,ql,qr,v);
for(int t=0;t<2;t++){
sum[id][t*2]=max(sum[id<<1][t*2],sum[id<<1|1][t*2]);
sum[id][t*2+1]=min(sum[id<<1][t*2+1],sum[id<<1|1][t*2+1]);
val[id][t]=max({val[id<<1][t],val[id<<1|1][t],sum[id<<1|1][t*2]-sum[id<<1][t*2+1]});
}
}
int lwb(int x){
int l=0,r=tot-1,p=tot;
while(l<=r){
int mid=(l+r)/2;
if(xs[mid]>=x) p=mid,r=mid-1;
else l=mid+1;
}
return p;
}
bool check(int x){
{
int ptr=0;
for(int i=1;i<=n;i++){
while(ptr+1<tot&&xs[ptr+1]<b[i]-x) ptr++;
int idx=(xs[ptr]>=b[i]-x?ptr:ptr+1);
if(idx<tot) d[idx][0]-=1;
}
}
{
int ptr=0;
for(int i=1;i<=n;i++){
while(ptr+1<tot&&xs[ptr+1]<b[i]+x) ptr++;
int idx=(xs[ptr]>=b[i]+x?ptr:ptr+1);
if(idx<tot) d[idx][1]-=1;
}
}
{
int ptr=0;
for(int i=1;i<=n;i++){
while(ptr+1<tot&&xs[ptr+1]<c[i]-x) ptr++;
int idx=(xs[ptr]>=c[i]-x?ptr:ptr+1);
if(idx<tot) d[idx][2]-=1;
}
}
{
int ptr=0;
for(int i=1;i<=n;i++){
while(ptr+1<tot&&xs[ptr+1]<c[i]+x) ptr++;
int idx=(xs[ptr]>=c[i]+x?ptr:ptr+1);
if(idx<tot) d[idx][3]-=1;
}
}
for(int i=1;i<=n;i++){
d[id[i]][0]+=1;
d[id[i]][1]+=1;
d[id[i+n]][2]+=1;
d[id[i+n]][3]+=1;
}
for(int i=1;i<tot;i++){
s[i][0]=s[i-1][0]+d[i][0];
s[i][1]=s[i-1][1]+d[i][2];
t[i][0]=t[i-1][0]+d[i][1];
t[i][1]=t[i-1][1]+d[i][3];
d[i][0]=d[i][1]=d[i][2]=d[i][3]=0;
}
build(1,0,tot-1);
if(val[1][0]<=0&&val[1][1]<=0) return true;
int fx=val[1][0],fy=val[1][1];
for(int i=1;i<=n;i++){
int p=id[i],q=id[i+n],sgn=-1;
if(p>q) swap(p,q),sgn=1;
if(p<q) change(1,0,tot-1,p,q-1,sgn);
if(val[1][0]<=0&&val[1][1]<=0) return true;
if(val[1][0]>n-i||val[1][1]>n-i) return false;
//if(val[1][0]>fx||val[1][1]>fy) return false;
if(val[1][0]==fx&&val[1][1]==fy) return false;
fx=val[1][0]; fy=val[1][1];
}
return false;
}
int solve(int f){
int l=0,r=f,res=1e9;
while(l<=r){
int mid=(l+r)/2;
if(check(mid)) res=mid,r=mid-1;
else l=mid+1;
}
return res;
}
int main(){
n=readint();
for(int i=1;i<=2*n;i++) a[i]=readint();
for(int i=1;i<=n;i++) b[i]=readint();
for(int i=1;i<=n;i++) c[i]=readint();
tot=0;
xs[tot++]=-1e9-5;
for(int i=1;i<=2*n;i++){
xs[tot++]=a[i];
xs[tot++]=a[i]-1;
}
sort(xs,xs+tot);
tot=unique(xs,xs+tot)-xs;
for(int i=1;i<=2*n;i++) id[i]=lwb(a[i]);
sort(b+1,b+n+1);
sort(c+1,c+n+1);
int ans=solve((int)1e9);
for(int i=1;i<=n;i++) swap(b[i],c[i]);
ans=min(ans,solve(ans));
printf("%d\n",ans);
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
16732 KB |
Output is correct |
2 |
Correct |
2 ms |
16832 KB |
Output is correct |
3 |
Correct |
2 ms |
16732 KB |
Output is correct |
4 |
Correct |
2 ms |
16876 KB |
Output is correct |
5 |
Correct |
2 ms |
16732 KB |
Output is correct |
6 |
Correct |
3 ms |
16732 KB |
Output is correct |
7 |
Correct |
2 ms |
16732 KB |
Output is correct |
8 |
Correct |
2 ms |
16732 KB |
Output is correct |
9 |
Correct |
2 ms |
16832 KB |
Output is correct |
10 |
Correct |
2 ms |
16732 KB |
Output is correct |
11 |
Correct |
2 ms |
16732 KB |
Output is correct |
12 |
Correct |
2 ms |
16732 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
16732 KB |
Output is correct |
2 |
Correct |
2 ms |
16832 KB |
Output is correct |
3 |
Correct |
2 ms |
16732 KB |
Output is correct |
4 |
Correct |
2 ms |
16876 KB |
Output is correct |
5 |
Correct |
2 ms |
16732 KB |
Output is correct |
6 |
Correct |
3 ms |
16732 KB |
Output is correct |
7 |
Correct |
2 ms |
16732 KB |
Output is correct |
8 |
Correct |
2 ms |
16732 KB |
Output is correct |
9 |
Correct |
2 ms |
16832 KB |
Output is correct |
10 |
Correct |
2 ms |
16732 KB |
Output is correct |
11 |
Correct |
2 ms |
16732 KB |
Output is correct |
12 |
Correct |
2 ms |
16732 KB |
Output is correct |
13 |
Correct |
2 ms |
16732 KB |
Output is correct |
14 |
Correct |
2 ms |
16732 KB |
Output is correct |
15 |
Correct |
2 ms |
16872 KB |
Output is correct |
16 |
Correct |
2 ms |
16732 KB |
Output is correct |
17 |
Correct |
3 ms |
16992 KB |
Output is correct |
18 |
Correct |
2 ms |
16732 KB |
Output is correct |
19 |
Correct |
2 ms |
16732 KB |
Output is correct |
20 |
Correct |
3 ms |
16732 KB |
Output is correct |
21 |
Correct |
2 ms |
16732 KB |
Output is correct |
22 |
Incorrect |
2 ms |
16840 KB |
Output isn't correct |
23 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
16732 KB |
Output is correct |
2 |
Correct |
2 ms |
16832 KB |
Output is correct |
3 |
Correct |
2 ms |
16732 KB |
Output is correct |
4 |
Correct |
2 ms |
16876 KB |
Output is correct |
5 |
Correct |
2 ms |
16732 KB |
Output is correct |
6 |
Correct |
3 ms |
16732 KB |
Output is correct |
7 |
Correct |
2 ms |
16732 KB |
Output is correct |
8 |
Correct |
2 ms |
16732 KB |
Output is correct |
9 |
Correct |
2 ms |
16832 KB |
Output is correct |
10 |
Correct |
2 ms |
16732 KB |
Output is correct |
11 |
Correct |
2 ms |
16732 KB |
Output is correct |
12 |
Correct |
2 ms |
16732 KB |
Output is correct |
13 |
Correct |
2 ms |
16732 KB |
Output is correct |
14 |
Correct |
2 ms |
16732 KB |
Output is correct |
15 |
Correct |
2 ms |
16872 KB |
Output is correct |
16 |
Correct |
2 ms |
16732 KB |
Output is correct |
17 |
Correct |
3 ms |
16992 KB |
Output is correct |
18 |
Correct |
2 ms |
16732 KB |
Output is correct |
19 |
Correct |
2 ms |
16732 KB |
Output is correct |
20 |
Correct |
3 ms |
16732 KB |
Output is correct |
21 |
Correct |
2 ms |
16732 KB |
Output is correct |
22 |
Incorrect |
2 ms |
16840 KB |
Output isn't correct |
23 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
5007 ms |
204188 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
16732 KB |
Output is correct |
2 |
Correct |
2 ms |
16832 KB |
Output is correct |
3 |
Correct |
2 ms |
16732 KB |
Output is correct |
4 |
Correct |
2 ms |
16876 KB |
Output is correct |
5 |
Correct |
2 ms |
16732 KB |
Output is correct |
6 |
Correct |
3 ms |
16732 KB |
Output is correct |
7 |
Correct |
2 ms |
16732 KB |
Output is correct |
8 |
Correct |
2 ms |
16732 KB |
Output is correct |
9 |
Correct |
2 ms |
16832 KB |
Output is correct |
10 |
Correct |
2 ms |
16732 KB |
Output is correct |
11 |
Correct |
2 ms |
16732 KB |
Output is correct |
12 |
Correct |
2 ms |
16732 KB |
Output is correct |
13 |
Correct |
2 ms |
16732 KB |
Output is correct |
14 |
Correct |
2 ms |
16732 KB |
Output is correct |
15 |
Correct |
2 ms |
16872 KB |
Output is correct |
16 |
Correct |
2 ms |
16732 KB |
Output is correct |
17 |
Correct |
3 ms |
16992 KB |
Output is correct |
18 |
Correct |
2 ms |
16732 KB |
Output is correct |
19 |
Correct |
2 ms |
16732 KB |
Output is correct |
20 |
Correct |
3 ms |
16732 KB |
Output is correct |
21 |
Correct |
2 ms |
16732 KB |
Output is correct |
22 |
Incorrect |
2 ms |
16840 KB |
Output isn't correct |
23 |
Halted |
0 ms |
0 KB |
- |