#include "shortcut.h"
#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define inside sl<=l_&&r<=sr
#define outside r<sl||sr<l_
#define orta ((l_+r)>>1)
#define INF 1000000009
#define mod 1000000007
#define ppair(x); cerr << "(" << x.first << ", " << x.second << ")\n";
#define bas(x) #x << ": " << x << " "
#define prarr(x, n); cerr << #x << ": "; for(int qsd = 0; qsd < n; qsd++) cerr << x[qsd] << " "; cerr << "\n";
#define prarrv(x); cerr << #x << ": "; for(int qsd = 0; qsd < (int)x.size(); qsd++) cerr << x[qsd] << " "; cerr << "\n";
using namespace std;
typedef long long ll;
vector<ll> lmax;
vector<ll> rmax;
vector<ll> prest;
vector<ll> sufst;
ll c;
int n;
vector<int> l, d;
vector<ll> pre;
ll stpre[4000006];
ll stsuf[4000006];
ll dist(int ll, int rr){
return abs(pre[rr] - pre[ll]);
}
ll getl(int i){
return max(lmax[i], (ll)d[i]);
}
ll getr(int i){
return max(rmax[i], (ll)d[i]);
}
void stcpre(int node, int l_, int r){
if (l_ == r) stpre[node] = prest[l_];
else {
int m = orta;
stcpre(node*2, l_, m);
stcpre(node*2+1, m+1, r);
stpre[node] = max(stpre[node*2], stpre[node*2+1]);
}
}
ll stqpre(int node, int l_, int r, int sl, int sr){
if (inside) return stpre[node];
else if (outside) return 0;
else {
int m = orta;
return max(stqpre(node*2, l_, m, sl, sr), stqpre(node*2+1, m+1, r, sl, sr));
}
}
void stcsuf(int node, int l_, int r){
if (l_ == r) stsuf[node] = sufst[l_];
else {
int m = orta;
stcsuf(node*2, l_, m);
stcsuf(node*2+1, m+1, r);
stsuf[node] = max(stsuf[node*2], stsuf[node*2+1]);
}
}
ll stqsuf(int node, int l_, int r, int sl, int sr){
if (inside) return stsuf[node];
else if (outside) return 0;
else {
int m = orta;
return max(stqsuf(node*2, l_, m, sl, sr), stqsuf(node*2+1, m+1, r, sl, sr));
}
}
int check(ll val){
int pl = 0;
int pr = n-1;
int islem = 1;
while (islem){
islem = 0;
for (int i = 0; i < n; i++){
ll rig = d[i] + dist(i, pl) + c + max(getr(pr), stqsuf(1, 0, n-1, max(i, pl)+1, pr) - dist(pr, n-1));
rig = min(rig, d[i] + rmax[i]);
while (pl < pr && rig > val){
pl++;
rig = rig = d[i] + dist(i, pl) + c + max(getr(pr), stqsuf(1, 0, n-1, max(i, pl)+1, pr) - dist(pr, n-1));
rig = min(rig, d[i] + rmax[i]);
islem = 1;
}
ll lef = d[i] + dist(i, pr) + c + max(getl(pl), stqpre(1, 0, n-1, pl, min(i, pr)-1) - dist(0, pl));
lef = min(lef, d[i] + lmax[i]);
while (pl < pr && lef > val){
pr--;
lef = d[i] + dist(i, pr) + c + max(getl(pl), stqpre(1, 0, n-1, pl, min(i, pr)-1) - dist(0, pl));
lef = min(lef, d[i] + lmax[i]);
islem = 1;
}
if (pl == pr) return 0;
}
}
return 1;
}
ll find_shortcut(int N, vector<int> L, vector<int> D, int C){
l = L;
d = D;
n = N;
c = C;
lmax.pb(0);
for (int i = 1; i < n; i++){
lmax.pb(max(lmax[i-1] + l[i-1], (ll)l[i-1] + d[i-1]));
}
rmax.resize(n);
rmax[n-1] = 0;
for (int i = n-2; i >= 0; i--){
rmax[i] = max(rmax[i+1] + l[i], (ll)l[i] + d[i+1]);
}
pre.pb(0);
for (int i = 1; i < n; i++){
pre.pb(pre[i-1] + l[i-1]);
}
prest.resize(n);
for (int i = 0; i < n; i++){
prest[i] = dist(0, i) + d[i];
}
sufst.resize(n);
for (int i = n-1; i >= 0; i--) sufst[i] = dist(i, n-1) + d[i];
stcpre(1, 0, n-1);
stcsuf(1, 0, n-1);
ll bas = 0;
ll son = 1e15+1;
while (bas < son){
int m = (bas+son)>>1;
if (check(m)) son = m;
else bas = m+1;
}
return bas;
}
Compilation message
shortcut.cpp: In function 'int check(ll)':
shortcut.cpp:92:9: warning: operation on 'rig' may be undefined [-Wsequence-point]
rig = rig = d[i] + dist(i, pl) + c + max(getr(pr), stqsuf(1, 0, n-1, max(i, pl)+1, pr) - dist(pr, n-1));
~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
n = 4, 80 is a correct answer |
2 |
Incorrect |
2 ms |
376 KB |
n = 9, incorrect answer: jury 110 vs contestant 130 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
n = 4, 80 is a correct answer |
2 |
Incorrect |
2 ms |
376 KB |
n = 9, incorrect answer: jury 110 vs contestant 130 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
n = 4, 80 is a correct answer |
2 |
Incorrect |
2 ms |
376 KB |
n = 9, incorrect answer: jury 110 vs contestant 130 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
n = 4, 80 is a correct answer |
2 |
Incorrect |
2 ms |
376 KB |
n = 9, incorrect answer: jury 110 vs contestant 130 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
n = 4, 80 is a correct answer |
2 |
Incorrect |
2 ms |
376 KB |
n = 9, incorrect answer: jury 110 vs contestant 130 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
n = 4, 80 is a correct answer |
2 |
Incorrect |
2 ms |
376 KB |
n = 9, incorrect answer: jury 110 vs contestant 130 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
n = 4, 80 is a correct answer |
2 |
Incorrect |
2 ms |
376 KB |
n = 9, incorrect answer: jury 110 vs contestant 130 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
n = 4, 80 is a correct answer |
2 |
Incorrect |
2 ms |
376 KB |
n = 9, incorrect answer: jury 110 vs contestant 130 |
3 |
Halted |
0 ms |
0 KB |
- |