This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "walk.h"
#include <bits/stdc++.h>
using namespace std;
//#define cerr if (false) cerr
#define db(x) cerr << #x << "=" << x << endl
#define db2(x, y) cerr << #x << "=" << x << "," << #y << "=" << y << endl
#define db3(x, y, z) cerr << #x << "=" << x << "," << #y << "=" << y << "," << #z << "=" << z << endl
#define dbv(v) cerr << #v << "="; for (auto _x : v) cerr << _x << ", "; cerr << endl
#define dba(a, n) cerr << #a << "="; for (int _i = 0; _i < (n); ++_i) cerr << a[_i] << ", "; cerr << endl
template <typename A, typename B>
ostream& operator<<(ostream& os, const pair<A, B>& x) {
return os << "(" << x.first << "," << x.second << ")";
}
typedef long long ll;
typedef long double ld;
ll min_distance(vector<int> x, vector<int> h, vector<int> l, vector<int> r, vector<int> y, int s, int g) {
int n = x.size();
int m = l.size();
vector<vector<int>> vert(n), hor(m);
vector<vector<pair<int, int>>> events(n);
for (int i = 0; i < m; ++i) {
events[l[i]].emplace_back(i, 1);
events[r[i]].emplace_back(i, -1);
}
auto f = [&](int q) {
vector<pair<int, int>> lhs = {{h[q], q}}, rhs = {{h[q], q}};
for (int i = q - 1; i >= 0; --i) {
if (h[i] > lhs.back().first) lhs.push_back({h[i], i});
}
for (int i = q + 1; i < n; ++i) {
if (h[i] > rhs.back().first) rhs.push_back({h[i], i});
}
for (int i = 0; i < m; ++i) {
if (r[i] < q || l[i] > q) continue;
auto it = lower_bound(lhs.begin(), lhs.end(), make_pair(y[i], -1));
if (it == lhs.end()) continue;
int p = it->second;
if (x[p] < l[i]) continue;
vert[p].push_back(y[i]);
hor[i].push_back(x[p]);
}
for (int i = 0; i < m; ++i) {
if (r[i] < q || l[i] > q) continue;
auto it = lower_bound(rhs.begin(), rhs.end(), make_pair(y[i], -1));
if (it == rhs.end()) continue;
int p = it->second;
if (x[p] > r[i]) continue;
vert[p].push_back(y[i]);
hor[i].push_back(x[p]);
}
};
f(s);
f(g);
set<pair<int, int>> S;
for (int i = 0; i < n; ++i) {
for (auto& e : events[i]) {
int j = e.first;
vert[i].push_back(y[j]);
hor[j].push_back(x[i]);
if (e.second == 1) S.insert({y[j], j});
}
vector<int> nvert;
for (int yj : vert[i]) {
auto it = S.lower_bound({yj, -1});
if (it == S.begin()) continue;
int k = prev(it)->second;
hor[k].push_back(x[i]);
nvert.push_back(y[k]);
}
vert[i].insert(vert[i].end(), nvert.begin(), nvert.end());
if (i == s || i == g) vert[i].push_back(0);
for (auto& e : events[i]) {
int j = e.first;
if (e.second == -1) S.erase({y[j], j});
}
}
map<pair<int, int>, int> mp;
auto getId = [&](pair<int, int> a) {
auto it = mp.find(a);
if (it != mp.end()) return it->second;
int s = mp.size();
return mp[a] = s;
};
for (int i = 0; i < n; ++i)
for (int j : vert[i]) getId({x[i], j});
for (int i = 0; i < m; ++i)
for (int j : hor[i]) getId({j, y[i]});
vector<vector<pair<int, int>>> E(mp.size());
for (int i = 0; i < n; ++i) {
sort(vert[i].begin(), vert[i].end());
vert[i].resize(unique(vert[i].begin(), vert[i].end()) - vert[i].begin());
for (int j = 0; j + 1 < vert[i].size(); ++j) {
int u = getId({x[i], vert[i][j]});
int v = getId({x[i], vert[i][j + 1]});
int d = vert[i][j + 1] - vert[i][j];
E[u].push_back({v, d});
E[v].push_back({u, d});
}
}
for (int i = 0; i < m; ++i) {
sort(hor[i].begin(), hor[i].end());
hor[i].resize(unique(hor[i].begin(), hor[i].end()) - hor[i].begin());
for (int j = 0; j + 1 < hor[i].size(); ++j) {
int u = getId({hor[i][j], y[i]});
int v = getId({hor[i][j + 1], y[i]});
int d = hor[i][j + 1] - hor[i][j];
E[u].push_back({v, d});
E[v].push_back({u, d});
}
}
const ll INF = 1LL << 60;
vector<ll> dist(mp.size(), INF);
int src = getId({x[s], 0}), dest = getId({x[g], 0});
typedef pair<ll, int> Pair;
priority_queue<Pair, vector<Pair>, greater<Pair>> pq;
dist[src] = 0;
pq.emplace(dist[src], src);
while (!pq.empty()) {
int u = pq.top().second;
ll cdist = pq.top().first;
pq.pop();
if (cdist > dist[u]) continue;
for (auto e : E[u]) {
int v = e.first;
ll val = dist[u] + e.second;
if (val < dist[v]) {
dist[v] = val;
pq.emplace(val, v);
}
}
}
return dist[dest] == INF ? -1 : dist[dest];
}
Compilation message (stderr)
walk.cpp: In function 'll min_distance(std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>, int, int)':
walk.cpp:92:25: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int j = 0; j + 1 < vert[i].size(); ++j) {
~~~~~~^~~~~~~~~~~~~~~~
walk.cpp:103:25: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int j = 0; j + 1 < hor[i].size(); ++j) {
~~~~~~^~~~~~~~~~~~~~~
# | 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... |