#include "walk.h"
#include<bits/stdc++.h>
#define ff first
#define ss second
#define all(x) x.begin(),x.end()
using namespace std;
using ll = long long;
using pii = pair<int,int>;
using pll = pair<ll,ll>;
template<typename T>
using matrix = vector<vector<T>>;
long long min_distance(std::vector<int> x, std::vector<int> h, std::vector<int> l, std::vector<int> r, std::vector<int> y, int from, int to) {
int n = x.size();
int m = l.size();
bool ok = 1;
for(int i = 1; i < n; i++){
ok&=h[i] == h[i-1];
}
if(!ok){
int nodes = n;
matrix<pii> lines;
for(int i = 0; i < n; i++)
lines.push_back({{0,i}});
set<int> s;
vector<int> obuild(n), oline(m);
iota(all(obuild),0);
iota(all(oline),0);
sort(all(obuild),[&](int a, int b){
return h[a] > h[b];
});
sort(all(oline),[&](int a, int b){
return y[a] > y[b];
});
int ptr = 0;
for(int i : oline){
while(ptr < n && h[obuild[ptr]] >= y[i])
s.insert(obuild[ptr]), ptr++;
vector<int> adj;
auto it = s.lower_bound(l[i]);
while(it!=s.end() && *it <= r[i])
adj.push_back(*it), it++;
vector<pii> hor;
for(int i = 0; i < adj.size(); i++){
hor.push_back({x[adj[i]],nodes+i});
}
lines.push_back(hor);
for(int j = 0; j < adj.size(); j++){
//cout << nodes+j << " = " << "(" << x[adj[j]] << ',' << y[i] << ")\n";
lines[adj[j]].push_back({y[i],nodes+j});
}
nodes+=adj.size();
}
matrix<pii> g(nodes);
for(auto& i : lines){
sort(all(i));
for(int j = 1; j < i.size(); j++){
int peso = i[j].ff-i[j-1].ff;
g[i[j-1].ss].push_back({i[j].ss,peso});
g[i[j].ss].push_back({i[j-1].ss,peso});
}
}
vector<ll> dist(nodes,1ll<<60);
dist[from] = 0;
priority_queue<pll,vector<pll>,greater<pll>> pq;
pq.push({0,from});
while(!pq.empty()){
int cur = pq.top().ss;
int curp = pq.top().ff;
pq.pop();
if(curp > dist[cur])
continue;
for(auto [viz,peso] : g[cur]){
if(dist[viz] > dist[cur]+peso){
dist[viz] = dist[cur]+peso;
pq.push({dist[viz],viz});
}
}
}
// for(int i = 0; i < nodes; i++)
// cout << "->" << i << ": " << dist[i] << '\n';
if(dist[to] == (1ll<<60))
return -1;
return dist[to];
}
matrix<pii> g(m+2);
// g[m] -> 0, g[m+1]->n-1
matrix<pii> op(n+1);
for(int i = 0; i < m; i++){
op[l[i]].push_back({1,i});
op[r[i]+1].push_back({0,i});
if(l[i] == 0)
g[m].push_back({i,y[i]}), g[i].push_back({m,y[i]});
if(r[i] == n-1)
g[m+1].push_back({i,y[i]}), g[i].push_back({m+1,y[i]});
}
set<pii> s;
for(int i = 0; i < n; i++){
for(auto [type,id]: op[i]){
if(type){
auto it = s.lower_bound({y[id],id});
if(it != s.end()){
g[it->ss].push_back({id,abs(y[id]-it->ff)});
g[id].push_back({it->ss,abs(y[id]-it->ff)});
}
if(it!=s.begin()){
it--;
g[it->ss].push_back({id,abs(y[id]-it->ff)});
g[id].push_back({it->ss,abs(y[id]-it->ff)});
}
s.insert({y[id],id});
} else {
s.erase({y[id],id});
}
}
}
vector<ll> dist(m+2,1ll<<60);
dist[m] = 0;
priority_queue<pll,vector<pll>,greater<pll>> pq;
pq.push({0,m});
while(!pq.empty()){
int cur = pq.top().ss;
int curp = pq.top().ff;
pq.pop();
if(curp > dist[cur])
continue;
for(auto [viz,peso] : g[cur]){
if(dist[viz] > dist[cur]+peso){
dist[viz] = dist[cur]+peso;
pq.push({dist[viz],viz});
}
}
}
// for(int i = 0; i < nodes; i++)
// cout << "->" << i << ": " << dist[i] << '\n';
if(dist[m+1] == (1ll<<60))
return -1;
return dist[m+1]+x.back()-x[0];
}
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:53:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
53 | for(int i = 0; i < adj.size(); i++){
| ~~^~~~~~~~~~~~
walk.cpp:58:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
58 | for(int j = 0; j < adj.size(); j++){
| ~~^~~~~~~~~~~~
walk.cpp:70:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int>, std::allocator<std::pair<int, int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
70 | for(int j = 1; j < i.size(); j++){
| ~~^~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 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 |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
658 ms |
82060 KB |
Output is correct |
4 |
Correct |
859 ms |
97668 KB |
Output is correct |
5 |
Correct |
506 ms |
84668 KB |
Output is correct |
6 |
Correct |
436 ms |
76700 KB |
Output is correct |
7 |
Correct |
516 ms |
84836 KB |
Output is correct |
8 |
Correct |
758 ms |
102248 KB |
Output is correct |
9 |
Correct |
615 ms |
83056 KB |
Output is correct |
10 |
Correct |
1008 ms |
129344 KB |
Output is correct |
11 |
Correct |
410 ms |
49780 KB |
Output is correct |
12 |
Correct |
346 ms |
45744 KB |
Output is correct |
13 |
Correct |
1002 ms |
115788 KB |
Output is correct |
14 |
Correct |
218 ms |
42704 KB |
Output is correct |
15 |
Correct |
272 ms |
45904 KB |
Output is correct |
16 |
Correct |
244 ms |
44788 KB |
Output is correct |
17 |
Correct |
277 ms |
43412 KB |
Output is correct |
18 |
Correct |
134 ms |
39932 KB |
Output is correct |
19 |
Correct |
8 ms |
2416 KB |
Output is correct |
20 |
Correct |
86 ms |
21976 KB |
Output is correct |
21 |
Correct |
251 ms |
39804 KB |
Output is correct |
22 |
Correct |
290 ms |
46088 KB |
Output is correct |
23 |
Correct |
244 ms |
53268 KB |
Output is correct |
24 |
Correct |
312 ms |
44700 KB |
Output is correct |
25 |
Correct |
274 ms |
43008 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
46 ms |
7324 KB |
Output is correct |
2 |
Correct |
129 ms |
13548 KB |
Output is correct |
3 |
Correct |
150 ms |
14252 KB |
Output is correct |
4 |
Correct |
180 ms |
18680 KB |
Output is correct |
5 |
Correct |
246 ms |
22972 KB |
Output is correct |
6 |
Correct |
256 ms |
21520 KB |
Output is correct |
7 |
Correct |
81 ms |
12096 KB |
Output is correct |
8 |
Incorrect |
103 ms |
16456 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
46 ms |
7324 KB |
Output is correct |
2 |
Correct |
129 ms |
13548 KB |
Output is correct |
3 |
Correct |
150 ms |
14252 KB |
Output is correct |
4 |
Correct |
180 ms |
18680 KB |
Output is correct |
5 |
Correct |
246 ms |
22972 KB |
Output is correct |
6 |
Correct |
256 ms |
21520 KB |
Output is correct |
7 |
Correct |
81 ms |
12096 KB |
Output is correct |
8 |
Incorrect |
103 ms |
16456 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 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 |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 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 |
658 ms |
82060 KB |
Output is correct |
21 |
Correct |
859 ms |
97668 KB |
Output is correct |
22 |
Correct |
506 ms |
84668 KB |
Output is correct |
23 |
Correct |
436 ms |
76700 KB |
Output is correct |
24 |
Correct |
516 ms |
84836 KB |
Output is correct |
25 |
Correct |
758 ms |
102248 KB |
Output is correct |
26 |
Correct |
615 ms |
83056 KB |
Output is correct |
27 |
Correct |
1008 ms |
129344 KB |
Output is correct |
28 |
Correct |
410 ms |
49780 KB |
Output is correct |
29 |
Correct |
346 ms |
45744 KB |
Output is correct |
30 |
Correct |
1002 ms |
115788 KB |
Output is correct |
31 |
Correct |
218 ms |
42704 KB |
Output is correct |
32 |
Correct |
272 ms |
45904 KB |
Output is correct |
33 |
Correct |
244 ms |
44788 KB |
Output is correct |
34 |
Correct |
277 ms |
43412 KB |
Output is correct |
35 |
Correct |
134 ms |
39932 KB |
Output is correct |
36 |
Correct |
8 ms |
2416 KB |
Output is correct |
37 |
Correct |
86 ms |
21976 KB |
Output is correct |
38 |
Correct |
251 ms |
39804 KB |
Output is correct |
39 |
Correct |
290 ms |
46088 KB |
Output is correct |
40 |
Correct |
244 ms |
53268 KB |
Output is correct |
41 |
Correct |
312 ms |
44700 KB |
Output is correct |
42 |
Correct |
274 ms |
43008 KB |
Output is correct |
43 |
Correct |
46 ms |
7324 KB |
Output is correct |
44 |
Correct |
129 ms |
13548 KB |
Output is correct |
45 |
Correct |
150 ms |
14252 KB |
Output is correct |
46 |
Correct |
180 ms |
18680 KB |
Output is correct |
47 |
Correct |
246 ms |
22972 KB |
Output is correct |
48 |
Correct |
256 ms |
21520 KB |
Output is correct |
49 |
Correct |
81 ms |
12096 KB |
Output is correct |
50 |
Incorrect |
103 ms |
16456 KB |
Output isn't correct |
51 |
Halted |
0 ms |
0 KB |
- |