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 "shortcut.h"
#include <bits/stdc++.h>
#define all(v) v.begin(), v.end()
#define pb push_back
#define lb lower_bound
#define gibon ios::sync_with_stdio(false); cin.tie(0);
#define fi first
#define se second
#define pii pair<int, int>
#define pll pair<ll, ll>
typedef long long ll;
using namespace std;
const int mxN=1000005;
const int mxM=2200005;
const int mxK=61;
const int MOD=1e9+7;
const ll INF=1e18;
int N;
ll A[mxN], D[mxN];
pll l[mxN], r[mxN];
ll rl[mxN], suffix_rl[mxN];
ll C;
void input(){
cin >> N;
for(int i=1;i<N;i++) cin >> A[i];
for(int i=0;i<N;i++) cin >> D[i];
cin >> C;
}
void init(int n, std::vector<int> l, std::vector<int> d, int c){
N=n;
for(int i=1;i<N;i++) A[i]=l[i-1];
for(int i=0;i<N;i++) D[i]=d[i];
C=c;
}
void make_prefix(){
for(int i=1;i<N;i++) A[i]+=A[i-1];
// make l
for(int i=0;i<N;i++) l[i]=pll(A[i]-D[i], i);
sort(l, l+N);
// make r, rl, suffix_rl
vector <pll> v(N);
for(int i=0;i<N;i++) v[i]=pll(A[i]+D[i], i);
sort(all(v));
for(int i=0;i<N;i++) r[i]=v[i], rl[i]=v[i].fi-2*D[v[i].se];
suffix_rl[N]=1e18;
for(int i=N-1;i>=0;i--){
suffix_rl[i]=min(suffix_rl[i+1], rl[i]);
}
/*
printf("A: ");
for(int i=0;i<N;i++) printf("%lld ", A[i]);
printf("\nl: ");
for(int i=0;i<N;i++) printf("(%lld %lld) ", l[i].fi, l[i].se);
printf("\nr: ");
for(int i=0;i<N;i++) printf("(%lld %lld) ", r[i].fi, r[i].se);
printf("\nrl: ");
for(int i=0;i<N;i++) printf("%lld ", rl[i]);
printf("\n");
*/
}
bool tf(ll lim){
// c1 <= x+y <= c2, c3 <= y-x <= c4
pll pp=pll(-1e18, 1e18), pm=pll(-1e18, 1e18);
int rc=0;
for(int i=0;i<N;i++){
//printf("r[N-1]=%lld, l[0].fi=%lld\n")
int now=l[i].se;
if(r[N-1].se==l[i].se){
if(r[N-2].fi-l[i].fi<=lim) continue;
}
else{
if(r[N-1].fi-l[i].fi<=lim) continue;
}
while(r[rc].fi-l[i].fi<=lim) rc++;
if(lim<C+D[now]) return false;
ll delta=lim-C-D[now];
ll lval, rval;
if(r[N-1].se==l[i].se) lval=r[N-2].fi-delta;
else lval=r[N-1].fi-delta;
if(r[rc].se==l[i].se) rval=suffix_rl[rc+1]+delta;
else rval=suffix_rl[rc]+delta;
pp.fi=max(pp.fi, A[now]+lval), pp.se=min(pp.se, A[now]+rval);
pm.fi=max(pm.fi, lval-A[now]), pm.se=min(pm.se, rval-A[now]);
}
//printf("pp=%lld %lld, pm=%lld %lld\n", pp.fi, pp.se, pm.fi, pm.se);
int s=0, e=0;
for(int i=0;i<N;i++){
while(s<N && (A[i]+A[s]<pp.fi || A[s]-A[i]<pm.fi)) s++;
while(s-1>=0 && A[i]+A[s-1]>=pp.fi && A[s-1]-A[i]>=pm.fi) s--;
while(e>0 && (A[i]+A[e]>pp.se || A[e]-A[i]>pm.se)) e--;
while(e+1<N && A[i]+A[e+1]<=pp.se && A[e+1]-A[i]<=pm.se) e++;
if(s!=N && e!=-1 && s<=e) return true;
}
return false;
}
ll solv(){
make_prefix();
ll s=0, e=1e16;
while(s!=e){
ll mid=(s+e)/2;
if(tf(mid)) e=mid;
else s=mid+1;
}
return e;
}
long long find_shortcut(int n, std::vector<int> l, std::vector<int> d, int c)
{
init(n, l, d, c);
return solv();
}
/*
int main(){
input();
printf("%lld", solv());
}
*/
# | 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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |