This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
#define pb push_back
#define jizz ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
#define F first
#define S second
#define ET cout << "\n"
#define MP make_pair
#define MEM(i,j) memset(i,j,sizeof i)
#define ALL(v) v.begin(),v.end()
#define DB(a,s,e) {for(int i=s;i<e;++i) cout << a[i] << " ";ET;}
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
const ll INF=1e18;
/*
j<i
si-sj+di+dj>k
si+di-k>sj-dj
x-y<0
|sj-x|+|si-y|+di+dj+c<=k
x+y>=(sj+dj)+(si+di)-k+c ->b0
x-y>=(sj+dj)-(si-di)-k+c ->b1
x-y<=(sj-dj)-(si+di)+k-c ->b2
x+y<=(sj-dj)+(si-di)+k-c ->b3
y>=b0-x
y>=x-b2
y<=x-b1
y<=b3-x
y<=sn
*/
ll find_shortcut(int n,vector<int> l,vector<int> d, int c)
{
ll mx=-INF,L=1,R=0;
vector<ll> s(n,0),dp(n,0),q(n,0);
for(int i=1;i<n;++i)
s[i]=s[i-1]+l[i-1];
for(int i=0;i<n;++i)
dp[i]=max((ll)d[i],s[i]+mx),mx=max(mx,dp[i]-s[i]);
for(int i=1;i<n;++i)
R=max(R,dp[i-1]+d[i]+s[i]-s[i-1]);
while(L<R)
{
ll mid=L+R>>1,mxt=-INF,mit=INF,b0=-INF,b1=-INF,b2=-1,b3=INF,flag=0,lf=0,rg=-1;
for(int i=0,j=0;i<n;++i)
{
while(rg>=lf&&s[i]+d[i]-s[q[lf]]+d[q[lf]]>mid)
mxt=max(mxt,s[q[lf]]+d[q[lf]]),mit=min(mit,s[q[lf]]-d[q[lf]]),lf++;
b0=max(b0,s[i]+d[i]+mxt-mid+c),b1=max(b1,mxt-s[i]+d[i]-mid+c);
b2=min(b2,mit-s[i]-d[i]+mid-c),b3=min(b3,mit+s[i]-d[i]+mid-c);
while(rg>=lf&&s[q[rg]]-d[q[rg]]>s[i]-d[i]) --rg;
q[++rg]=i;
}
for(int i=0,j=n,k=0;i+1<n&&!flag;++i)
{
ll up=min({s[n-1],s[i]-b1,b3-s[i]}),dn=max(b0-s[i],s[i]-b2);
while(j>0&&s[j-1]>=b0-s[i]) --j;
while(k<n&&s[k]<s[i]-b2) ++k;
if(max(j,k)!=n&&s[max(j,k)]<=up) flag=1;
}
if(flag) R=mid;
else L=mid+1;
}
return L;
}
Compilation message (stderr)
shortcut.cpp: In function 'll find_shortcut(int, std::vector<int>, std::vector<int>, int)':
shortcut.cpp:51:11: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
ll mid=L+R>>1,mxt=-INF,mit=INF,b0=-INF,b1=-INF,b2=-1,b3=INF,flag=0,lf=0,rg=-1;
~^~
shortcut.cpp:52:15: warning: unused variable 'j' [-Wunused-variable]
for(int i=0,j=0;i<n;++i)
^
shortcut.cpp:63:40: warning: unused variable 'dn' [-Wunused-variable]
ll up=min({s[n-1],s[i]-b1,b3-s[i]}),dn=max(b0-s[i],s[i]-b2);
^~
# | 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... |