#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[5000000];
vector<pll> E[5000000], ver[100005], hor[100005], table[100005][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(), greater<pll>());
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])
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
87 ms |
169292 KB |
Output is correct |
2 |
Correct |
91 ms |
169268 KB |
Output is correct |
3 |
Correct |
91 ms |
169420 KB |
Output is correct |
4 |
Correct |
105 ms |
169432 KB |
Output is correct |
5 |
Correct |
90 ms |
169524 KB |
Output is correct |
6 |
Incorrect |
90 ms |
169516 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
92 ms |
169352 KB |
Output is correct |
2 |
Correct |
101 ms |
169332 KB |
Output is correct |
3 |
Correct |
1054 ms |
372216 KB |
Output is correct |
4 |
Correct |
1957 ms |
974308 KB |
Output is correct |
5 |
Incorrect |
1641 ms |
953336 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
326 ms |
247380 KB |
Output is correct |
2 |
Incorrect |
1669 ms |
442596 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
326 ms |
247380 KB |
Output is correct |
2 |
Incorrect |
1669 ms |
442596 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
87 ms |
169292 KB |
Output is correct |
2 |
Correct |
91 ms |
169268 KB |
Output is correct |
3 |
Correct |
91 ms |
169420 KB |
Output is correct |
4 |
Correct |
105 ms |
169432 KB |
Output is correct |
5 |
Correct |
90 ms |
169524 KB |
Output is correct |
6 |
Incorrect |
90 ms |
169516 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |