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>
using namespace std;
#define ll long long
const int N=1000050;
const ll inf=5e18;
int n,d[N],c,q[N],ql,qr;
ll x[N];
bool Check(ll k)
{
ll mx=-inf,mn=inf;
ll ls=-inf,rs=inf,ld=-inf,rd=inf;
ql=1;qr=0;
for(int i=1;i<=n;i++)
{
for(;ql<=qr && x[q[ql]]-d[q[ql]]<x[i]+d[i]-k;ql++) mx=max(mx,x[q[ql]]+d[q[ql]]),mn=min(mn,x[q[ql]]-d[q[ql]]);
ls=max(ls,mx+x[i]+d[i]-k+c);
rs=min(rs,mn+x[i]-d[i]+k-c);
ld=max(ld,mx-x[i]+d[i]-k+c);
rd=min(rd,mn-x[i]-d[i]+k-c);
for(;ql<=qr && x[q[qr]]-d[q[qr]]>=x[i]-d[i];qr--);
q[++qr]=i;
}
int l1=n+1,r1=n+1,l2=0,r2=0;
for(int i=1;i<=n;i++)
{
for(;l1>=2 && x[i]+x[l1-1]>=ls;l1--);
for(;r1>=1 && x[i]+x[r1]>rs;r1--);
for(;l2<=n && x[i]-x[l2]>rd;l2++);
for(;r2<n && x[i]-x[r2+1]>=ld;r2++);
int l=max(l1,l2),r=min(r1,r2);
if(l<=r) return 1;
}
return 0;
}
ll find_shortcut(int n, vector<int> l, vector<int> d, int c)
{
::n=n;::c=c;::d[1]=d[0];int mxd=d[0];x[n+1]=inf;
for(int i=2;i<=n;i++) x[i]=x[i-1]+l[i-2],::d[i]=d[i-1],mxd=max(mxd,d[i-1]);
ll lo=0,hi=2*mxd+x[n],mi=lo+hi+1>>1;
for(;lo<hi;mi=lo+hi+1>>1)
{
if(Check(mi)) hi=mi-1;
else lo=mi;
}
return lo+1;
}
Compilation message (stderr)
shortcut.cpp: In function 'long long int find_shortcut(int, std::vector<int>, std::vector<int>, int)':
shortcut.cpp:40:32: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
ll lo=0,hi=2*mxd+x[n],mi=lo+hi+1>>1;
~~~~~^~
shortcut.cpp:41:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
for(;lo<hi;mi=lo+hi+1>>1)
~~~~~^~
# | 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... |