#include "shortcut.h"
#include <bits/stdc++.h>
using namespace std;
// macros
typedef long long ll;
typedef long double ld;
typedef pair<int, int> ii;
typedef pair<ll, ll> lll;
typedef tuple<int, int, int> iii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<iii> viii;
typedef vector<ll> vll;
typedef vector<lll> vlll;
#define REP(a,b,c) for(int a=int(b); a<int(c); a++)
#define RE(a,c) REP(a,0,c)
#define RE1(a,c) REP(a,1,c+1)
#define REI(a,b,c) REP(a,b,c+1)
#define REV(a,b,c) for(int a=int(c-1); a>=int(b); a--)
#define FOR(a,b) for(auto& a : b)
#define all(a) a.begin(), a.end()
#define EPS 1e-9
#define pb push_back
#define popb pop_back
#define fi first
#define se second
#define sz size()
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
const ll INF = 1e18;
const int N = 3e5+10;
ll n, c;
vll l, d;
struct MaxSeg {
ll seg[N];
void build(ll v) {
RE(i,N) seg[i] = v;
}
void set(int i, ll v) {
for(i++; i<N; i+=i&-i)
seg[i] = max(seg[i], v);
}
ll get(int i) {
ll res = -INF;
for(i++; i>0; i-=i&-i)
res = max(res, seg[i]);
return res;
}
} max1, max2;
vll difValues;
vll difVal2;
vi rval;
void preCalc() {
difValues.assign(n,0);
RE(i,n) difValues[i] = i;
sort(all(difValues),[&](int i, int j) {
return d[i]-l[i] > d[j]-l[j];
});
difVal2.assign(n,0);
RE(i,n) difVal2[i] = i;
sort(all(difVal2),[&](int i, int j) {
return d[i]+l[i] < d[j]+l[j];
});
rval.assign(n,0);
RE(i,n) rval[difValues[i]] = i;
}
bool possible(ll dist) {
ll minSum=0, maxSum=INF, minDif=0, maxDif=INF;
max1.build(-INF);
max2.build(-INF);
RE(i,n) {
max1.set(rval[i], d[i]-l[i]);
max2.set(rval[i], d[i]+l[i]);
}
FOR(i,difVal2) {
int lb=0, ub=n;
while(lb != ub) {
int mid=(lb+ub+1)/2;
int j = difValues[mid-1];
if(d[i]+l[i] + d[j]-l[j] > dist) lb=mid;
else ub=mid-1;
}
ll res1 = max1.get(lb-1);
ll res2 = max2.get(lb-1);
minDif = max(minDif, l[i] + d[i] - dist + c + res1);
maxDif = min(maxDif, l[i] - d[i] + dist - c - res2);
minSum = max(minSum, l[i] + d[i] - dist + c + res2);
maxSum = min(maxSum, l[i] - d[i] + dist - c - res1);
}
REP(i,1,n) {
ll curDif, curSum;
int j1 = 0, j2 = 0;
int lb=0, ub=i-1;
while(lb != ub) {
int mid=(lb+ub)/2;
curDif = l[i]-l[mid];
if(curDif > maxDif) lb=mid+1;
else ub=mid;
}
j1 = lb;
lb=0, ub=i-1;
while(lb != ub) {
int mid=(lb+ub)/2;
curSum = l[i]+l[mid];
if(curSum < minSum) lb=mid+1;
else ub=mid;
}
j2 = lb;
curDif = l[i]-l[j1];
curSum = l[i]+l[j1];
if(minDif <= curDif && curDif <= maxDif && minSum <= curSum && curSum <= maxSum)
return true;
curDif = l[i]-l[j2];
curSum = l[i]+l[j2];
if(minDif <= curDif && curDif <= maxDif && minSum <= curSum && curSum <= maxSum)
return true;
}
return false;
}
ll find_shortcut(int n_, vi l_, vi d_, int c_) {
n = n_; c=c_;
l.pb(0);
FOR(i,l_)
l.pb(i);
FOR(i,d_) d.pb(i);
REP(i,1,n) l[i]+=l[i-1];
preCalc();
ll lb=0, ub=INF;
while(lb != ub) {
ll mid=(lb+ub)/2;
if(possible(mid)) ub=mid;
else lb=mid+1;
}
return lb;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
4940 KB |
n = 4, 80 is a correct answer |
2 |
Correct |
30 ms |
4992 KB |
n = 9, 110 is a correct answer |
3 |
Correct |
23 ms |
4940 KB |
n = 4, 21 is a correct answer |
4 |
Correct |
22 ms |
4992 KB |
n = 3, 4 is a correct answer |
5 |
Incorrect |
23 ms |
4940 KB |
n = 2, incorrect answer: jury 62 vs contestant 72 |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
4940 KB |
n = 4, 80 is a correct answer |
2 |
Correct |
30 ms |
4992 KB |
n = 9, 110 is a correct answer |
3 |
Correct |
23 ms |
4940 KB |
n = 4, 21 is a correct answer |
4 |
Correct |
22 ms |
4992 KB |
n = 3, 4 is a correct answer |
5 |
Incorrect |
23 ms |
4940 KB |
n = 2, incorrect answer: jury 62 vs contestant 72 |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
4940 KB |
n = 4, 80 is a correct answer |
2 |
Correct |
30 ms |
4992 KB |
n = 9, 110 is a correct answer |
3 |
Correct |
23 ms |
4940 KB |
n = 4, 21 is a correct answer |
4 |
Correct |
22 ms |
4992 KB |
n = 3, 4 is a correct answer |
5 |
Incorrect |
23 ms |
4940 KB |
n = 2, incorrect answer: jury 62 vs contestant 72 |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
4940 KB |
n = 4, 80 is a correct answer |
2 |
Correct |
30 ms |
4992 KB |
n = 9, 110 is a correct answer |
3 |
Correct |
23 ms |
4940 KB |
n = 4, 21 is a correct answer |
4 |
Correct |
22 ms |
4992 KB |
n = 3, 4 is a correct answer |
5 |
Incorrect |
23 ms |
4940 KB |
n = 2, incorrect answer: jury 62 vs contestant 72 |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
4940 KB |
n = 4, 80 is a correct answer |
2 |
Correct |
30 ms |
4992 KB |
n = 9, 110 is a correct answer |
3 |
Correct |
23 ms |
4940 KB |
n = 4, 21 is a correct answer |
4 |
Correct |
22 ms |
4992 KB |
n = 3, 4 is a correct answer |
5 |
Incorrect |
23 ms |
4940 KB |
n = 2, incorrect answer: jury 62 vs contestant 72 |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
4940 KB |
n = 4, 80 is a correct answer |
2 |
Correct |
30 ms |
4992 KB |
n = 9, 110 is a correct answer |
3 |
Correct |
23 ms |
4940 KB |
n = 4, 21 is a correct answer |
4 |
Correct |
22 ms |
4992 KB |
n = 3, 4 is a correct answer |
5 |
Incorrect |
23 ms |
4940 KB |
n = 2, incorrect answer: jury 62 vs contestant 72 |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
4940 KB |
n = 4, 80 is a correct answer |
2 |
Correct |
30 ms |
4992 KB |
n = 9, 110 is a correct answer |
3 |
Correct |
23 ms |
4940 KB |
n = 4, 21 is a correct answer |
4 |
Correct |
22 ms |
4992 KB |
n = 3, 4 is a correct answer |
5 |
Incorrect |
23 ms |
4940 KB |
n = 2, incorrect answer: jury 62 vs contestant 72 |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
4940 KB |
n = 4, 80 is a correct answer |
2 |
Correct |
30 ms |
4992 KB |
n = 9, 110 is a correct answer |
3 |
Correct |
23 ms |
4940 KB |
n = 4, 21 is a correct answer |
4 |
Correct |
22 ms |
4992 KB |
n = 3, 4 is a correct answer |
5 |
Incorrect |
23 ms |
4940 KB |
n = 2, incorrect answer: jury 62 vs contestant 72 |
6 |
Halted |
0 ms |
0 KB |
- |