#pragma GCC optimize("Ofast")
#include "walk.h"
#include <bits/stdc++.h>
#define ll long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define F first
#define S second
using namespace std;
ll N, M, K, S, T, dis[4000000];
vector<pll> E[4000000], ver[100000], hor[100000], table[100000][20];
ll min_distance(vector<int> x, vector<int> h, vector<int> l, vector<int> r, vector<int> y, int s, int g)
{
N = x.size(), M = l.size();
for (int i = 0; i < N; i++)
table[i][0].emplace_back(pll(x[i], i));
for (int p = 0; p + 1 < 18; p++)
for (int i = 0; i + (1 << p) < N; i++)
{
for (auto j : table[i][p])
table[i][p + 1].emplace_back(j);
for (auto j : table[i + (1 << p)][p])
table[i][p + 1].emplace_back(j);
sort(table[i][p + 1].begin(), table[i][p + 1].end());
if (table[i][p + 1].size() > 10)
table[i][p + 1].resize(10);
}
for (int i = 0; i < N; i++)
{
++K;
ver[i].emplace_back(pll(0, K));
if (i == s)
S = K;
if (i == g)
T = K;
}
for (int i = 0; i < M; i++)
{
int lg = __lg(r[i] - l[i] + 1);
vector<pll> mango;
for (auto j : table[l[i]][lg])
mango.emplace_back(j);
for (auto j : table[r[i] - (1 << lg) + 1][lg])
mango.emplace_back(j);
for (auto [_, j] : mango)
{
if (y[i] <= h[j])
{
++K;
ver[j].emplace_back(pll(y[i], K));
hor[i].emplace_back(pll(x[j], K));
}
}
}
for (int i = 0; i < N; i++)
{
sort(ver[i].begin(), ver[i].end());
for (int j = 0; j + 1 < ver[i].size(); j++)
E[ver[i][j].S].emplace_back(ver[i][j + 1].S, ver[i][j + 1].F - ver[i][j].F),
E[ver[i][j + 1].S].emplace_back(ver[i][j].S, ver[i][j + 1].F - ver[i][j].F);
}
for (int i = 0; i < M; i++)
{
sort(hor[i].begin(), hor[i].end());
for (int j = 0; j + 1 < hor[i].size(); j++)
E[hor[i][j].S].emplace_back(hor[i][j + 1].S, hor[i][j + 1].F - hor[i][j].F),
E[hor[i][j + 1].S].emplace_back(hor[i][j].S, hor[i][j + 1].F - hor[i][j].F);
}
for (int i = 0; i <= K; i++)
dis[i] = 1e18;
priority_queue<pll, vector<pll>, greater<pll>> pq;
dis[S] = 0;
pq.push(pll(0, S));
while (!pq.empty())
{
auto [d, u] = pq.top();
pq.pop();
if (d > dis[u])
continue;
for (auto [v, w] : E[u])
if (dis[u] + w < dis[v])
{
dis[v] = dis[u] + w;
pq.push(pll(dis[v], v));
}
}
return (dis[T] >= 1e18 ? -1 : dis[T]);
}
Compilation message
walk.cpp: In function 'long long int min_distance(std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>, int, int)':
walk.cpp:48:13: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
48 | for (auto [_, j] : mango)
| ^
walk.cpp:61:25: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
61 | for (int j = 0; j + 1 < ver[i].size(); j++)
| ~~~~~~^~~~~~~~~~~~~~~
walk.cpp:68:25: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
68 | for (int j = 0; j + 1 < hor[i].size(); j++)
| ~~~~~~^~~~~~~~~~~~~~~
walk.cpp:79:8: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
79 | auto [d, u] = pq.top();
| ^
walk.cpp:83:13: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
83 | for (auto [v, w] : E[u])
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
92 ms |
145852 KB |
Output is correct |
2 |
Correct |
86 ms |
145800 KB |
Output is correct |
3 |
Correct |
90 ms |
145868 KB |
Output is correct |
4 |
Correct |
86 ms |
146060 KB |
Output is correct |
5 |
Correct |
80 ms |
145956 KB |
Output is correct |
6 |
Correct |
81 ms |
145932 KB |
Output is correct |
7 |
Incorrect |
82 ms |
146028 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
100 ms |
145924 KB |
Output is correct |
2 |
Correct |
82 ms |
145888 KB |
Output is correct |
3 |
Correct |
1074 ms |
348948 KB |
Output is correct |
4 |
Correct |
2015 ms |
950772 KB |
Output is correct |
5 |
Incorrect |
1628 ms |
929508 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
297 ms |
223712 KB |
Output is correct |
2 |
Incorrect |
1634 ms |
418576 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
297 ms |
223712 KB |
Output is correct |
2 |
Incorrect |
1634 ms |
418576 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
92 ms |
145852 KB |
Output is correct |
2 |
Correct |
86 ms |
145800 KB |
Output is correct |
3 |
Correct |
90 ms |
145868 KB |
Output is correct |
4 |
Correct |
86 ms |
146060 KB |
Output is correct |
5 |
Correct |
80 ms |
145956 KB |
Output is correct |
6 |
Correct |
81 ms |
145932 KB |
Output is correct |
7 |
Incorrect |
82 ms |
146028 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |