#include "shortcut.h"
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define fi first
#define se second
#define mp make_pair
const int N = (int)1e6 + 10;
ll pl[N];
ll pr[N];
ll cl[N];
vector<int> l;
vector<int> d;
ll lim;
int n;
ll dim;
struct segment_tree{
ll segt[N * 2];
int sz;
void upd(int id, ll v){
id += sz;
segt[id] = v;
id /= 2;
while(id > 0){
segt[id] = max(segt[id * 2], segt[id * 2 + 1]);
id /= 2;
}
}
ll get(int li, int ri){
li += sz;
ri += sz;
ll val = -(ll)1e18;
while(li <= ri){
if(li % 2 == 1) val = max(val, segt[li ++ ]);
if(ri % 2 == 0) val = max(val, segt[ri -- ]);
li /= 2;
ri /= 2;
}
return val;
}
};
segment_tree rp;
segment_tree lp;
bool check(ll k){ // k < dim by binary search so a shortcut is mandatory
int en = 0;
ll cmp;
for(int i = 0 ;i < n; i ++ ){
if(i + 1 >= n)
cmp = 0ll;
else
cmp = pr[i + 1] + l[i];
cmp += d[i];
if(cmp > k){
rp.upd(i, cl[i] + d[i]);
}
else{
rp.upd(i, -(ll)1e18);
}
}
ll vv;
for(int i = n - 1; i >= 0 ; i -- ){
if(i == 0)
cmp = 0ll;
else
cmp = pl[i - 1] + l[i - 1];
cmp += d[i];
if(cmp > k){
lp.upd(i,-cl[i] + d[i]);
}
else{
lp.upd(i, -(ll)1e18);
}
}
for(int st = 0 ; st < n ; st ++ ){
while(en + 1 < n && pl[st] + lim + pr[en] > k){
en ++ ;
}
if(pl[st] + lim + pr[en] <= k){
// clearly st < en
vv = rp.get(st + 1, en - 1) + lim + pr[en] - cl[st];
if(vv > k) continue;
vv = lp.get(st + 1, en - 1) + cl[en] + lim + pl[st];
if(vv > k) continue;
return true;
}
}
return false;
}
ll find_shortcut(int _n, vector<int> _l, vector<int> _d, int _c){
n = _n;
l = _l;
d = _d;
lim = _c;
for(int i = 0 ; i < n; i ++ ){
pl[i]=d[i];
if(i>0)
pl[i]=max(pl[i],pl[i-1]+l[i]);
}
dim = pl[n - 1];
for(int i = n - 1; i >= 0; i -- ){
pr[i]=d[i];
if(i + 1 < n)
pr[i]=max(pr[i],pr[i+1]+l[i]);
}
for(int i = 0 ; i + 1 < n; i ++ ){
dim = max(dim, pl[i] + pr[i + 1] + l[i]);
}
rp.sz = n;
lp.sz = n;
for(int i = 0 ;i + 1 < n; i ++ ){
cl[i + 1] += cl[i] + l[i];
}
ll lf = 0, rf = dim;
ll mid;
while(lf < rf){
mid = (lf + rf) / 2;
if(check(mid))
rf = mid;
else
lf = mid + 1;
}
return rf;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
n = 4, 80 is a correct answer |
2 |
Correct |
1 ms |
364 KB |
n = 9, 110 is a correct answer |
3 |
Incorrect |
1 ms |
364 KB |
n = 4, incorrect answer: jury 21 vs contestant 14 |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
n = 4, 80 is a correct answer |
2 |
Correct |
1 ms |
364 KB |
n = 9, 110 is a correct answer |
3 |
Incorrect |
1 ms |
364 KB |
n = 4, incorrect answer: jury 21 vs contestant 14 |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
n = 4, 80 is a correct answer |
2 |
Correct |
1 ms |
364 KB |
n = 9, 110 is a correct answer |
3 |
Incorrect |
1 ms |
364 KB |
n = 4, incorrect answer: jury 21 vs contestant 14 |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
n = 4, 80 is a correct answer |
2 |
Correct |
1 ms |
364 KB |
n = 9, 110 is a correct answer |
3 |
Incorrect |
1 ms |
364 KB |
n = 4, incorrect answer: jury 21 vs contestant 14 |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
n = 4, 80 is a correct answer |
2 |
Correct |
1 ms |
364 KB |
n = 9, 110 is a correct answer |
3 |
Incorrect |
1 ms |
364 KB |
n = 4, incorrect answer: jury 21 vs contestant 14 |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
n = 4, 80 is a correct answer |
2 |
Correct |
1 ms |
364 KB |
n = 9, 110 is a correct answer |
3 |
Incorrect |
1 ms |
364 KB |
n = 4, incorrect answer: jury 21 vs contestant 14 |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
n = 4, 80 is a correct answer |
2 |
Correct |
1 ms |
364 KB |
n = 9, 110 is a correct answer |
3 |
Incorrect |
1 ms |
364 KB |
n = 4, incorrect answer: jury 21 vs contestant 14 |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
n = 4, 80 is a correct answer |
2 |
Correct |
1 ms |
364 KB |
n = 9, 110 is a correct answer |
3 |
Incorrect |
1 ms |
364 KB |
n = 4, incorrect answer: jury 21 vs contestant 14 |
4 |
Halted |
0 ms |
0 KB |
- |