#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx,popcnt,sse4,abm")
#include <bits/stdc++.h>
using namespace std;
#ifndef WAIMAI
#include "walk.h"
#else
#include "grader.cpp"
#endif
#ifdef WAIMAI
#define debug(HEHE...) cout << "[" << #HEHE << "] : ", dout(HEHE)
void dout() {cout << '\n';}
template<typename T, typename...U>
void dout(T t, U...u) {cout << t << (sizeof...(u) ? ", " : ""), dout(u...);}
#else
#define debug(...) 7122
#endif
#define ll long long
#define Waimai ios::sync_with_stdio(false), cin.tie(0)
#define FOR(x,a,b) for (int x = a, I = b; x <= I; x++)
#define pb emplace_back
#define F first
#define S second
const ll INF = 1e18;
const int SIZE = 1e5 + 5;
int n, m, s, e, sz;
vector<int> y[SIZE];
int x[SIZE], h[SIZE];
vector<pair<int, int>> P;
tuple<int, int, int> a[SIZE];
vector<tuple<int, int, int>> ed;
int get_id(int i, int y) {
return lower_bound(P.begin(), P.end(), make_pair(i, y)) - P.begin() + 1;
}
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();
s = _s + 1, e = _g + 1;
FOR (i, 1, n) {
x[i] = _x[i - 1];
h[i] = _h[i - 1];
}
FOR (i, 1, m) {
_l[i - 1]++, _r[i - 1]++;
a[i] = {_y[i - 1], _l[i - 1], _r[i - 1]};
}
set<int> st;
FOR (i, 1, n) st.insert(i), y[i].pb(0);
vector<tuple<int, int, int>> all;
FOR (i, 1, m) all.pb(a[i]);
FOR (i, 1, n) all.pb(h[i], x[i], -i);
sort(all.begin(), all.end(), [&](auto x, auto y) {
return get<0>(x) != get<0>(y) ? get<0>(x) < get<0>(y) : get<2>(x) > get<2>(y);
});
for (auto [a, b, c] : all) {
if (c < 0) {
st.erase(-c);
continue;
}
int last = 0;
for (auto it = st.lower_bound(b); it != st.end() && *it <= c; it++) {
y[*it].pb(a);
if (last != 0) ed.pb(last, *it, a);
last = *it;
}
}
FOR (i, 1, n) {
sort(y[i].begin(), y[i].end());
y[i].erase(unique(y[i].begin(), y[i].end()), y[i].end());
for (int iy : y[i]) P.pb(i, iy);
sz += y[i].size();
}
sort(ed.begin(), ed.end());
ed.erase(unique(ed.begin(), ed.end()), ed.end());
vector<ll> dis(sz + 1, INF);
vector<vector<pair<int, int>>> adj(sz + 1);
priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<pair<ll, int>>> pq;
for (auto [i, j, y] : ed) {
int u = get_id(i, y), v = get_id(j, y);
// debug(i, j, y);
adj[u].pb(v, x[j] - x[i]);
adj[v].pb(u, x[j] - x[i]);
}
FOR (i, 1, n) for (int j = 0; j + 1 < y[i].size(); j++) {
// debug(i, y[i][j], y[i][j + 1]);
int u = get_id(i, y[i][j]), v = get_id(i, y[i][j + 1]);
adj[u].pb(v, y[i][j + 1] - y[i][j]);
adj[v].pb(u, y[i][j + 1] - y[i][j]);
}
s = get_id(s, 0), e = get_id(e, 0);
pq.emplace(dis[s] = 0, s);
while (pq.size()) {
auto [d, pos] = pq.top();
pq.pop();
if (d > dis[pos]) continue;
for (auto [np, w] : adj[pos]) if (d + w < dis[np]) {
pq.emplace(dis[np] = d + w, np);
}
}
return dis[e] == INF ? -1 : dis[e];
}
/*
in1
7 7
0 8
3 7
5 9
7 7
10 6
12 6
14 9
0 1 1
0 2 6
0 6 8
2 3 1
2 6 7
3 4 2
4 6 5
1 5
out1
27
in2
5 3
0 6
4 6
5 6
6 6
9 6
3 4 1
1 3 3
0 2 6
0 4
out2
21
*/
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:92:41: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
92 | FOR (i, 1, n) for (int j = 0; j + 1 < y[i].size(); j++) {
| ~~~~~~^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
2 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
2 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4700 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
1 ms |
4444 KB |
Output is correct |
14 |
Correct |
1 ms |
4444 KB |
Output is correct |
15 |
Correct |
1 ms |
4444 KB |
Output is correct |
16 |
Correct |
1 ms |
4444 KB |
Output is correct |
17 |
Correct |
1 ms |
4700 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
446 ms |
84920 KB |
Output is correct |
4 |
Correct |
548 ms |
95668 KB |
Output is correct |
5 |
Correct |
378 ms |
83128 KB |
Output is correct |
6 |
Correct |
326 ms |
72892 KB |
Output is correct |
7 |
Correct |
351 ms |
83564 KB |
Output is correct |
8 |
Correct |
611 ms |
105316 KB |
Output is correct |
9 |
Correct |
421 ms |
80400 KB |
Output is correct |
10 |
Correct |
781 ms |
127924 KB |
Output is correct |
11 |
Correct |
270 ms |
49224 KB |
Output is correct |
12 |
Correct |
205 ms |
39948 KB |
Output is correct |
13 |
Correct |
675 ms |
110852 KB |
Output is correct |
14 |
Correct |
163 ms |
37056 KB |
Output is correct |
15 |
Correct |
143 ms |
38592 KB |
Output is correct |
16 |
Correct |
156 ms |
41296 KB |
Output is correct |
17 |
Correct |
162 ms |
37656 KB |
Output is correct |
18 |
Correct |
140 ms |
42436 KB |
Output is correct |
19 |
Correct |
7 ms |
6172 KB |
Output is correct |
20 |
Correct |
65 ms |
20952 KB |
Output is correct |
21 |
Correct |
135 ms |
37056 KB |
Output is correct |
22 |
Correct |
160 ms |
39268 KB |
Output is correct |
23 |
Correct |
198 ms |
49136 KB |
Output is correct |
24 |
Correct |
149 ms |
40204 KB |
Output is correct |
25 |
Correct |
163 ms |
39092 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
43 ms |
14280 KB |
Output is correct |
2 |
Correct |
3273 ms |
538888 KB |
Output is correct |
3 |
Runtime error |
1492 ms |
1048576 KB |
Execution killed with signal 9 |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
43 ms |
14280 KB |
Output is correct |
2 |
Correct |
3273 ms |
538888 KB |
Output is correct |
3 |
Runtime error |
1492 ms |
1048576 KB |
Execution killed with signal 9 |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
2 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
2 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4700 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
1 ms |
4444 KB |
Output is correct |
14 |
Correct |
1 ms |
4444 KB |
Output is correct |
15 |
Correct |
1 ms |
4444 KB |
Output is correct |
16 |
Correct |
1 ms |
4444 KB |
Output is correct |
17 |
Correct |
1 ms |
4700 KB |
Output is correct |
18 |
Correct |
1 ms |
4444 KB |
Output is correct |
19 |
Correct |
1 ms |
4444 KB |
Output is correct |
20 |
Correct |
446 ms |
84920 KB |
Output is correct |
21 |
Correct |
548 ms |
95668 KB |
Output is correct |
22 |
Correct |
378 ms |
83128 KB |
Output is correct |
23 |
Correct |
326 ms |
72892 KB |
Output is correct |
24 |
Correct |
351 ms |
83564 KB |
Output is correct |
25 |
Correct |
611 ms |
105316 KB |
Output is correct |
26 |
Correct |
421 ms |
80400 KB |
Output is correct |
27 |
Correct |
781 ms |
127924 KB |
Output is correct |
28 |
Correct |
270 ms |
49224 KB |
Output is correct |
29 |
Correct |
205 ms |
39948 KB |
Output is correct |
30 |
Correct |
675 ms |
110852 KB |
Output is correct |
31 |
Correct |
163 ms |
37056 KB |
Output is correct |
32 |
Correct |
143 ms |
38592 KB |
Output is correct |
33 |
Correct |
156 ms |
41296 KB |
Output is correct |
34 |
Correct |
162 ms |
37656 KB |
Output is correct |
35 |
Correct |
140 ms |
42436 KB |
Output is correct |
36 |
Correct |
7 ms |
6172 KB |
Output is correct |
37 |
Correct |
65 ms |
20952 KB |
Output is correct |
38 |
Correct |
135 ms |
37056 KB |
Output is correct |
39 |
Correct |
160 ms |
39268 KB |
Output is correct |
40 |
Correct |
198 ms |
49136 KB |
Output is correct |
41 |
Correct |
149 ms |
40204 KB |
Output is correct |
42 |
Correct |
163 ms |
39092 KB |
Output is correct |
43 |
Correct |
43 ms |
14280 KB |
Output is correct |
44 |
Correct |
3273 ms |
538888 KB |
Output is correct |
45 |
Runtime error |
1492 ms |
1048576 KB |
Execution killed with signal 9 |
46 |
Halted |
0 ms |
0 KB |
- |