#include "walk.h"
#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template<typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template<typename T, typename U> using ordered_map = tree<T, U, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
typedef long long ll;
typedef long double ld;
typedef double db;
typedef string str;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<pii> vpii;
typedef vector<pll> vpll;
typedef vector<str> vstr;
#define FOR(i,j,k,in) for(int i=(j); i < (k);i+=in)
#define FORD(i,j,k,in) for(int i=(j); i >=(k);i-=in)
#define REP(i,b) FOR(i,0,b,1)
#define REPD(i,b) FORD(i,b,0,1)
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define all(x) begin(x), end(x)
#define rsz resize
#define MANY_TESTS int tcase; cin >> tcase; while(tcase--)
const double EPS = 1e-9;
const int MOD = 1e9+7; // 998244353;
const ll INFF = 1e18;
const int INF = 1e9;
const ld PI = acos((ld)-1);
const vi dy = {1, 0, -1, 0, -1, 1, 1, -1};
const vi dx = {0, 1, 0, -1, -1, 1, -1, 1};
#ifdef DEBUG
#define DBG if(1)
#else
#define DBG if(0)
#endif
#define dbg(x) cout << "(" << #x << " : " << x << ")" << endl;
// ostreams
template <class T, class U>
ostream& operator<<(ostream& out, const pair<T, U> &par) {out << "[" << par.first << ";" << par.second << "]"; return out;}
template <class T>
ostream& operator<<(ostream& out, const set<T> &cont) { out << "{"; for( const auto &x:cont) out << x << ", "; out << "}"; return out; }
template <class T, class U>
ostream& operator<<(ostream& out, const map<T, U> &cont) {out << "{"; for( const auto &x:cont) out << x << ", "; out << "}"; return out; }
template<class T>
ostream& operator<<(ostream& out, const vector<T> &v){ out << "["; REP(i, v.size()) out << v[i] << ", "; out << "]"; return out;}
// istreams
template<class T>
istream& operator>>(istream& in, vector<T> &v){ for(auto &x : v) in >> x; return in; }
template<class T, class U>
istream& operator>>(istream& in, pair<T, U> &p){ in >> p.ff >> p.ss; return in; }
//searches
template<typename T, typename U>
T bsl(T lo, T hi, U f){ hi++; T mid; while(lo < hi){ mid = (lo + hi)/2; f(mid) ? hi = mid : lo = mid+1; } return lo; }
template<typename U>
double bsld(double lo, double hi, U f, double p = 1e-9){ int r = 3 + (int)log2((hi - lo)/p); double mid; while(r--){ mid = (lo + hi)/2; f(mid) ? hi = mid : lo = mid; } return (lo + hi)/2; }
template<typename T, typename U>
T bsh(T lo, T hi, U f){ lo--; T mid; while(lo < hi){ mid = (lo + hi + 1)/2; f(mid) ? lo = mid : hi = mid-1; } return lo; }
template<typename U>
double bshd(double lo, double hi, U f, double p = 1e-9){ int r = 3+(int)log2((hi - lo)/p); double mid; while(r--){ mid = (lo + hi)/2; f(mid) ? lo = mid : hi = mid; } return (lo + hi)/2; }
// some more utility functions
template<typename T>
pair<T, int> get_min(vector<T> &v){ typename vector<T> :: iterator it = min_element(v.begin(), v.end()); return mp(*it, it - v.begin());}
template<typename T>
pair<T, int> get_max(vector<T> &v){ typename vector<T> :: iterator it = max_element(v.begin(), v.end()); return mp(*it, it - v.begin());}
template<typename T> bool ckmin(T& a, const T& b){return b < a ? a = b , true : false;}
template<typename T> bool ckmax(T& a, const T& b){return b > a ? a = b, true : false;}
long long dijkstra(int s, int e, vector<vector<array<int, 2>>> &adj){
vector<ll> dist(adj.size(), -1);
priority_queue<pll, vector<pll>, greater<pll>> pq;
pq.push({0, s});
while(!pq.empty()){
while(!pq.empty() && dist[pq.top().ss] != -1) pq.pop();
if(pq.empty()) break;
int v = pq.top().ss;
dist[v] = pq.top().ff;
pq.pop();
for(auto x : adj[v]){
if(dist[x[0]] == -1){
pq.push({dist[v] + x[1], x[0]});
}
}
}
return dist[e];
}
long long solve3(vector<int> x, vector<int> h, vector<int> l, vector<int> r, vector<int> y, int s, int g){
int n = x.size(); int m = l.size();
int mx = 1000000;
vector<vector<array<int, 2>>> adj(mx);
set<array<int, 3>> open_edges;
vector<vi> starts(n);
vector<vi> ends(n);
vector<vi> points(n);
map<pair<int, int>, int> compression;
REP(i, m){
starts[l[i]].pb(i);
ends[r[i]].pb(i);
points[l[i]].pb(y[i]);
points[r[i]].pb(y[i]);
}
points[0].pb(0);
points[n - 1].pb(0);
int vid = 0;
REP(i, n){
sort(all(points[i]));
REP(j, ends[i].size()){
int id = ends[i][j];
open_edges.erase({y[id], l[id], r[id]});
}
REPD(j, (int)points[i].size() - 1){
int nxt = -1;
if(j != 0) nxt = max(nxt, points[i][j-1]);
auto it = open_edges.lower_bound({points[i][j], -INF, -INF});
if(it != open_edges.begin()){
it--;
if(nxt <= (*it)[0]){
if(compression.find(mp(i, (*it)[0])) == compression.end()){
compression[{i, (*it)[0]}] = vid++;
}
int r = (*it)[2];
if(compression.find(mp(r, (*it)[0])) == compression.end()){
compression[{r, (*it)[0]}] = vid++;
}
int s = compression[{i, (*it)[0]}];
int e = compression[{r, (*it)[0]}];
adj[s].pb({e, abs(x[i] - x[r])});
adj[e].pb({s, abs(x[i] - x[r])});
}
nxt = max(nxt, (*it)[0]);
}
if(compression.find({i, points[i][j]}) == compression.end()){
compression[{i, points[i][j]}] = vid++;
}
if(nxt == -1) continue;
if(compression.find({i, nxt}) == compression.end()){
compression[{i, nxt}] = vid++;
}
int s = compression[{i, points[i][j]}];
int e = compression[{i, nxt}];
adj[s].pb({e, abs(points[i][j] - nxt)});
adj[e].pb({s, abs(points[i][j] - nxt)});
}
REP(j, starts[i].size()){
int id = starts[i][j];
open_edges.insert({y[id], l[id], r[id]});
if(compression.find({r[id], y[id]}) == compression.end()){
compression[{r[id], y[id]}] = vid++;
}
if(compression.find({i, y[id]}) == compression.end()){
compression[{i, y[id]}] = vid++;
}
int s = compression[{i, y[id]}];
int e = compression[{r[id], y[id]}];
adj[s].pb({e, abs(x[i] - x[r[id]])});
adj[e].pb({s, abs(x[i] - x[r[id]])});
}
}
if(compression.find({s, 0}) == compression.end()){
compression[{s, 0}] = vid++;
}
if(compression.find({g, 0}) == compression.end()){
compression[{g, 0}] = vid++;
}
//dbg(vid)
/* for(auto x : compression) { */
/* dbg(mp(x.ff[0], x.ff[1])) */
/* } */
/* REP(i, 20){ */
/* for(auto x : adj[i]){ */
/* cout << i << " " << x[0] << " " << x[1] << "\n"; */
/* } */
/* } */
return dijkstra(compression[{s, 0}], compression[{g, 0}], adj);
}
long long solve_12(vector<int> x, vector<int> h, vector<int> l, vector<int> r, vector<int> y, int s, int g){
int n = x.size(); int m = l.size();
vector<vector<array<int, 2>>> adj(2000000);
map<array<int, 2>, int> compression;
int vid = 0;
auto _alloc = [&](array<int, 2> nw){
if(compression.find(nw) == compression.end()){
compression[nw] = vid++;
}
};
vector<vi> points(n);
points[s].pb(0); points[g].pb(0);
/* REP(i, m){ */ // for subtask 2, I have to alter this, so that I only traverse the places that I intersect
/* int prev = l[i]; */
/* points[prev].pb(y[i]); */
/* FOR(j, l[i] + 1, r[i] + 1, 1){ */
/* if(h[j] < y[i]) continue; */
/* _alloc({j, y[i]}); */
/* _alloc({prev, y[i]}); */
/* int s = compression[{j, y[i]}]; */
/* int e = compression[{prev, y[i]}]; */
/* adj[s].pb({e, abs(x[j] - x[prev])}); */
/* adj[e].pb({s, abs(x[j] - x[prev])}); */
/* prev = j; */
/* points[j].pb(y[i]); */
/* } */
/* } */
vector<array<int, 3>> edges;
REP(i, m) edges.pb({y[i], l[i], r[i]});
sort(all(edges));
set<array<int, 2>> buildings;
vector<array<int, 2>> bv;
REP(i, n){
buildings.insert({i, h[i]});
bv.pb({h[i], i});
}
sort(all(bv));
int pp = 0;
REP(i, edges.size()){
while(pp < bv.size() && bv[pp][0] < edges[i][0]){
buildings.erase({bv[pp][1], bv[pp][0]});
pp++;
}
int prev = -1;
set<array<int, 2>> :: iterator it = buildings.lower_bound({edges[i][1], -INF});
while(it != buildings.end() && (*it)[0] <= edges[i][2]){
points[(*it)[0]].pb(edges[i][0]);
int y = edges[i][0];
if(prev != -1){
_alloc({prev, y});
_alloc({(*it)[0], y});
int s = compression[{prev, y}];
int e = compression[{(*it)[0], y}];
int curr = (*it)[0];
adj[s].pb({e, abs(x[prev] - x[curr])});
adj[e].pb({s, abs(x[prev] - x[curr])});
}
prev = (*it)[0];
it++;
}
}
REP(i, n){
sort(all(points[i]));
reverse(all(points[i]));
REP(j, (int)points[i].size() - 1){
_alloc({i, points[i][j]});
_alloc({i, points[i][j + 1]});
int s = compression[{i, points[i][j]}];
int e = compression[{i, points[i][j + 1]}];
adj[s].pb({e, abs(points[i][j] - points[i][j + 1])});
adj[e].pb({s, abs(points[i][j] - points[i][j + 1])});
}
}
_alloc({s, 0}); _alloc({g, 0});
return dijkstra(compression[{s, 0}], compression[{g, 0}], adj);
}
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 s, int g) {
bool allh = true;
FOR(i, 1, h.size(), 1){
if(h[i] != h[i-1]) allh = false;
}
if(allh)
return solve3(x, h, l, r, y, s, g);
return solve_12(x, h, l, r, y, s, g);
}
Compilation message
walk.cpp: In function 'long long int solve3(std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>, int, int)':
walk.cpp:27:40: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
27 | #define FOR(i,j,k,in) for(int i=(j); i < (k);i+=in)
| ^
walk.cpp:29:18: note: in expansion of macro 'FOR'
29 | #define REP(i,b) FOR(i,0,b,1)
| ^~~
walk.cpp:127:9: note: in expansion of macro 'REP'
127 | REP(j, ends[i].size()){
| ^~~
walk.cpp:27:40: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
27 | #define FOR(i,j,k,in) for(int i=(j); i < (k);i+=in)
| ^
walk.cpp:29:18: note: in expansion of macro 'FOR'
29 | #define REP(i,b) FOR(i,0,b,1)
| ^~~
walk.cpp:165:9: note: in expansion of macro 'REP'
165 | REP(j, starts[i].size()){
| ^~~
walk.cpp: In function 'long long int solve_12(std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>, int, int)':
walk.cpp:27:40: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::array<int, 3> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
27 | #define FOR(i,j,k,in) for(int i=(j); i < (k);i+=in)
| ^
walk.cpp:29:18: note: in expansion of macro 'FOR'
29 | #define REP(i,b) FOR(i,0,b,1)
| ^~~
walk.cpp:235:5: note: in expansion of macro 'REP'
235 | REP(i, edges.size()){
| ^~~
walk.cpp:236:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::array<int, 2> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
236 | while(pp < bv.size() && bv[pp][0] < edges[i][0]){
| ~~~^~~~~~~~~~~
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:27:40: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
27 | #define FOR(i,j,k,in) for(int i=(j); i < (k);i+=in)
| ^
walk.cpp:275:5: note: in expansion of macro 'FOR'
275 | FOR(i, 1, h.size(), 1){
| ^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
42 ms |
62956 KB |
Output is correct |
2 |
Correct |
22 ms |
31596 KB |
Output is correct |
3 |
Correct |
42 ms |
62956 KB |
Output is correct |
4 |
Correct |
42 ms |
62956 KB |
Output is correct |
5 |
Correct |
43 ms |
63084 KB |
Output is correct |
6 |
Correct |
43 ms |
63084 KB |
Output is correct |
7 |
Correct |
43 ms |
63084 KB |
Output is correct |
8 |
Correct |
42 ms |
62956 KB |
Output is correct |
9 |
Correct |
42 ms |
62956 KB |
Output is correct |
10 |
Correct |
43 ms |
63104 KB |
Output is correct |
11 |
Correct |
43 ms |
62968 KB |
Output is correct |
12 |
Correct |
42 ms |
62956 KB |
Output is correct |
13 |
Correct |
42 ms |
62956 KB |
Output is correct |
14 |
Correct |
42 ms |
62956 KB |
Output is correct |
15 |
Correct |
44 ms |
62956 KB |
Output is correct |
16 |
Correct |
42 ms |
62956 KB |
Output is correct |
17 |
Correct |
43 ms |
63084 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
42 ms |
62956 KB |
Output is correct |
2 |
Correct |
21 ms |
31596 KB |
Output is correct |
3 |
Correct |
2080 ms |
165988 KB |
Output is correct |
4 |
Correct |
1854 ms |
166844 KB |
Output is correct |
5 |
Correct |
1280 ms |
152116 KB |
Output is correct |
6 |
Correct |
1174 ms |
141736 KB |
Output is correct |
7 |
Correct |
1285 ms |
152460 KB |
Output is correct |
8 |
Correct |
2654 ms |
194880 KB |
Output is correct |
9 |
Correct |
1543 ms |
151712 KB |
Output is correct |
10 |
Correct |
2641 ms |
209508 KB |
Output is correct |
11 |
Correct |
1016 ms |
117988 KB |
Output is correct |
12 |
Correct |
686 ms |
101732 KB |
Output is correct |
13 |
Correct |
2185 ms |
193892 KB |
Output is correct |
14 |
Correct |
471 ms |
98532 KB |
Output is correct |
15 |
Correct |
427 ms |
98524 KB |
Output is correct |
16 |
Correct |
415 ms |
100324 KB |
Output is correct |
17 |
Correct |
447 ms |
98916 KB |
Output is correct |
18 |
Correct |
467 ms |
103140 KB |
Output is correct |
19 |
Correct |
54 ms |
64620 KB |
Output is correct |
20 |
Correct |
196 ms |
80360 KB |
Output is correct |
21 |
Correct |
373 ms |
99940 KB |
Output is correct |
22 |
Correct |
387 ms |
102884 KB |
Output is correct |
23 |
Correct |
484 ms |
106084 KB |
Output is correct |
24 |
Correct |
391 ms |
99812 KB |
Output is correct |
25 |
Correct |
451 ms |
100000 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
142 ms |
43444 KB |
Output is correct |
2 |
Correct |
816 ms |
76912 KB |
Output is correct |
3 |
Correct |
915 ms |
79472 KB |
Output is correct |
4 |
Correct |
1043 ms |
91628 KB |
Output is correct |
5 |
Correct |
1322 ms |
93668 KB |
Output is correct |
6 |
Correct |
1322 ms |
92952 KB |
Output is correct |
7 |
Correct |
458 ms |
68840 KB |
Output is correct |
8 |
Correct |
404 ms |
72812 KB |
Output is correct |
9 |
Correct |
1115 ms |
93280 KB |
Output is correct |
10 |
Correct |
470 ms |
72680 KB |
Output is correct |
11 |
Correct |
37 ms |
36332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
142 ms |
43444 KB |
Output is correct |
2 |
Correct |
816 ms |
76912 KB |
Output is correct |
3 |
Correct |
915 ms |
79472 KB |
Output is correct |
4 |
Correct |
1043 ms |
91628 KB |
Output is correct |
5 |
Correct |
1322 ms |
93668 KB |
Output is correct |
6 |
Correct |
1322 ms |
92952 KB |
Output is correct |
7 |
Correct |
458 ms |
68840 KB |
Output is correct |
8 |
Correct |
404 ms |
72812 KB |
Output is correct |
9 |
Correct |
1115 ms |
93280 KB |
Output is correct |
10 |
Correct |
470 ms |
72680 KB |
Output is correct |
11 |
Correct |
37 ms |
36332 KB |
Output is correct |
12 |
Runtime error |
2690 ms |
509284 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
13 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
42 ms |
62956 KB |
Output is correct |
2 |
Correct |
22 ms |
31596 KB |
Output is correct |
3 |
Correct |
42 ms |
62956 KB |
Output is correct |
4 |
Correct |
42 ms |
62956 KB |
Output is correct |
5 |
Correct |
43 ms |
63084 KB |
Output is correct |
6 |
Correct |
43 ms |
63084 KB |
Output is correct |
7 |
Correct |
43 ms |
63084 KB |
Output is correct |
8 |
Correct |
42 ms |
62956 KB |
Output is correct |
9 |
Correct |
42 ms |
62956 KB |
Output is correct |
10 |
Correct |
43 ms |
63104 KB |
Output is correct |
11 |
Correct |
43 ms |
62968 KB |
Output is correct |
12 |
Correct |
42 ms |
62956 KB |
Output is correct |
13 |
Correct |
42 ms |
62956 KB |
Output is correct |
14 |
Correct |
42 ms |
62956 KB |
Output is correct |
15 |
Correct |
44 ms |
62956 KB |
Output is correct |
16 |
Correct |
42 ms |
62956 KB |
Output is correct |
17 |
Correct |
43 ms |
63084 KB |
Output is correct |
18 |
Correct |
42 ms |
62956 KB |
Output is correct |
19 |
Correct |
21 ms |
31596 KB |
Output is correct |
20 |
Correct |
2080 ms |
165988 KB |
Output is correct |
21 |
Correct |
1854 ms |
166844 KB |
Output is correct |
22 |
Correct |
1280 ms |
152116 KB |
Output is correct |
23 |
Correct |
1174 ms |
141736 KB |
Output is correct |
24 |
Correct |
1285 ms |
152460 KB |
Output is correct |
25 |
Correct |
2654 ms |
194880 KB |
Output is correct |
26 |
Correct |
1543 ms |
151712 KB |
Output is correct |
27 |
Correct |
2641 ms |
209508 KB |
Output is correct |
28 |
Correct |
1016 ms |
117988 KB |
Output is correct |
29 |
Correct |
686 ms |
101732 KB |
Output is correct |
30 |
Correct |
2185 ms |
193892 KB |
Output is correct |
31 |
Correct |
471 ms |
98532 KB |
Output is correct |
32 |
Correct |
427 ms |
98524 KB |
Output is correct |
33 |
Correct |
415 ms |
100324 KB |
Output is correct |
34 |
Correct |
447 ms |
98916 KB |
Output is correct |
35 |
Correct |
467 ms |
103140 KB |
Output is correct |
36 |
Correct |
54 ms |
64620 KB |
Output is correct |
37 |
Correct |
196 ms |
80360 KB |
Output is correct |
38 |
Correct |
373 ms |
99940 KB |
Output is correct |
39 |
Correct |
387 ms |
102884 KB |
Output is correct |
40 |
Correct |
484 ms |
106084 KB |
Output is correct |
41 |
Correct |
391 ms |
99812 KB |
Output is correct |
42 |
Correct |
451 ms |
100000 KB |
Output is correct |
43 |
Correct |
142 ms |
43444 KB |
Output is correct |
44 |
Correct |
816 ms |
76912 KB |
Output is correct |
45 |
Correct |
915 ms |
79472 KB |
Output is correct |
46 |
Correct |
1043 ms |
91628 KB |
Output is correct |
47 |
Correct |
1322 ms |
93668 KB |
Output is correct |
48 |
Correct |
1322 ms |
92952 KB |
Output is correct |
49 |
Correct |
458 ms |
68840 KB |
Output is correct |
50 |
Correct |
404 ms |
72812 KB |
Output is correct |
51 |
Correct |
1115 ms |
93280 KB |
Output is correct |
52 |
Correct |
470 ms |
72680 KB |
Output is correct |
53 |
Correct |
37 ms |
36332 KB |
Output is correct |
54 |
Runtime error |
2690 ms |
509284 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
55 |
Halted |
0 ms |
0 KB |
- |