This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// ~ Be Name Khoda ~ //
#include "shortcut.h"
//#pragma GCC optimize ("O3")
#pragma GCC target("avx2")
#pragma GCC optimize("unroll-loops,Ofast")
#include <bits/stdc++.h>
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
typedef long long ll;
#define pb push_back
#define mp make_pair
#define all(x) x.begin(), x.end()
#define fi first
#define se second
const int maxn = 1e6 + 10;
const int maxn5 = 3e3 + 10;
const int maxnt = 1.2e6 + 10;
const int maxn3 = 1e3 + 10;
const ll mod = 1e9 + 7;
const int lg = 20;
const ll inf = 1e18;
int n, pt[maxn5], per[maxn5], l[maxn5], r[maxn5];
ll c;
vector <ll> d;
ll ps[maxn5], ps2[maxn5];
vector <pair<ll, pair<ll, ll>>> av[maxn5];
vector <pair<ll, int>> srt[maxn5];
ll dis(int a, int b){
if(a > b)
swap(a, b);
return ps[b] - ps[a];
}
bool check(ll lim){
for(int i = 0; i < n; i++){
l[i] = i + 1;
r[i] = n;
pt[i] = upper_bound(all(av[i]), mp(lim, mp(inf, inf))) - av[i].begin();
}
for(int i = 0; i < n; i++) if(pt[i] < av[i].size()){
int b = upper_bound(ps, ps + n, c + d[i] + srt[i][0].fi + av[i][pt[i]].se.fi - lim) - ps - 1;
b = max(b, 0);
for(auto [w, a] : srt[i]){
while(b + 1 < n && c + d[i] + w + av[i][pt[i]].se.fi - ps[b] > lim)
b++;
l[a] = max(l[a], b);
}
b = upper_bound(ps2, ps2 + n, c + d[i] + srt[i][0].fi + av[i][pt[i]].se.se - lim) - ps2 - 1;
b = n - b - 1;
b = min(b, n - 1);
for(auto [w, a] : srt[i]){
while(b && c + d[i] + w + av[i][pt[i]].se.se + ps[b] > lim)
b--;
r[a] = min(r[a], b);
}
/*
int lo = i, hi = n;
while(hi - lo > 1){
int mid = (lo + hi) >> 1;
if(check(i, mid, 0, lim))
hi = mid;
else
lo = mid;
}
l = max(i + 1, hi);
lo = i; hi = n;
while(hi - lo > 1){
int mid = (lo + hi) >> 1;
if(check(i, mid, 1, lim))
lo = mid;
else
hi = mid;
}
r = min(n, lo);
//cout << "ok " << lim << ' ' << i << ' ' << l << ' ' << r << endl;
if(l <= r)
return true;
/*
bool re = true;
for(int j = 0; j < n && re; j++) for(int k = j + 1; k < n && re; k++) if(dis(j, k) + d[j] + d[k] > lim){
ll dis1 = dis(i, j);
ll need = lim - c - d[j] - d[k] - dis1;
if(need < 0)
re = false;
int ptl = k - (upper_bound(all(av[k][0]), need) - av[k][0].begin() - 1);
int ptr = k + (upper_bound(all(av[k][1]), need) - av[k][1].begin() - 1);
l = max(l, ptl);
r = min(r, ptr);
//cout << lim << ' ' << i << ' ' << j << ' ' << k << ' ' << l << ' ' << r << endl;
if(l > r)
re = false;
}
if(re)
return true;
*/
}
//cout << "FAslse" << endl;
for(int i = 0; i < n; i++)
if(l[i] <= r[i])
return true;
return false;
}
long long find_shortcut(int N, std::vector<int> l, std::vector<int> D, int C)
{
n = N;
c = C;
for(auto u : D)
d.pb(u);
ps[0] = 0;
for(int i = 1; i < n; i++)
ps[i] = ps[i - 1] + l[i - 1];
for(int i = 0; i < n; i++)
ps2[i] = -ps[i];
reverse(ps2, ps2 + n);
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++)
srt[i].pb({dis(i, j), j});
sort(all(srt[i]));
for(int j = i + 1; j < n; j++)
av[i].pb({dis(i, j) + d[i] + d[j], {d[j] + ps[j], d[j] - ps[j]}});
//if(i == 1)
// cout << av[i][1].back().se << ' ' << av[i][1].back().fi << endl;
sort(all(av[i]));
for(int j = int(av[i].size()) - 2; j >= 0; j--){
//if(i == 1)
// cout << j << endl;
av[i][j].se.fi = max(av[i][j].se.fi, av[i][j + 1].se.fi);
av[i][j].se.se = max(av[i][j].se.se, av[i][j + 1].se.se);
}
//if(i == 1)
// cout << av[i][1].back().se << ' ' << av[i][1].back().fi << endl;
}
ll lo = -1, hi = mod * (n + 3);
while(hi - lo > 1){
ll mid = (lo + hi) >> 1;
if(check(mid))
hi = mid;
else
lo = mid;
}
return hi;
}
Compilation message (stderr)
shortcut.cpp:91:9: warning: "/*" within comment [-Wcomment]
91 | /*
|
shortcut.cpp: In function 'bool check(ll)':
shortcut.cpp:49:41: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, std::pair<long long int, long long int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
49 | for(int i = 0; i < n; i++) if(pt[i] < av[i].size()){
| ~~~~~~^~~~~~~~~~~~~~
# | 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... |