#include<bits/stdc++.h>
using namespace std;
const int N=3e5+5;
const int K=1<<21;
const int INF=INT_MAX/2;
int n;
int a[3*N],b[N],c[N];
int pos[3*N];
int l[3*N],r[3*N];
bool d1[3*N],d2[3*N];
struct Info{
int sum,pre,suf,mx;
friend Info operator+(const Info &l,const Info &r){
return {l.sum+r.sum,max(l.pre,l.sum+r.pre),max(r.suf,r.sum+l.suf),max({l.mx,r.mx,l.suf+r.pre})};
}
};
struct SegTree{
Info t[K];
void build(int l,int r,int i){
t[i]={0,-INF,-INF,-INF};
if(l==r)return;
int m=(l+r)/2;
build(l,m,i*2);
build(m+1,r,i*2+1);
}
void build(){
build(1,2*n,1);
}
void modify(int l,int r,int i,int x,Info v){
if(l==r)return void(t[i]=v);
int m=(l+r)/2;
if(x<=m)modify(l,m,i*2,x,v);
else modify(m+1,r,i*2+1,x,v);
t[i]=t[i*2]+t[i*2+1];
}
void modify(int x,Info v){
modify(1,2*n,1,x,v);
}
bool query(){
return t[1].mx<=0;
}
}seg;
bool check(int k){
for(int i=1;i<=2*n;i++){
l[i]=lower_bound(b+1,b+n+1,a[i]-k)-b-1;
r[i]=upper_bound(b+1,b+n+1,a[i]+k)-b-1;
}
for(int i=2*n+1;i<=3*n;i++){
l[i]=l[i-2*n];
r[i]=r[i-2*n];
}
seg.build();
for(int i=1;i<=n;i++){
seg.modify(pos[i],{1,1-r[i],1+l[i],1+l[i]-r[i]});
}
for(int i=n+1;i<=3*n;i++){
seg.modify(pos[i-n],{0,-INF,-INF,-INF});
seg.modify(pos[i],{1,1-r[i],1+l[i],1+l[i]-r[i]});
d1[i]=seg.query();
}
for(int i=1;i<=2*n;i++){
l[i]=lower_bound(c+1,c+n+1,a[i]-k)-c-1;
r[i]=upper_bound(c+1,c+n+1,a[i]+k)-c-1;
}
for(int i=2*n+1;i<=3*n;i++){
l[i]=l[i-2*n];
r[i]=r[i-2*n];
}
seg.build();
for(int i=1;i<=n;i++){
seg.modify(pos[i],{1,1-r[i],1+l[i],1+l[i]-r[i]});
}
for(int i=n+1;i<=3*n;i++){
seg.modify(pos[i-n],{0,-INF,-INF,-INF});
seg.modify(pos[i],{1,1-r[i],1+l[i],1+l[i]-r[i]});
d2[i]=seg.query();
}
for(int i=n+1;i<=2*n;i++){
if((d1[i]&&d2[i+n])||(d2[i]&&d1[i+n])){
return true;
}
}
return false;
}
int main(){
cin >> n;
vector<pair<int,int>> vals;
for(int i=1;i<=2*n;i++){
cin >> a[i];
vals.emplace_back(a[i],i);
}
sort(vals.begin(),vals.end());
for(int i=0;i<2*n;i++){
pos[vals[i].second]=i+1;
}
for(int i=2*n+1;i<=3*n;i++){
a[i]=a[i-2*n];
pos[i]=pos[i-2*n];
}
for(int i=1;i<=n;i++){
cin >> b[i];
}
for(int i=1;i<=n;i++){
cin >> c[i];
}
sort(b+1,b+n+1);
sort(c+1,c+n+1);
int l=0,r=1'000'000'000;
while(l<r){
int m=(l+r)/2;
if(check(m))r=m;
else l=m+1;
}
cout << l << "\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
14672 KB |
Output is correct |
2 |
Correct |
2 ms |
14672 KB |
Output is correct |
3 |
Correct |
3 ms |
14796 KB |
Output is correct |
4 |
Correct |
3 ms |
14672 KB |
Output is correct |
5 |
Correct |
5 ms |
14672 KB |
Output is correct |
6 |
Correct |
2 ms |
14928 KB |
Output is correct |
7 |
Correct |
3 ms |
14672 KB |
Output is correct |
8 |
Correct |
2 ms |
14672 KB |
Output is correct |
9 |
Correct |
3 ms |
14672 KB |
Output is correct |
10 |
Correct |
2 ms |
14780 KB |
Output is correct |
11 |
Correct |
2 ms |
14672 KB |
Output is correct |
12 |
Correct |
2 ms |
14672 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
14672 KB |
Output is correct |
2 |
Correct |
2 ms |
14672 KB |
Output is correct |
3 |
Correct |
3 ms |
14796 KB |
Output is correct |
4 |
Correct |
3 ms |
14672 KB |
Output is correct |
5 |
Correct |
5 ms |
14672 KB |
Output is correct |
6 |
Correct |
2 ms |
14928 KB |
Output is correct |
7 |
Correct |
3 ms |
14672 KB |
Output is correct |
8 |
Correct |
2 ms |
14672 KB |
Output is correct |
9 |
Correct |
3 ms |
14672 KB |
Output is correct |
10 |
Correct |
2 ms |
14780 KB |
Output is correct |
11 |
Correct |
2 ms |
14672 KB |
Output is correct |
12 |
Correct |
2 ms |
14672 KB |
Output is correct |
13 |
Correct |
2 ms |
14672 KB |
Output is correct |
14 |
Correct |
2 ms |
14672 KB |
Output is correct |
15 |
Correct |
3 ms |
14672 KB |
Output is correct |
16 |
Correct |
2 ms |
14672 KB |
Output is correct |
17 |
Correct |
3 ms |
14672 KB |
Output is correct |
18 |
Correct |
2 ms |
14672 KB |
Output is correct |
19 |
Correct |
2 ms |
14784 KB |
Output is correct |
20 |
Correct |
3 ms |
14672 KB |
Output is correct |
21 |
Correct |
3 ms |
14672 KB |
Output is correct |
22 |
Correct |
2 ms |
14672 KB |
Output is correct |
23 |
Correct |
3 ms |
14672 KB |
Output is correct |
24 |
Correct |
3 ms |
14672 KB |
Output is correct |
25 |
Correct |
3 ms |
14672 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
14672 KB |
Output is correct |
2 |
Correct |
2 ms |
14672 KB |
Output is correct |
3 |
Correct |
3 ms |
14796 KB |
Output is correct |
4 |
Correct |
3 ms |
14672 KB |
Output is correct |
5 |
Correct |
5 ms |
14672 KB |
Output is correct |
6 |
Correct |
2 ms |
14928 KB |
Output is correct |
7 |
Correct |
3 ms |
14672 KB |
Output is correct |
8 |
Correct |
2 ms |
14672 KB |
Output is correct |
9 |
Correct |
3 ms |
14672 KB |
Output is correct |
10 |
Correct |
2 ms |
14780 KB |
Output is correct |
11 |
Correct |
2 ms |
14672 KB |
Output is correct |
12 |
Correct |
2 ms |
14672 KB |
Output is correct |
13 |
Correct |
2 ms |
14672 KB |
Output is correct |
14 |
Correct |
2 ms |
14672 KB |
Output is correct |
15 |
Correct |
3 ms |
14672 KB |
Output is correct |
16 |
Correct |
2 ms |
14672 KB |
Output is correct |
17 |
Correct |
3 ms |
14672 KB |
Output is correct |
18 |
Correct |
2 ms |
14672 KB |
Output is correct |
19 |
Correct |
2 ms |
14784 KB |
Output is correct |
20 |
Correct |
3 ms |
14672 KB |
Output is correct |
21 |
Correct |
3 ms |
14672 KB |
Output is correct |
22 |
Correct |
2 ms |
14672 KB |
Output is correct |
23 |
Correct |
3 ms |
14672 KB |
Output is correct |
24 |
Correct |
3 ms |
14672 KB |
Output is correct |
25 |
Correct |
3 ms |
14672 KB |
Output is correct |
26 |
Correct |
67 ms |
15068 KB |
Output is correct |
27 |
Correct |
70 ms |
14928 KB |
Output is correct |
28 |
Correct |
63 ms |
14928 KB |
Output is correct |
29 |
Correct |
4 ms |
14672 KB |
Output is correct |
30 |
Correct |
74 ms |
14928 KB |
Output is correct |
31 |
Correct |
75 ms |
14928 KB |
Output is correct |
32 |
Correct |
35 ms |
14672 KB |
Output is correct |
33 |
Correct |
16 ms |
14864 KB |
Output is correct |
34 |
Correct |
67 ms |
14928 KB |
Output is correct |
35 |
Correct |
65 ms |
15096 KB |
Output is correct |
36 |
Correct |
92 ms |
14928 KB |
Output is correct |
37 |
Correct |
59 ms |
14928 KB |
Output is correct |
38 |
Correct |
63 ms |
14928 KB |
Output is correct |
39 |
Correct |
67 ms |
15076 KB |
Output is correct |
40 |
Correct |
66 ms |
14928 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
5044 ms |
69304 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
14672 KB |
Output is correct |
2 |
Correct |
2 ms |
14672 KB |
Output is correct |
3 |
Correct |
3 ms |
14796 KB |
Output is correct |
4 |
Correct |
3 ms |
14672 KB |
Output is correct |
5 |
Correct |
5 ms |
14672 KB |
Output is correct |
6 |
Correct |
2 ms |
14928 KB |
Output is correct |
7 |
Correct |
3 ms |
14672 KB |
Output is correct |
8 |
Correct |
2 ms |
14672 KB |
Output is correct |
9 |
Correct |
3 ms |
14672 KB |
Output is correct |
10 |
Correct |
2 ms |
14780 KB |
Output is correct |
11 |
Correct |
2 ms |
14672 KB |
Output is correct |
12 |
Correct |
2 ms |
14672 KB |
Output is correct |
13 |
Correct |
2 ms |
14672 KB |
Output is correct |
14 |
Correct |
2 ms |
14672 KB |
Output is correct |
15 |
Correct |
3 ms |
14672 KB |
Output is correct |
16 |
Correct |
2 ms |
14672 KB |
Output is correct |
17 |
Correct |
3 ms |
14672 KB |
Output is correct |
18 |
Correct |
2 ms |
14672 KB |
Output is correct |
19 |
Correct |
2 ms |
14784 KB |
Output is correct |
20 |
Correct |
3 ms |
14672 KB |
Output is correct |
21 |
Correct |
3 ms |
14672 KB |
Output is correct |
22 |
Correct |
2 ms |
14672 KB |
Output is correct |
23 |
Correct |
3 ms |
14672 KB |
Output is correct |
24 |
Correct |
3 ms |
14672 KB |
Output is correct |
25 |
Correct |
3 ms |
14672 KB |
Output is correct |
26 |
Correct |
67 ms |
15068 KB |
Output is correct |
27 |
Correct |
70 ms |
14928 KB |
Output is correct |
28 |
Correct |
63 ms |
14928 KB |
Output is correct |
29 |
Correct |
4 ms |
14672 KB |
Output is correct |
30 |
Correct |
74 ms |
14928 KB |
Output is correct |
31 |
Correct |
75 ms |
14928 KB |
Output is correct |
32 |
Correct |
35 ms |
14672 KB |
Output is correct |
33 |
Correct |
16 ms |
14864 KB |
Output is correct |
34 |
Correct |
67 ms |
14928 KB |
Output is correct |
35 |
Correct |
65 ms |
15096 KB |
Output is correct |
36 |
Correct |
92 ms |
14928 KB |
Output is correct |
37 |
Correct |
59 ms |
14928 KB |
Output is correct |
38 |
Correct |
63 ms |
14928 KB |
Output is correct |
39 |
Correct |
67 ms |
15076 KB |
Output is correct |
40 |
Correct |
66 ms |
14928 KB |
Output is correct |
41 |
Execution timed out |
5044 ms |
69304 KB |
Time limit exceeded |
42 |
Halted |
0 ms |
0 KB |
- |