#define BUGO(x) cerr << #x << " -> " << (x) << endl;
#include "walk.h"
#include <algorithm>
#include <iostream>
#include <numeric>
#include <vector>
#include <queue>
#include <set>
#include <map>
using namespace std;
using llong = long long;
vector<pair<int, int>> nei[2000'000];
llong dist[2000'000];
const llong inf = 69'000'000'000'000'000LL;
llong Dijkstra(int s, int e)
{
fill(dist, dist + 2000'000, inf);
priority_queue<pair<llong, int>> q;
q.push({dist[s] = 0, s});
while (q.size())
{
int v = q.top().second, d = -q.top().first;
q.pop();
if (dist[v] != d) continue;
for (auto to: nei[v])
if (d + to.second < dist[to.first])
q.push({-(dist[to.first] = d + to.second), to.first});
}
return dist[e];
}
map<int, int> nodeOf[100'000];
long long min_distance(std::vector<int> x, std::vector<int> y, std::vector<int> l, std::vector<int> r,
std::vector<int> h, int s, int e) {
int n = x.size();
int m = l.size();
int nodes = 0;
for (int i = 0; i < n; ++i)
nodeOf[i][y[i]] = nodes++;
nodeOf[s][0] = nodes++;
nodeOf[e][0] = nodes++;
bool smallTest = (n <= 50 && m <= 50) || y != vector<int>(n, y[0]);
vector<int> towers(n);
iota(towers.begin(), towers.end(), 0);
sort(towers.begin(), towers.end(), [&](int l, int r) {
return y[l] < y[r];
});
vector<int> bridges(m);
iota(bridges.begin(), bridges.end(), 0);
sort(bridges.begin(), bridges.end(), [&](int l, int r) {
return h[l] < h[r];
});
set<int> activeTowers;
for (int tch = m - 1; tch >= 0; --tch)
{
int j = bridges[tch];
while (towers.size() && y[towers.back()] >= h[j]) {
activeTowers.insert(towers.back());
towers.pop_back();
}
int lt = -1;
for (auto it = activeTowers.lower_bound(l[j]); it != activeTowers.end() && *it <= r[j]; )
{
int i = *it;
if (!nodeOf[i].count(h[j]))
nodeOf[i][h[j]] = nodes++;
if (lt != -1) {
int u = nodeOf[i][h[j]];
int v = nodeOf[lt][h[j]];
nei[u].push_back({v, x[i] - x[lt]});
nei[v].push_back({u, x[i] - x[lt]});
}
lt = i;
if (smallTest)
++it;
else if (*it == l[j])
it = activeTowers.lower_bound(r[j]);
else
break;
}
}
for (int i = 0; i < n; ++i)
{
int ltHei = -1;
for (auto [hei, node]: nodeOf[i])
{
if (ltHei != -1) {
int u = nodeOf[i][ltHei];
int v = node;
nei[u].push_back({v, hei - ltHei});
nei[v].push_back({u, hei - ltHei});
}
ltHei = hei;
}
}
return Dijkstra(n, n + 1);
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
42 ms |
67552 KB |
Output is correct |
2 |
Correct |
42 ms |
67500 KB |
Output is correct |
3 |
Correct |
48 ms |
67592 KB |
Output is correct |
4 |
Incorrect |
48 ms |
67596 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
42 ms |
67608 KB |
Output is correct |
2 |
Correct |
43 ms |
67520 KB |
Output is correct |
3 |
Incorrect |
1565 ms |
156972 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
108 ms |
75644 KB |
Output is correct |
2 |
Incorrect |
413 ms |
92880 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
108 ms |
75644 KB |
Output is correct |
2 |
Incorrect |
413 ms |
92880 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
42 ms |
67552 KB |
Output is correct |
2 |
Correct |
42 ms |
67500 KB |
Output is correct |
3 |
Correct |
48 ms |
67592 KB |
Output is correct |
4 |
Incorrect |
48 ms |
67596 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |