#include <bits/stdc++.h>
//#pragma GCC optimize ("O3")
#define all(x) (x).begin(),(x).end()
using namespace std;
using ll = long long;
//#define int ll
#define sz(x) ((int)(x).size())
using pii = pair<ll,ll>;
//using tii = tuple<int,int,int>;
struct tii {
int first, second, ind;
tii(int x, int y, int i) : first(x), second(y), ind(i) {;}
tii() : first(-1), second(-1), ind(-1) {;}
bool operator == (const tii& x) const {
return first == x.first && second == x.second;
}
};
const int nmax = 1e5 + 5;
const ll inf = 1e18 + 5;
map<int, vector<pii>, greater<int>> segm;
map<int, vector<int>, greater<int>> columns;
vector<pii> buildings;
void repair(vector<pii> &v) {
sort(all(v));
int last = 0;
for(int i = 1; i < sz(v); i++) {
if(v[last].second == v[i].first) {
v[last].second = v[i].second;
v[i].second = -1;
}
else
last = i;
}
v.resize(distance(v.begin(), stable_partition(all(v), [](auto x) { return x.second != -1; })));
}
namespace PointsG {
vector<tii> pts;
vector<vector<pii>> g;
void addedge(int x, int y, int w) {
g[x].emplace_back(y, w);
g[y].emplace_back(x, w);
}
void add(int x, int y) {
//cerr << "+ " << x << ' ' << y< '\n';
pts.emplace_back(x, y, sz(pts));
}
void init() {
sort(all(pts), [](auto a, auto b) { return pii{a.first, a.second} < pii{b.first, b.second}; });
pts.resize(distance(pts.begin(), unique(all(pts))));
for(int i = 0; i < sz(pts); i++)
pts[i].ind = i;
g.resize(sz(pts));
for(int l = 0; l < sz(pts);) {
int r = l;
while(r < sz(pts) && pts[r].first == pts[l].first)
r++;
//cerr << l << ' ' << r << ' ' << pts[l].first << '\n';
for(int j = l + 1; j < r; j++)
if(pts[j].second <= buildings[pts[l].first].second)
addedge(pts[j].ind, pts[j - 1].ind, pts[j].second - pts[j - 1].second);
l = r;
}
sort(all(pts), [](auto a, auto b) { return pii{a.second, a.first} < pii{b.second, b.first}; });
for(int l = 0; l < sz(pts);) {
int r = l;
while(r < sz(pts) && pts[r].second == pts[l].second)
r++;
auto &v = segm[pts[l].second];
int ptr = 0;
for(int j = l + 1; j < r; j++) {
while(ptr < sz(v) && v[ptr].second < pts[j].first) ptr++;
if(ptr == sz(v)) break;
if(v[ptr].first > pts[j].first) continue;
if(v[ptr].first <= pts[j - 1].first) {
addedge(pts[j].ind, pts[j - 1].ind, buildings[pts[j].first].first - buildings[pts[j - 1].first].first);
}
}
l = r;
}
}
ll start(int S = -1, int T = -1) {
//sort(all(pts), [](auto a, auto b) { return pii{a.first, a.second} < pii{b.first, b.second}; });
for(auto [a, b, i] : pts) {
if(b == 0) {
if(S == -1) S = i;
else { T = i; break; }
}
}
//cerr << sz(pts) << '\n';
vector<ll> arriv(sz(pts), inf);
priority_queue<pii, vector<pii>, greater<pii>> heap;
heap.emplace(0, S);
arriv[S] = 0;
while(!heap.empty()) {
auto [c, node] = heap.top();
heap.pop();
if(c > arriv[node]) continue;
for(auto [x, e] : g[node]) {
if(e + c < arriv[x]) {
arriv[x] = e + c;
heap.emplace(e + c, x);
}
}
}
return arriv[T];
}
}
namespace Beats {
int r[nmax];
int atr[nmax];
int ggprev[nmax], ggnext[nmax];
set<int> segm;
set<int> availible;
//#warning Gamble
int gnext(int p) { auto it = availible.upper_bound(p); return (it == availible.end()? nmax - 1 : *it); }
int gprev(int p) { auto it = availible.lower_bound(p); return (it == availible.begin()? -1 : *prev(it)); }
void split(int p, int y, bool add = 1) { // inutil?
//return;
if(add)
PointsG::add(p, y);
if(add == 0) {
availible.insert(p);
if(availible.size() == 0) {
ggprev[p] = -1;
ggnext[p] = nmax - 1;
}
else {
ggnext[p] = gnext(p);
ggprev[p] = gprev(p);
if(ggprev[p] != -1)
ggnext[ggprev[p]] = p;
ggprev[ggnext[p]] = p;
}
//return;
}
int pp1 = ggnext[p], pm1 = ggprev[p];
//int pp1 = p + 1, pm1 = p - 1;
auto it = segm.upper_bound(p);
if(it != segm.begin() && !segm.empty()) {
it = prev(it);
int l = *it;
//cerr << y << ": " << l << ' ' << p << ' ' << r[l] << ' ' << add << '\n';
if(add && l <= p && p <= r[l])
PointsG::add(p, atr[l]);
if(l < p && p < r[l]) { // p nu se afla printre aia
segm.insert(pp1);
r[pp1] = r[l];
r[l] = pm1;
atr[pp1] = atr[l];
}
else if(l == p && p == r[l]) {
segm.erase(p);
}
else if(l == p) {
atr[pp1] = atr[l];
segm.insert(pp1);
r[pp1] = r[l];
segm.erase(l);
}
else if(p == r[l]) r[l] = ggprev[r[l]];
}
}
void color(int l, int R, int y) {
//cerr << "\t" << l << ' ' << R << ' ' << y << '\n';
split(l, y, 1);
split(R, y, 1);
auto it = segm.lower_bound(l);
while(it != segm.end() && *it <= R) {
//cerr << '\t' << *it << ' ' << r[*it] << '\n';
PointsG::add(*it, y);
PointsG::add(*it, atr[*it]);
PointsG::add(r[*it], y);
PointsG::add(r[*it], atr[*it]);
segm.erase(it);
it = segm.lower_bound(l);
}
segm.insert(l);
r[l] = R;
atr[l] = y;
return;
}
}
set<int, greater<int>> appearances;
void initpoints() {
for(auto y : appearances) {
for(auto a : columns[y])
Beats::split(a, y, 0);
repair(segm[y]);
for(auto [l, r] : segm[y])
Beats::color(l, r, y);
}
return;
}
long long min_distance(vector<signed> x, vector<signed> h, vector<signed> l, vector<signed> r, vector<signed> y, signed s, signed g) {
//cerr << s << ' ' << g << '\n';
PointsG::pts.reserve(sz(l) * 10);
PointsG::g.reserve(sz(l) * 10);
PointsG::add(s, 0);
PointsG::add(g, 0);
for(int i = 0; i < sz(l); i++) {
appearances.emplace(y[i]);
segm[y[i]].emplace_back(l[i], r[i]);
if(l[i] <= s && s <= r[i])
PointsG::add(s, y[i]);
if(l[i] <= g && g <= r[i])
PointsG::add(g, y[i]);
}
buildings.reserve(sz(x));
for(int i = 0; i < sz(x); i++) {
appearances.emplace(h[i]);
buildings.emplace_back(x[i], h[i]);
columns[h[i]].emplace_back(i);
}
initpoints();
PointsG::init();
auto a = PointsG::start();
if(a == inf) return -1;
return a;
}
#undef int
/**
Vom spune că toamna a venit... foarte trist -
-- George Bacovia, Scântei galbene
*/
Compilation message
walk.cpp: In function 'll PointsG::start(int, int)':
walk.cpp:97:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
97 | for(auto [a, b, i] : pts) {
| ^
walk.cpp:109:12: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
109 | auto [c, node] = heap.top();
| ^
walk.cpp:112:16: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
112 | for(auto [x, e] : g[node]) {
| ^
walk.cpp: In function 'void initpoints()':
walk.cpp:211:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
211 | for(auto [l, r] : segm[y])
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
797 ms |
115136 KB |
Output is correct |
4 |
Correct |
1181 ms |
143392 KB |
Output is correct |
5 |
Correct |
790 ms |
119952 KB |
Output is correct |
6 |
Correct |
648 ms |
111064 KB |
Output is correct |
7 |
Correct |
686 ms |
119524 KB |
Output is correct |
8 |
Correct |
923 ms |
130204 KB |
Output is correct |
9 |
Correct |
962 ms |
128456 KB |
Output is correct |
10 |
Correct |
1350 ms |
165564 KB |
Output is correct |
11 |
Correct |
701 ms |
91640 KB |
Output is correct |
12 |
Correct |
782 ms |
86108 KB |
Output is correct |
13 |
Correct |
1208 ms |
160536 KB |
Output is correct |
14 |
Correct |
480 ms |
81448 KB |
Output is correct |
15 |
Correct |
353 ms |
49188 KB |
Output is correct |
16 |
Correct |
229 ms |
43416 KB |
Output is correct |
17 |
Correct |
222 ms |
40644 KB |
Output is correct |
18 |
Correct |
368 ms |
68940 KB |
Output is correct |
19 |
Correct |
13 ms |
3912 KB |
Output is correct |
20 |
Correct |
166 ms |
36432 KB |
Output is correct |
21 |
Correct |
199 ms |
39268 KB |
Output is correct |
22 |
Correct |
212 ms |
40236 KB |
Output is correct |
23 |
Correct |
371 ms |
61192 KB |
Output is correct |
24 |
Correct |
200 ms |
41172 KB |
Output is correct |
25 |
Correct |
235 ms |
39884 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
3284 KB |
Output is correct |
2 |
Correct |
1198 ms |
182672 KB |
Output is correct |
3 |
Correct |
1486 ms |
202204 KB |
Output is correct |
4 |
Correct |
1573 ms |
215804 KB |
Output is correct |
5 |
Correct |
1643 ms |
216624 KB |
Output is correct |
6 |
Correct |
1475 ms |
201964 KB |
Output is correct |
7 |
Correct |
823 ms |
114904 KB |
Output is correct |
8 |
Correct |
454 ms |
58840 KB |
Output is correct |
9 |
Correct |
1347 ms |
187604 KB |
Output is correct |
10 |
Correct |
364 ms |
73996 KB |
Output is correct |
11 |
Correct |
34 ms |
5264 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
3284 KB |
Output is correct |
2 |
Correct |
1198 ms |
182672 KB |
Output is correct |
3 |
Correct |
1486 ms |
202204 KB |
Output is correct |
4 |
Correct |
1573 ms |
215804 KB |
Output is correct |
5 |
Correct |
1643 ms |
216624 KB |
Output is correct |
6 |
Correct |
1475 ms |
201964 KB |
Output is correct |
7 |
Correct |
823 ms |
114904 KB |
Output is correct |
8 |
Correct |
454 ms |
58840 KB |
Output is correct |
9 |
Correct |
1347 ms |
187604 KB |
Output is correct |
10 |
Correct |
364 ms |
73996 KB |
Output is correct |
11 |
Correct |
34 ms |
5264 KB |
Output is correct |
12 |
Correct |
1375 ms |
207796 KB |
Output is correct |
13 |
Correct |
1672 ms |
281720 KB |
Output is correct |
14 |
Correct |
2202 ms |
287248 KB |
Output is correct |
15 |
Correct |
992 ms |
123532 KB |
Output is correct |
16 |
Correct |
1526 ms |
200484 KB |
Output is correct |
17 |
Correct |
1657 ms |
220848 KB |
Output is correct |
18 |
Correct |
966 ms |
123696 KB |
Output is correct |
19 |
Correct |
1508 ms |
199504 KB |
Output is correct |
20 |
Correct |
1323 ms |
178956 KB |
Output is correct |
21 |
Correct |
487 ms |
70780 KB |
Output is correct |
22 |
Correct |
973 ms |
180104 KB |
Output is correct |
23 |
Correct |
836 ms |
150340 KB |
Output is correct |
24 |
Correct |
626 ms |
92704 KB |
Output is correct |
25 |
Correct |
860 ms |
143708 KB |
Output is correct |
26 |
Correct |
518 ms |
68768 KB |
Output is correct |
27 |
Correct |
2122 ms |
274920 KB |
Output is correct |
28 |
Correct |
1747 ms |
278272 KB |
Output is correct |
29 |
Correct |
2075 ms |
272104 KB |
Output is correct |
30 |
Correct |
1297 ms |
173720 KB |
Output is correct |
31 |
Correct |
2028 ms |
256100 KB |
Output is correct |
32 |
Correct |
277 ms |
55144 KB |
Output is correct |
33 |
Correct |
293 ms |
54888 KB |
Output is correct |
34 |
Correct |
373 ms |
64360 KB |
Output is correct |
35 |
Correct |
630 ms |
91616 KB |
Output is correct |
36 |
Correct |
363 ms |
58068 KB |
Output is correct |
37 |
Correct |
197 ms |
39304 KB |
Output is correct |
38 |
Correct |
199 ms |
40260 KB |
Output is correct |
39 |
Correct |
397 ms |
61128 KB |
Output is correct |
40 |
Correct |
201 ms |
41224 KB |
Output is correct |
41 |
Correct |
221 ms |
39996 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
797 ms |
115136 KB |
Output is correct |
21 |
Correct |
1181 ms |
143392 KB |
Output is correct |
22 |
Correct |
790 ms |
119952 KB |
Output is correct |
23 |
Correct |
648 ms |
111064 KB |
Output is correct |
24 |
Correct |
686 ms |
119524 KB |
Output is correct |
25 |
Correct |
923 ms |
130204 KB |
Output is correct |
26 |
Correct |
962 ms |
128456 KB |
Output is correct |
27 |
Correct |
1350 ms |
165564 KB |
Output is correct |
28 |
Correct |
701 ms |
91640 KB |
Output is correct |
29 |
Correct |
782 ms |
86108 KB |
Output is correct |
30 |
Correct |
1208 ms |
160536 KB |
Output is correct |
31 |
Correct |
480 ms |
81448 KB |
Output is correct |
32 |
Correct |
353 ms |
49188 KB |
Output is correct |
33 |
Correct |
229 ms |
43416 KB |
Output is correct |
34 |
Correct |
222 ms |
40644 KB |
Output is correct |
35 |
Correct |
368 ms |
68940 KB |
Output is correct |
36 |
Correct |
13 ms |
3912 KB |
Output is correct |
37 |
Correct |
166 ms |
36432 KB |
Output is correct |
38 |
Correct |
199 ms |
39268 KB |
Output is correct |
39 |
Correct |
212 ms |
40236 KB |
Output is correct |
40 |
Correct |
371 ms |
61192 KB |
Output is correct |
41 |
Correct |
200 ms |
41172 KB |
Output is correct |
42 |
Correct |
235 ms |
39884 KB |
Output is correct |
43 |
Correct |
19 ms |
3284 KB |
Output is correct |
44 |
Correct |
1198 ms |
182672 KB |
Output is correct |
45 |
Correct |
1486 ms |
202204 KB |
Output is correct |
46 |
Correct |
1573 ms |
215804 KB |
Output is correct |
47 |
Correct |
1643 ms |
216624 KB |
Output is correct |
48 |
Correct |
1475 ms |
201964 KB |
Output is correct |
49 |
Correct |
823 ms |
114904 KB |
Output is correct |
50 |
Correct |
454 ms |
58840 KB |
Output is correct |
51 |
Correct |
1347 ms |
187604 KB |
Output is correct |
52 |
Correct |
364 ms |
73996 KB |
Output is correct |
53 |
Correct |
34 ms |
5264 KB |
Output is correct |
54 |
Correct |
1375 ms |
207796 KB |
Output is correct |
55 |
Correct |
1672 ms |
281720 KB |
Output is correct |
56 |
Correct |
2202 ms |
287248 KB |
Output is correct |
57 |
Correct |
992 ms |
123532 KB |
Output is correct |
58 |
Correct |
1526 ms |
200484 KB |
Output is correct |
59 |
Correct |
1657 ms |
220848 KB |
Output is correct |
60 |
Correct |
966 ms |
123696 KB |
Output is correct |
61 |
Correct |
1508 ms |
199504 KB |
Output is correct |
62 |
Correct |
1323 ms |
178956 KB |
Output is correct |
63 |
Correct |
487 ms |
70780 KB |
Output is correct |
64 |
Correct |
973 ms |
180104 KB |
Output is correct |
65 |
Correct |
836 ms |
150340 KB |
Output is correct |
66 |
Correct |
626 ms |
92704 KB |
Output is correct |
67 |
Correct |
860 ms |
143708 KB |
Output is correct |
68 |
Correct |
518 ms |
68768 KB |
Output is correct |
69 |
Correct |
2122 ms |
274920 KB |
Output is correct |
70 |
Correct |
1747 ms |
278272 KB |
Output is correct |
71 |
Correct |
2075 ms |
272104 KB |
Output is correct |
72 |
Correct |
1297 ms |
173720 KB |
Output is correct |
73 |
Correct |
2028 ms |
256100 KB |
Output is correct |
74 |
Correct |
277 ms |
55144 KB |
Output is correct |
75 |
Correct |
293 ms |
54888 KB |
Output is correct |
76 |
Correct |
373 ms |
64360 KB |
Output is correct |
77 |
Correct |
630 ms |
91616 KB |
Output is correct |
78 |
Correct |
363 ms |
58068 KB |
Output is correct |
79 |
Correct |
197 ms |
39304 KB |
Output is correct |
80 |
Correct |
199 ms |
40260 KB |
Output is correct |
81 |
Correct |
397 ms |
61128 KB |
Output is correct |
82 |
Correct |
201 ms |
41224 KB |
Output is correct |
83 |
Correct |
221 ms |
39996 KB |
Output is correct |
84 |
Correct |
29 ms |
6308 KB |
Output is correct |
85 |
Correct |
1419 ms |
211060 KB |
Output is correct |
86 |
Correct |
2259 ms |
301140 KB |
Output is correct |
87 |
Correct |
101 ms |
20844 KB |
Output is correct |
88 |
Correct |
541 ms |
73456 KB |
Output is correct |
89 |
Correct |
92 ms |
20864 KB |
Output is correct |
90 |
Correct |
18 ms |
4544 KB |
Output is correct |
91 |
Correct |
4 ms |
1108 KB |
Output is correct |
92 |
Correct |
68 ms |
14060 KB |
Output is correct |
93 |
Correct |
1063 ms |
148044 KB |
Output is correct |
94 |
Correct |
457 ms |
70464 KB |
Output is correct |
95 |
Correct |
1063 ms |
191776 KB |
Output is correct |
96 |
Correct |
861 ms |
155092 KB |
Output is correct |
97 |
Correct |
593 ms |
99672 KB |
Output is correct |
98 |
Correct |
859 ms |
147220 KB |
Output is correct |
99 |
Correct |
2328 ms |
300460 KB |
Output is correct |
100 |
Correct |
2176 ms |
288504 KB |
Output is correct |
101 |
Correct |
2205 ms |
282692 KB |
Output is correct |
102 |
Correct |
1294 ms |
177496 KB |
Output is correct |
103 |
Correct |
288 ms |
58156 KB |
Output is correct |
104 |
Correct |
314 ms |
58084 KB |
Output is correct |
105 |
Correct |
380 ms |
67396 KB |
Output is correct |
106 |
Correct |
365 ms |
67132 KB |
Output is correct |
107 |
Correct |
369 ms |
58388 KB |
Output is correct |
108 |
Correct |
122 ms |
21600 KB |
Output is correct |
109 |
Correct |
1715 ms |
223868 KB |
Output is correct |
110 |
Correct |
1431 ms |
260680 KB |
Output is correct |
111 |
Correct |
1434 ms |
259512 KB |
Output is correct |
112 |
Correct |
505 ms |
84764 KB |
Output is correct |
113 |
Correct |
479 ms |
73036 KB |
Output is correct |