# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1101029 | azberjibiou | Shortcut (IOI16_shortcut) | C++17 | 0 ms | 0 KiB |
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], rl[mxN];
pair<pll, pll> 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]=pll(v[i].fi-2*D[v[i].se], v[i].se);
suffix_rl[N]=pair<pll, pll>(pll(1e18, -1), pll(1e18, -1));
for(int i=N-1;i>=0;i--){
pll now=rl[i];
pair<pll, pll> pre=suffix_rl[i+1];
if(now<pre.fi) suffix_rl[i]=pair<pll, pll>(now, pre.fi);
else if(now<pre.se) suffix_rl[i]=pair<pll, pll>(pre.fi, now);
else suffix_rl[i]=pre;
}
/*
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];
//printf("delta=%lld\n", delta);
ll lval, rval;
if(r[N-1].se==now) lval=r[N-2].fi-delta;
else lval=r[N-1].fi-delta;
if(suffix_rl[rc].fi.se==now) rval=suffix_rl[rc].se.fi+delta;
else rval=suffix_rl[rc].fi.fi+delta;
//printf("lval=%lld, rval=%lld\n", lval, rval);
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++;
//printf("i=%d, s=%d, e=%d\n", i, s, e);
if(s!=N && e!=-1 && s<=e) return true;
}
return false;
}
ll solv(){
make_prefix();
ll s=0, e=1e16;
while(s!=e){
//printf("s=%lld, e=%lld\n", 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();
/*
for(int i=0;i<10;i++){
int n=2;
int a=rand()%10+1;
int d1=rand()%10, d2=rand()%10;
int d=rand()%10;
int res=find_shortcut(n, vector<int>{a}, vector<int>{d1, d2}, d);
if(res!=d1+d2+min(a, d)){
printf("solv(n=2, a=%d, d1, d2=%d %d, d=%d)=%lld\n", a, d1, d2, d, res);
}
}
*/
printf("%lld", solv());
}
*/