#include <bits/stdc++.h>
using namespace std;
using ll = long long;
vector<int> intersect[100'005];
vector<pair<int, int>> adj[100'005];
vector<ll> dist[100'005];
vector<int> idx[100'005];
const ll INF = 1e18;
ll min_distance(vector<int> x, vector<int> h, vector<int> l, vector<int> r, vector<int> y, int s, int g) {
auto st = chrono::steady_clock::now().time_since_epoch().count();
int n = x.size(), m = l.size();
array<int, 3> skywalks[m];
for (int i = 0; i < m; i++) {
skywalks[i] = {y[i], l[i], r[i]};
}
sort(skywalks, skywalks + m);
for (int i = m - 1; i >= 0; i--) {
y[m - 1 - i] = skywalks[i][0];
l[m - 1 - i] = skywalks[i][1];
r[m - 1 - i] = skywalks[i][2];
}
array<int, 3> buildings[n];
for (int i = 0; i < n; i++) {
buildings[i] = {h[i], x[i], i};
}
int actual[n];
sort(buildings, buildings + n);
bool oks = 0, okg = 0;
for (int i = 0; i < n; i++) {
h[i] = buildings[i][0];
x[i] = buildings[i][1];
actual[i] = buildings[i][2];
if (!oks && s == actual[i]) {
s = i;
oks = 1;
}
if (!okg && g == actual[i]) {
g = i;
okg = 1;
}
}
y.push_back(0);
idx[s].push_back(m);
dist[s].push_back(INF);
int ptr = n - 1;
set<array<int, 3>> gud;
for (int i = 0; i < m; i++) {
while (ptr >= 0 && y[i] <= h[ptr]) {
gud.insert({actual[ptr], ptr, h[ptr]});
ptr--;
}
auto it = gud.lower_bound({l[i], 0, 0});
while (it != gud.end() && (*it)[0] <= r[i]) {
intersect[i].push_back((*it)[1]);
it++;
}
for (int k = 0; k < intersect[i].size(); k++) {
dist[intersect[i][k]].push_back(INF);
idx[intersect[i][k]].push_back(i);
}
for (int k = 0; k < intersect[i].size(); k++) {
if (k + 1 < intersect[i].size()) {
adj[intersect[i][k]].push_back({intersect[i][k + 1], idx[intersect[i][k + 1]].size() - 1});
}
if (k - 1 >= 0) {
adj[intersect[i][k]].push_back({intersect[i][k - 1], idx[intersect[i][k - 1]].size() - 1});
}
}
}
priority_queue<pair<ll, pair<int, int>>> pq;
pq.push({0, {s, 0}});
while (pq.size()) {
ll d = -pq.top().first;
int i = pq.top().second.first;
int j = pq.top().second.second;
pq.pop();
if (d > dist[i][j]) continue;
j = y[idx[i][j]];
for (auto [nxt, nxth] : adj[i]) {
ll newd = d + abs(x[nxt] - x[i]) + abs(j - y[idx[nxt][nxth]]);
if (newd < dist[nxt][nxth]) {
dist[nxt][nxth] = newd;
pq.push({-newd, {nxt, nxth}});
}
}
}
// cout << s << ' ' << g << '\n';
ll ans = INF;
for (int i = 0; i < dist[g].size(); i++) {
ans = min(ans, dist[g][i] + y[idx[g][i]]);
}
// auto en = chrono::steady_clock::now().time_since_epoch().count();
// cout << (en - st) / 1e9 << '\n';
if (ans >= INF) return -1;
return ans;
}
//int main() {
// ios_base::sync_with_stdio(0), cin.tie(0);
// int N, M;
// cin >> N >> M;
// vector<int> X(N), H(N);
// for (int i = 0; i < N; i++) {
// cin >> X[i] >> H[i];
// }
// vector<int> L(M), R(M), Y(M);
// for (int i = 0; i < M; i++) {
// cin >> L[i] >> R[i] >> Y[i];
// }
// int S, G;
// cin >> S >> G;
// cout << min_distance(X, H, L, R, Y, S, G) << '\n';
// return 0;
//}
Compilation message
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:58:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
58 | for (int k = 0; k < intersect[i].size(); k++) {
| ~~^~~~~~~~~~~~~~~~~~~~~
walk.cpp:62:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
62 | for (int k = 0; k < intersect[i].size(); k++) {
| ~~^~~~~~~~~~~~~~~~~~~~~
walk.cpp:63:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
63 | if (k + 1 < intersect[i].size()) {
| ~~~~~~^~~~~~~~~~~~~~~~~~~~~
walk.cpp:90:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
90 | for (int i = 0; i < dist[g].size(); i++) {
| ~~^~~~~~~~~~~~~~~~
walk.cpp:11:10: warning: unused variable 'st' [-Wunused-variable]
11 | auto st = chrono::steady_clock::now().time_since_epoch().count();
| ^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
9684 KB |
Output is correct |
2 |
Correct |
5 ms |
9684 KB |
Output is correct |
3 |
Correct |
4 ms |
9676 KB |
Output is correct |
4 |
Correct |
5 ms |
9632 KB |
Output is correct |
5 |
Correct |
5 ms |
9684 KB |
Output is correct |
6 |
Correct |
5 ms |
9684 KB |
Output is correct |
7 |
Correct |
5 ms |
9684 KB |
Output is correct |
8 |
Correct |
5 ms |
9684 KB |
Output is correct |
9 |
Correct |
4 ms |
9684 KB |
Output is correct |
10 |
Correct |
6 ms |
9812 KB |
Output is correct |
11 |
Correct |
4 ms |
9684 KB |
Output is correct |
12 |
Correct |
5 ms |
9688 KB |
Output is correct |
13 |
Correct |
4 ms |
9684 KB |
Output is correct |
14 |
Correct |
5 ms |
9684 KB |
Output is correct |
15 |
Correct |
5 ms |
9684 KB |
Output is correct |
16 |
Correct |
4 ms |
9684 KB |
Output is correct |
17 |
Correct |
5 ms |
9684 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
9684 KB |
Output is correct |
2 |
Correct |
4 ms |
9684 KB |
Output is correct |
3 |
Correct |
1625 ms |
55600 KB |
Output is correct |
4 |
Correct |
630 ms |
55924 KB |
Output is correct |
5 |
Correct |
305 ms |
51796 KB |
Output is correct |
6 |
Correct |
256 ms |
48540 KB |
Output is correct |
7 |
Correct |
313 ms |
51900 KB |
Output is correct |
8 |
Correct |
3205 ms |
87540 KB |
Output is correct |
9 |
Correct |
399 ms |
49896 KB |
Output is correct |
10 |
Correct |
970 ms |
72688 KB |
Output is correct |
11 |
Correct |
266 ms |
33432 KB |
Output is correct |
12 |
Correct |
253 ms |
35064 KB |
Output is correct |
13 |
Correct |
811 ms |
70756 KB |
Output is correct |
14 |
Correct |
171 ms |
34832 KB |
Output is correct |
15 |
Correct |
150 ms |
35052 KB |
Output is correct |
16 |
Correct |
320 ms |
34452 KB |
Output is correct |
17 |
Correct |
135 ms |
33704 KB |
Output is correct |
18 |
Execution timed out |
4066 ms |
27584 KB |
Time limit exceeded |
19 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
49 ms |
17844 KB |
Output is correct |
2 |
Execution timed out |
4038 ms |
260332 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
49 ms |
17844 KB |
Output is correct |
2 |
Execution timed out |
4038 ms |
260332 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
9684 KB |
Output is correct |
2 |
Correct |
5 ms |
9684 KB |
Output is correct |
3 |
Correct |
4 ms |
9676 KB |
Output is correct |
4 |
Correct |
5 ms |
9632 KB |
Output is correct |
5 |
Correct |
5 ms |
9684 KB |
Output is correct |
6 |
Correct |
5 ms |
9684 KB |
Output is correct |
7 |
Correct |
5 ms |
9684 KB |
Output is correct |
8 |
Correct |
5 ms |
9684 KB |
Output is correct |
9 |
Correct |
4 ms |
9684 KB |
Output is correct |
10 |
Correct |
6 ms |
9812 KB |
Output is correct |
11 |
Correct |
4 ms |
9684 KB |
Output is correct |
12 |
Correct |
5 ms |
9688 KB |
Output is correct |
13 |
Correct |
4 ms |
9684 KB |
Output is correct |
14 |
Correct |
5 ms |
9684 KB |
Output is correct |
15 |
Correct |
5 ms |
9684 KB |
Output is correct |
16 |
Correct |
4 ms |
9684 KB |
Output is correct |
17 |
Correct |
5 ms |
9684 KB |
Output is correct |
18 |
Correct |
4 ms |
9684 KB |
Output is correct |
19 |
Correct |
4 ms |
9684 KB |
Output is correct |
20 |
Correct |
1625 ms |
55600 KB |
Output is correct |
21 |
Correct |
630 ms |
55924 KB |
Output is correct |
22 |
Correct |
305 ms |
51796 KB |
Output is correct |
23 |
Correct |
256 ms |
48540 KB |
Output is correct |
24 |
Correct |
313 ms |
51900 KB |
Output is correct |
25 |
Correct |
3205 ms |
87540 KB |
Output is correct |
26 |
Correct |
399 ms |
49896 KB |
Output is correct |
27 |
Correct |
970 ms |
72688 KB |
Output is correct |
28 |
Correct |
266 ms |
33432 KB |
Output is correct |
29 |
Correct |
253 ms |
35064 KB |
Output is correct |
30 |
Correct |
811 ms |
70756 KB |
Output is correct |
31 |
Correct |
171 ms |
34832 KB |
Output is correct |
32 |
Correct |
150 ms |
35052 KB |
Output is correct |
33 |
Correct |
320 ms |
34452 KB |
Output is correct |
34 |
Correct |
135 ms |
33704 KB |
Output is correct |
35 |
Execution timed out |
4066 ms |
27584 KB |
Time limit exceeded |
36 |
Halted |
0 ms |
0 KB |
- |