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;
const int N = 1e6 + 10;
const long long int inf = 1e18;
long long n , c , d[N];
long long s[N];
struct NODE{
long long mxp , mxm;
long long ans;
NODE(){
mxp = mxm = ans = 0;
}
};
struct SEG{
NODE t[N << 2];
NODE Merge(NODE aa , NODE bb)
{
NODE res;
res.mxp = max(aa.mxp , bb.mxp);
res.mxm = max(aa.mxm , bb.mxm);
res.ans = max({aa.ans , bb.ans , 1LL * bb.mxp + aa.mxm});
return res;
}
void Build(int node = 1 , int nl = 0 , int nr = n)
{
if(nr - nl == 1)
{
t[node].mxp = d[nl] + s[nl];
t[node].mxm = d[nl] - s[nl];
t[node].ans = 0;
return;
}
int mid = (nl + nr) >> 1 , lc = node << 1 , rc = lc | 1;
Build(lc , nl , mid); Build(rc , mid , nr);
t[node] = Merge(t[lc] , t[rc]);
}
NODE Get(int l , int r , int node = 1 , int nl = 0 , int nr = n)
{
if(r <= nl || nr <= l || r <= l)
{
NODE emp;
emp.mxm = -inf;
return emp;
}
if(l <= nl && nr <= r)
{
//cout << "WTFFF " << nl << " " << nr << " " << t[node].mxm << endl;
return t[node];
}
int mid = (nl + nr) >> 1 , lc = node << 1 , rc = lc | 1;
return Merge(Get(l , r , lc , nl , mid) , Get(l , r , rc , mid , nr));
}
} seg;
long long Solve(int a , int b)
{
auto L = seg.Get(0 , a + 1) , R = seg.Get(b , n);
long long res = max({L.ans , R.ans , R.mxp - s[b] + s[a] + L.mxm + min(c , s[b] - s[a])});
if(c >= s[b] - s[a])
return res;
//cout << a << " " << b << " " << res << endl;
long long tmp = (c + s[a] + s[b]) / 2;
int pos = upper_bound(s , s + n , tmp) - s;
pos = min(pos , b + 1);
auto L1 = seg.Get(0 , pos) , L2 = seg.Get(pos , b + 1);
//cout << "WTF " << L1.ans << " " << L.mxm << " " << L2.mxm << endl;
res = max({res , L1.ans , c + s[b] + s[a] + L.mxm + L2.mxm});
//cout << a << " " << b << " p1 " << pos << " " << res << endl;
tmp = (s[a] + s[b] - c) / 2;
if(s[a] + s[b] - c < 0)
tmp = -1;
pos = upper_bound(s , s + n , tmp) - s;
pos = max(pos , a);
auto R1 = seg.Get(a , pos) , R2 = seg.Get(pos , n);
//cout << "WTF " << R2.ans << " " << R.mxp << " " << R1.mxp << endl;
res = max({res , R2.ans , c - s[a] - s[b] + R.mxp + R1.mxp});
//cout << a << " " << b << " p2 " << pos << " " << res << endl;
for(int i = a ; i < b ; i++) for(int j = i + 1 ; j < b ; j++)
res = max(res , min(d[i] + d[j] + s[j] - s[i] , d[i] + d[j] + c + s[b] - s[a] - s[j] + s[i]));
//cout << a << " " << b << " " << res << endl;
return res;
}
long long find_shortcut(int nn, std::vector<int> l, std::vector<int> D, int C)
{
n = nn;
c = C;
for(int i = 1 ; i < n ; i++)
s[i] = s[i - 1] + l[i - 1];
for(int i = 0 ; i < n ; i++)
d[i] = D[i];
seg.Build();
long long res = inf;
for(int a = 0 ; a + 1 < n ; a++) for(int b = a + 1 ; b < n ; b++)
{
//cout << a << " " << b << endl;
res = min(res , Solve(a , b));
}
return res;
}
# | 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... |