This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// I can do all things through Christ who strenghthens me
// Philippians 4:13
#include "bits/stdc++.h"
#include "walk.h"
using namespace std;
#define REP(i, j, k) for (int i = j; i < (k); i++)
#define RREP(i, j, k) for (int i = j; i >= (k); i--)
template <class T>
inline bool mnto(T &a, const T b) {return a > b ? a = b, 1 : 0;}
template <class T>
inline bool mxto(T &a, const T b) {return a < b ? a = b, 1 : 0;}
typedef long long ll;
typedef long double ld;
#define FI first
#define SE second
typedef pair<int, int> ii;
typedef pair<ll, ll> pll;
#define ALL(x) x.begin(), x.end()
#define SZ(x) (int) x.size()
#define pb push_back
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<ll> vll;
typedef tuple<int, int, int> iii;
typedef tuple<ll, int, int> lll;
typedef vector<iii> viii;
#ifndef DEBUG
#define cerr if (0) cerr
#endif
const int INF = 1000000005;
const ll LINF = 1000000000000000005;
const int MAXN = 100005;
int n, m;
vi vy[MAXN];
map<int, int> mp[MAXN];
vll d[MAXN];
vi prv[MAXN], nxt[MAXN];
priority_queue<lll, vector<lll>, greater<lll>> pq;
ll min_distance(vi x, vi h, vi l, vi r, vi y, int s, int g) {
n = SZ(x), m = SZ(l);
REP (i, 0, m) {
REP (p, l[i], r[i] + 1) {
if (h[p] < y[i]) {
continue;
}
vy[p].pb(i);
}
}
REP (i, 0, n) {
d[i] = vll(SZ(vy[i]), LINF);
prv[i] = vi(SZ(vy[i]), -1);
nxt[i] = vi(SZ(vy[i]), INF);
sort(ALL(vy[i]), [&] (int l, int r) {
return y[l] < y[r];
});
REP (j, 0, SZ(vy[i])) {
mp[i][y[vy[i][j]]] = j;
}
}
REP (i, 0, m) {
int tprv = -1;
REP (p, l[i], r[i] + 1) {
if (h[p] < y[i]) {
continue;
}
prv[p][mp[p][y[i]]] = tprv;
if (tprv != -1) {
nxt[tprv][mp[tprv][y[i]]] = p;
}
tprv = p;
}
}
REP (j, 0, SZ(vy[s])) {
d[s][j] = y[vy[s][j]];
pq.push({d[s][j], s, j});
}
while (!pq.empty()) {
auto [ud, up, ui] = pq.top(); pq.pop();
if (ud != d[up][ui]) {
continue;
}
int id = vy[up][ui];
for (int j : {max(0, (int) ui - 1), min(SZ(vy[up]), (int) ui + 2)}) {
if (mnto(d[up][j], ud + abs(y[id] - y[vy[up][j]]))) {
pq.push({d[up][j], up, j});
}
}
for (int p : {max(l[id], prv[up][ui]), min(r[id], nxt[up][ui])}) {
int j = mp[p][y[id]];
if (mnto(d[p][j], ud + abs(x[p] - x[up]))) {
pq.push({d[p][j], p, j});
}
}
}
ll ans = LINF;
REP (j, 0, SZ(vy[g])) {
mnto(ans, d[g][j] + y[vy[g][j]]);
}
if (ans == LINF) {
return -1;
}
return ans;
}
# | 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... |