#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[6900'000];
llong dist[6900'000];
const llong inf = 69'000'000'000'000'000LL;
llong Dijkstra(int s, int e)
{
fill(dist, dist + 6900'000, inf);
priority_queue<pair<llong, int>> q;
q.push({dist[s] = 0, s});
while (q.size())
{
int v = q.top().second;
llong 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);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
144 ms |
220932 KB |
Output is correct |
2 |
Correct |
144 ms |
221020 KB |
Output is correct |
3 |
Correct |
137 ms |
220912 KB |
Output is correct |
4 |
Incorrect |
139 ms |
220936 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
142 ms |
220936 KB |
Output is correct |
2 |
Correct |
138 ms |
220976 KB |
Output is correct |
3 |
Correct |
1330 ms |
308196 KB |
Output is correct |
4 |
Correct |
1198 ms |
323488 KB |
Output is correct |
5 |
Correct |
787 ms |
310332 KB |
Output is correct |
6 |
Correct |
735 ms |
299712 KB |
Output is correct |
7 |
Incorrect |
807 ms |
310372 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
213 ms |
228292 KB |
Output is correct |
2 |
Incorrect |
494 ms |
244440 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
213 ms |
228292 KB |
Output is correct |
2 |
Incorrect |
494 ms |
244440 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
144 ms |
220932 KB |
Output is correct |
2 |
Correct |
144 ms |
221020 KB |
Output is correct |
3 |
Correct |
137 ms |
220912 KB |
Output is correct |
4 |
Incorrect |
139 ms |
220936 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |