#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 inf = 2e9;
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, Q;
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;
}
set<pair<int, int>> seg;
void split(int p) {
auto it = prev(seg.lower_bound({p + 1, 0}));
if (it->F != p) seg.emplace(p, it->S);
}
void upd(int l, int r, int y) {
split(l), split(r + 1);
for (auto it = seg.lower_bound({l, 0}); it->F <= r; it = seg.erase(it));
seg.emplace(l, y);
}
int get_h(int p) {
auto it = prev(seg.lower_bound({p + 1, 0}));
return it->S;
}
ll same() {
seg.emplace(1, 0);
seg.emplace(n + 1, 0);
sort(a + 1, a + m + 1);
FOR (i, 1, n) y[i].pb(0);
FOR (i, 1, m) {
auto [h, l, r] = a[i];
y[l].pb(h), y[r].pb(h);
y[l].pb(get_h(l));
y[r].pb(get_h(r));
upd(l, r, h);
}
seg.clear();
seg.emplace(1, inf);
seg.emplace(n + 1, inf);
for (int i = m; i >= 1; i--) {
auto [h, l, r] = a[i];
int hl = get_h(l), hr = get_h(r);
if (hl != inf) y[l].pb(hl);
if (hr != inf) y[r].pb(hr);
upd(l, r, h);
}
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), Q.pb(iy, i);
sz += y[i].size();
}
sort(Q.begin(), Q.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 (i, 1, n) for (int j = 0; j + 1 < y[i].size(); j++) {
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]);
}
FOR (i, 1, m) {
auto [h, l, r] = a[i];
int j = lower_bound(Q.begin(), Q.end(), make_pair(h, l)) - Q.begin();
while (Q[j].S < r) {
int u = get_id(Q[j].S, h), v = get_id(Q[j + 1].S, h), d = x[Q[j + 1].S] - x[Q[j].S];
adj[u].pb(v, d);
adj[v].pb(u, d);
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];
}
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]};
}
if (s == 1 && e == n) return same();
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);
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++) {
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 same()':
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++) {
| ~~~~~~^~~~~~~~~~~~~
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:171:41: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
171 | 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 |
2 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4616 KB |
Output is correct |
6 |
Correct |
1 ms |
4700 KB |
Output is correct |
7 |
Correct |
1 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 |
2 ms |
4700 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
12 |
Correct |
1 ms |
4440 KB |
Output is correct |
13 |
Correct |
2 ms |
4444 KB |
Output is correct |
14 |
Correct |
2 ms |
4444 KB |
Output is correct |
15 |
Correct |
1 ms |
4444 KB |
Output is correct |
16 |
Correct |
1 ms |
4612 KB |
Output is correct |
17 |
Correct |
1 ms |
4532 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
464 ms |
84576 KB |
Output is correct |
4 |
Correct |
581 ms |
98492 KB |
Output is correct |
5 |
Correct |
358 ms |
86716 KB |
Output is correct |
6 |
Correct |
332 ms |
78268 KB |
Output is correct |
7 |
Correct |
362 ms |
87036 KB |
Output is correct |
8 |
Correct |
620 ms |
106428 KB |
Output is correct |
9 |
Correct |
436 ms |
82764 KB |
Output is correct |
10 |
Correct |
822 ms |
126904 KB |
Output is correct |
11 |
Correct |
296 ms |
50440 KB |
Output is correct |
12 |
Correct |
475 ms |
49836 KB |
Output is correct |
13 |
Correct |
783 ms |
78628 KB |
Output is correct |
14 |
Correct |
186 ms |
41036 KB |
Output is correct |
15 |
Correct |
179 ms |
41444 KB |
Output is correct |
16 |
Correct |
161 ms |
42180 KB |
Output is correct |
17 |
Correct |
180 ms |
40824 KB |
Output is correct |
18 |
Correct |
200 ms |
45840 KB |
Output is correct |
19 |
Correct |
8 ms |
6336 KB |
Output is correct |
20 |
Correct |
86 ms |
22984 KB |
Output is correct |
21 |
Correct |
285 ms |
47892 KB |
Output is correct |
22 |
Correct |
351 ms |
61804 KB |
Output is correct |
23 |
Correct |
365 ms |
64392 KB |
Output is correct |
24 |
Correct |
274 ms |
55976 KB |
Output is correct |
25 |
Correct |
359 ms |
51440 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
104 ms |
15252 KB |
Output is correct |
2 |
Correct |
628 ms |
65936 KB |
Output is correct |
3 |
Correct |
678 ms |
70296 KB |
Output is correct |
4 |
Correct |
847 ms |
83692 KB |
Output is correct |
5 |
Correct |
838 ms |
87708 KB |
Output is correct |
6 |
Correct |
691 ms |
83360 KB |
Output is correct |
7 |
Correct |
414 ms |
50344 KB |
Output is correct |
8 |
Correct |
407 ms |
47532 KB |
Output is correct |
9 |
Correct |
598 ms |
78156 KB |
Output is correct |
10 |
Correct |
239 ms |
52656 KB |
Output is correct |
11 |
Correct |
15 ms |
9424 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
104 ms |
15252 KB |
Output is correct |
2 |
Correct |
628 ms |
65936 KB |
Output is correct |
3 |
Correct |
678 ms |
70296 KB |
Output is correct |
4 |
Correct |
847 ms |
83692 KB |
Output is correct |
5 |
Correct |
838 ms |
87708 KB |
Output is correct |
6 |
Correct |
691 ms |
83360 KB |
Output is correct |
7 |
Correct |
414 ms |
50344 KB |
Output is correct |
8 |
Correct |
407 ms |
47532 KB |
Output is correct |
9 |
Correct |
598 ms |
78156 KB |
Output is correct |
10 |
Correct |
239 ms |
52656 KB |
Output is correct |
11 |
Correct |
15 ms |
9424 KB |
Output is correct |
12 |
Correct |
582 ms |
70296 KB |
Output is correct |
13 |
Correct |
584 ms |
81052 KB |
Output is correct |
14 |
Correct |
691 ms |
85656 KB |
Output is correct |
15 |
Correct |
523 ms |
75596 KB |
Output is correct |
16 |
Correct |
499 ms |
73884 KB |
Output is correct |
17 |
Correct |
629 ms |
89332 KB |
Output is correct |
18 |
Correct |
489 ms |
74564 KB |
Output is correct |
19 |
Correct |
505 ms |
75416 KB |
Output is correct |
20 |
Correct |
353 ms |
47784 KB |
Output is correct |
21 |
Correct |
28 ms |
14528 KB |
Output is correct |
22 |
Correct |
581 ms |
71604 KB |
Output is correct |
23 |
Correct |
557 ms |
70780 KB |
Output is correct |
24 |
Correct |
452 ms |
57412 KB |
Output is correct |
25 |
Correct |
559 ms |
64352 KB |
Output is correct |
26 |
Correct |
391 ms |
56656 KB |
Output is correct |
27 |
Correct |
755 ms |
86000 KB |
Output is correct |
28 |
Correct |
661 ms |
86172 KB |
Output is correct |
29 |
Correct |
701 ms |
79996 KB |
Output is correct |
30 |
Correct |
391 ms |
49896 KB |
Output is correct |
31 |
Correct |
610 ms |
76996 KB |
Output is correct |
32 |
Correct |
238 ms |
48592 KB |
Output is correct |
33 |
Correct |
286 ms |
53736 KB |
Output is correct |
34 |
Correct |
307 ms |
54188 KB |
Output is correct |
35 |
Correct |
336 ms |
57216 KB |
Output is correct |
36 |
Correct |
321 ms |
50856 KB |
Output is correct |
37 |
Correct |
256 ms |
45664 KB |
Output is correct |
38 |
Correct |
284 ms |
58880 KB |
Output is correct |
39 |
Correct |
332 ms |
60732 KB |
Output is correct |
40 |
Correct |
267 ms |
53524 KB |
Output is correct |
41 |
Correct |
283 ms |
48296 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
2 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4616 KB |
Output is correct |
6 |
Correct |
1 ms |
4700 KB |
Output is correct |
7 |
Correct |
1 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 |
2 ms |
4700 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
12 |
Correct |
1 ms |
4440 KB |
Output is correct |
13 |
Correct |
2 ms |
4444 KB |
Output is correct |
14 |
Correct |
2 ms |
4444 KB |
Output is correct |
15 |
Correct |
1 ms |
4444 KB |
Output is correct |
16 |
Correct |
1 ms |
4612 KB |
Output is correct |
17 |
Correct |
1 ms |
4532 KB |
Output is correct |
18 |
Correct |
1 ms |
4440 KB |
Output is correct |
19 |
Correct |
1 ms |
4444 KB |
Output is correct |
20 |
Correct |
464 ms |
84576 KB |
Output is correct |
21 |
Correct |
581 ms |
98492 KB |
Output is correct |
22 |
Correct |
358 ms |
86716 KB |
Output is correct |
23 |
Correct |
332 ms |
78268 KB |
Output is correct |
24 |
Correct |
362 ms |
87036 KB |
Output is correct |
25 |
Correct |
620 ms |
106428 KB |
Output is correct |
26 |
Correct |
436 ms |
82764 KB |
Output is correct |
27 |
Correct |
822 ms |
126904 KB |
Output is correct |
28 |
Correct |
296 ms |
50440 KB |
Output is correct |
29 |
Correct |
475 ms |
49836 KB |
Output is correct |
30 |
Correct |
783 ms |
78628 KB |
Output is correct |
31 |
Correct |
186 ms |
41036 KB |
Output is correct |
32 |
Correct |
179 ms |
41444 KB |
Output is correct |
33 |
Correct |
161 ms |
42180 KB |
Output is correct |
34 |
Correct |
180 ms |
40824 KB |
Output is correct |
35 |
Correct |
200 ms |
45840 KB |
Output is correct |
36 |
Correct |
8 ms |
6336 KB |
Output is correct |
37 |
Correct |
86 ms |
22984 KB |
Output is correct |
38 |
Correct |
285 ms |
47892 KB |
Output is correct |
39 |
Correct |
351 ms |
61804 KB |
Output is correct |
40 |
Correct |
365 ms |
64392 KB |
Output is correct |
41 |
Correct |
274 ms |
55976 KB |
Output is correct |
42 |
Correct |
359 ms |
51440 KB |
Output is correct |
43 |
Correct |
104 ms |
15252 KB |
Output is correct |
44 |
Correct |
628 ms |
65936 KB |
Output is correct |
45 |
Correct |
678 ms |
70296 KB |
Output is correct |
46 |
Correct |
847 ms |
83692 KB |
Output is correct |
47 |
Correct |
838 ms |
87708 KB |
Output is correct |
48 |
Correct |
691 ms |
83360 KB |
Output is correct |
49 |
Correct |
414 ms |
50344 KB |
Output is correct |
50 |
Correct |
407 ms |
47532 KB |
Output is correct |
51 |
Correct |
598 ms |
78156 KB |
Output is correct |
52 |
Correct |
239 ms |
52656 KB |
Output is correct |
53 |
Correct |
15 ms |
9424 KB |
Output is correct |
54 |
Correct |
582 ms |
70296 KB |
Output is correct |
55 |
Correct |
584 ms |
81052 KB |
Output is correct |
56 |
Correct |
691 ms |
85656 KB |
Output is correct |
57 |
Correct |
523 ms |
75596 KB |
Output is correct |
58 |
Correct |
499 ms |
73884 KB |
Output is correct |
59 |
Correct |
629 ms |
89332 KB |
Output is correct |
60 |
Correct |
489 ms |
74564 KB |
Output is correct |
61 |
Correct |
505 ms |
75416 KB |
Output is correct |
62 |
Correct |
353 ms |
47784 KB |
Output is correct |
63 |
Correct |
28 ms |
14528 KB |
Output is correct |
64 |
Correct |
581 ms |
71604 KB |
Output is correct |
65 |
Correct |
557 ms |
70780 KB |
Output is correct |
66 |
Correct |
452 ms |
57412 KB |
Output is correct |
67 |
Correct |
559 ms |
64352 KB |
Output is correct |
68 |
Correct |
391 ms |
56656 KB |
Output is correct |
69 |
Correct |
755 ms |
86000 KB |
Output is correct |
70 |
Correct |
661 ms |
86172 KB |
Output is correct |
71 |
Correct |
701 ms |
79996 KB |
Output is correct |
72 |
Correct |
391 ms |
49896 KB |
Output is correct |
73 |
Correct |
610 ms |
76996 KB |
Output is correct |
74 |
Correct |
238 ms |
48592 KB |
Output is correct |
75 |
Correct |
286 ms |
53736 KB |
Output is correct |
76 |
Correct |
307 ms |
54188 KB |
Output is correct |
77 |
Correct |
336 ms |
57216 KB |
Output is correct |
78 |
Correct |
321 ms |
50856 KB |
Output is correct |
79 |
Correct |
256 ms |
45664 KB |
Output is correct |
80 |
Correct |
284 ms |
58880 KB |
Output is correct |
81 |
Correct |
332 ms |
60732 KB |
Output is correct |
82 |
Correct |
267 ms |
53524 KB |
Output is correct |
83 |
Correct |
283 ms |
48296 KB |
Output is correct |
84 |
Correct |
42 ms |
13776 KB |
Output is correct |
85 |
Runtime error |
1321 ms |
1048576 KB |
Execution killed with signal 9 |
86 |
Halted |
0 ms |
0 KB |
- |