Submission #1045053

# Submission time Handle Problem Language Result Execution time Memory
1045053 2024-08-05T16:14:24 Z Alihan_8 Sky Walking (IOI19_walk) C++17
57 / 100
4000 ms 892548 KB
#include "walk.h"

#include <bits/stdc++.h>

using namespace std;

#define pb push_back
#define ar array
#define all(x) x.begin(), x.end()
#define ln '\n'

using i64 = long long;

template <class F, class S>
bool chmin(F &u, const S &v){
	bool flag = false;
	
	if ( u > v ){
		u = v; flag |= true;
	}
	
	return flag;
}

const i64 inf = 1e15;

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) {
	int n = x.size(), m = l.size();
	
	if ( s == 0 && g == n - 1 ){ // subtask #3
		vector <vector<ar<int,3>>> H(n);
		
		vector <vector<int>> qry(n);
		
		for ( int i = 0; i < m; i++ ){
			H[l[i]].pb({1, y[i], i});
			H[r[i]].pb({0, y[i], i});
			qry[r[i]].pb(i);
		}
		
		y.pb(0), l.pb(0), r.pb(0); // start
		y.pb(0), l.pb(n - 1), r.pb(n - 1); // end
		
		qry[0].pb(m), qry[n - 1].pb(m + 1);
		
		vector <int> D(m + 2, -1), U(m + 2, -1);
		
		// m -> (0), m + 1 -> (n - 1)
		
		set <pair<int,int>> st;
		
		for ( int i = 0; i < n; i++ ){
			for ( auto &[t, u, j]: H[i] ){
				if ( t > 0 ){
					st.insert({u, j});
				} else if ( i + 1 < n ){ // for (n - 1, 0)
					st.erase({u, j});
				}
			}
			
			for ( auto &j: qry[i] ){
				auto it = st.lower_bound({y[j], -1});
				
				if ( it != st.end() ){
					U[j] = it -> second;
				}
				
				if ( it != st.begin() ){
					D[j] = prev(it) -> second;
				}
			}
		}
		
		//~ cout << "Up & Down : \n";
		//~ for ( int i = 0; i < m + 2; i++ ){
			//~ cout << U[i] << " " << D[i] << ln;
		//~ } cout << ln;
		
		vector <vector<int>> rng(m + 2);
		
		for ( int i = 0; i < m + 2; i++ ){
			rng[i].pb(l[i]);
			rng[i].pb(r[i]);
			
			if ( U[i] == i ) U[i] = -1;
			if ( D[i] == i ) D[i] = -1;
			
			if ( U[i] != -1 ){
				rng[U[i]].pb(r[i]);
			}
			
			if ( D[i] != -1 ){
				rng[D[i]].pb(r[i]);
			}
		}
		
		map <ar<int,2>,int> id;
		
		vector <vector<ar<i64,2>>> adj;
		
		int k = 0;
		
		auto get = [&](int u, int v){
			if ( !id.count({u, v}) ){
				id[{u, v}] = k++;
				adj.pb(vector <ar<i64,2>>());
			}
			
			return id[{u, v}];
		};
		
		for ( int i = 0; i < m + 2; i++ ){
			auto &p = rng[i];
			
			sort(all(p));
			p.resize(unique(all(p)) - p.begin());
			
			{ // assigning index
				for ( auto x: p ) get(x, y[i]); 
			}
			
			for ( int j = 0; j + 1 < (int)p.size(); j++ ){
				int u = get(p[j], y[i]), v = get(p[j + 1], y[i]);
				
				adj[u].pb({v, x[p[j + 1]] - x[p[j]]}); // to the right
				adj[v].pb({u, x[p[j + 1]] - x[p[j]]});
				
				//~ cout << "{" << p[j] << " " << y[i] << "}  {" << p[j + 1] << " " << y[i] << "}  " << x[p[j + 1]] - x[p[j]] << ln;
			}
			
			if ( D[i] != -1 ){ // down
				int u = get(r[i], y[i]), v = get(r[i], y[D[i]]);
				
				//~ cout << "{" << r[i] << " " << y[i] << "}  {" << r[i] << " " << y[D[i]] << "}  " << y[i] - y[D[i]] << ln; 
				
				adj[u].pb({v, y[i] - y[D[i]]});
				adj[v].pb({u, y[i] - y[D[i]]});
			} 
			
			if ( U[i] != -1 ){ // up
				int u = get(r[i], y[i]), v = get(r[i], y[U[i]]);
				
				//~ cout << "{" << r[i] << " " << y[i] << "}  {" << r[i] << " " << y[U[i]] << "}  " << -y[i] + y[U[i]] << ln; 
				
				adj[u].pb({v, -y[i] + y[U[i]]});
				adj[v].pb({u, -y[i] + y[U[i]]});
			}
		}
		
		vector <i64> dp(k, inf);
		
		priority_queue <ar<i64,2>> q;
		
		int u = get(0, 0);
		
		dp[u] = 0;
		q.push({0, u});
		
		while ( !q.empty() ){
			auto [val, u] = q.top();
			q.pop();
			
			if ( -val != dp[u] ) continue;
			
			for ( auto &[v, w]: adj[u] ){
				if ( chmin(dp[v], dp[u] + w) ){
					q.push({-dp[v], v});
				}
			}
		}
		
		u = get(n - 1, 0);
		
		if ( dp[u] == inf ){
			dp[u] = -1;
		}
		
		return dp[u];
	} else{
		vector <vector<ar<int,3>>> H(n + 1);
	
		{ // shifting y
			y.pb(0);
			for ( int i = m; i > 0; i-- ){
				y[i] = y[i - 1];
			} y[0] = 0;
		}
		
		for ( int i = 1; i <= m; i++ ){
			H[l[i - 1]].pb({1, y[i], i});
			H[r[i - 1] + 1].pb({0, y[i], i});
		}
		
		set <pair<int,int>> st;
		
		vector <vector<int>> a(n, {0}), mp(m + 1);
		
		auto V = a;
		
		for ( int i = 0; i < n; i++ ){
			for ( auto &[t, y, j]: H[i] ){
				if ( t > 0 ){
					st.insert({y, j});
				} else st.erase({y, j});
			}
			
			auto it = st.begin();
			
			while ( it != st.end() && it -> first <= h[i] ){
				a[i].pb(it -> second);
				V[i].pb(it -> first);
				mp[it -> second].pb(i);
				it = next(it);
			}
		}
		
		vector <vector<i64>> dp(n);
		vector <vector<int>> id(n);
		
		for ( int i = 0; i < n; i++ ){
			sort(all(a[i]), [&](int &u, int &v){
				return y[u] < y[v];
			});
			
			int s = a[i].size();
			dp[i].resize(s, inf);
			id[i].resize(s);
			
			for ( int j = 1; j < s; j++ ){
				int x = a[i][j];
				
				id[i][j] = lower_bound(all(mp[x]), i) - mp[x].begin();
			}
		}
		
		dp[s][0] = 0;
		
		priority_queue <ar<i64,3>> q;
		
		q.push({0, s, 0});
		
		while ( !q.empty() ){
			auto [val, u, j] = q.top();
			q.pop();
			
			if ( -val != dp[u][j] ) continue;
			
			if ( j != 0 ){ // using skywalk a[u][j]
				int t = a[u][j];
				
				for ( auto i: {id[u][j] - 1, id[u][j] + 1} ){
					
					if ( i < 0 || i >= (int)mp[t].size() ){
						continue;
					}
					
					int v = mp[t][i], nxt = lower_bound(all(V[v]), y[t]) - V[v].begin();
					
					if ( chmin(dp[v][nxt], dp[u][j] + abs(x[v] - x[u])) ){
						q.push({-dp[v][nxt], v, nxt});
					}
				}
			}
			
			for ( auto i: {j + 1, j - 1} ){
				if ( i < 0 || i >= (int)dp[u].size() ){
					continue;
				}
				
				if ( chmin(dp[u][i], dp[u][j] + abs(y[a[u][j]] - y[a[u][i]])) ){
					q.push({-dp[u][i], u, i});
				}
			}
		}
		
		i64 ans = dp[g][0];
		
		if ( ans == inf ){
			ans = -1;
		}
		
		return ans;
	}
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 436 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 1 ms 460 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 604 KB Output is correct
13 Correct 0 ms 436 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 0 ms 352 KB Output is correct
16 Correct 0 ms 352 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 352 KB Output is correct
2 Correct 0 ms 432 KB Output is correct
3 Correct 281 ms 34940 KB Output is correct
4 Correct 280 ms 55752 KB Output is correct
5 Correct 142 ms 49704 KB Output is correct
6 Correct 135 ms 47904 KB Output is correct
7 Correct 150 ms 49832 KB Output is correct
8 Correct 371 ms 40392 KB Output is correct
9 Correct 157 ms 48156 KB Output is correct
10 Correct 354 ms 65148 KB Output is correct
11 Correct 135 ms 31432 KB Output is correct
12 Correct 268 ms 54192 KB Output is correct
13 Correct 546 ms 84768 KB Output is correct
14 Correct 89 ms 40692 KB Output is correct
15 Correct 114 ms 42444 KB Output is correct
16 Correct 109 ms 38600 KB Output is correct
17 Correct 104 ms 37084 KB Output is correct
18 Correct 322 ms 59856 KB Output is correct
19 Correct 5 ms 2140 KB Output is correct
20 Correct 43 ms 19808 KB Output is correct
21 Correct 289 ms 53168 KB Output is correct
22 Correct 400 ms 65968 KB Output is correct
23 Correct 406 ms 66660 KB Output is correct
24 Correct 375 ms 62164 KB Output is correct
25 Correct 348 ms 57148 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 108 ms 20872 KB Output is correct
2 Correct 773 ms 78476 KB Output is correct
3 Correct 868 ms 80808 KB Output is correct
4 Correct 882 ms 88240 KB Output is correct
5 Correct 957 ms 90012 KB Output is correct
6 Correct 823 ms 81880 KB Output is correct
7 Correct 362 ms 48696 KB Output is correct
8 Correct 305 ms 49980 KB Output is correct
9 Correct 684 ms 75120 KB Output is correct
10 Correct 413 ms 60268 KB Output is correct
11 Correct 6 ms 3416 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 108 ms 20872 KB Output is correct
2 Correct 773 ms 78476 KB Output is correct
3 Correct 868 ms 80808 KB Output is correct
4 Correct 882 ms 88240 KB Output is correct
5 Correct 957 ms 90012 KB Output is correct
6 Correct 823 ms 81880 KB Output is correct
7 Correct 362 ms 48696 KB Output is correct
8 Correct 305 ms 49980 KB Output is correct
9 Correct 684 ms 75120 KB Output is correct
10 Correct 413 ms 60268 KB Output is correct
11 Correct 6 ms 3416 KB Output is correct
12 Correct 750 ms 81828 KB Output is correct
13 Correct 682 ms 87832 KB Output is correct
14 Correct 861 ms 92188 KB Output is correct
15 Correct 544 ms 73120 KB Output is correct
16 Correct 560 ms 77216 KB Output is correct
17 Correct 655 ms 87456 KB Output is correct
18 Correct 555 ms 72940 KB Output is correct
19 Correct 565 ms 75948 KB Output is correct
20 Correct 319 ms 48504 KB Output is correct
21 Correct 17 ms 7260 KB Output is correct
22 Correct 530 ms 68396 KB Output is correct
23 Correct 478 ms 66724 KB Output is correct
24 Correct 386 ms 58720 KB Output is correct
25 Correct 417 ms 64476 KB Output is correct
26 Correct 380 ms 58272 KB Output is correct
27 Correct 838 ms 92212 KB Output is correct
28 Correct 658 ms 87460 KB Output is correct
29 Correct 738 ms 82672 KB Output is correct
30 Correct 303 ms 48564 KB Output is correct
31 Correct 623 ms 74072 KB Output is correct
32 Correct 341 ms 56744 KB Output is correct
33 Correct 375 ms 60588 KB Output is correct
34 Correct 469 ms 61120 KB Output is correct
35 Correct 450 ms 67320 KB Output is correct
36 Correct 383 ms 56504 KB Output is correct
37 Correct 298 ms 50348 KB Output is correct
38 Correct 405 ms 63160 KB Output is correct
39 Correct 395 ms 63836 KB Output is correct
40 Correct 388 ms 59304 KB Output is correct
41 Correct 306 ms 54332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 436 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 1 ms 460 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 604 KB Output is correct
13 Correct 0 ms 436 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 0 ms 352 KB Output is correct
16 Correct 0 ms 352 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 352 KB Output is correct
19 Correct 0 ms 432 KB Output is correct
20 Correct 281 ms 34940 KB Output is correct
21 Correct 280 ms 55752 KB Output is correct
22 Correct 142 ms 49704 KB Output is correct
23 Correct 135 ms 47904 KB Output is correct
24 Correct 150 ms 49832 KB Output is correct
25 Correct 371 ms 40392 KB Output is correct
26 Correct 157 ms 48156 KB Output is correct
27 Correct 354 ms 65148 KB Output is correct
28 Correct 135 ms 31432 KB Output is correct
29 Correct 268 ms 54192 KB Output is correct
30 Correct 546 ms 84768 KB Output is correct
31 Correct 89 ms 40692 KB Output is correct
32 Correct 114 ms 42444 KB Output is correct
33 Correct 109 ms 38600 KB Output is correct
34 Correct 104 ms 37084 KB Output is correct
35 Correct 322 ms 59856 KB Output is correct
36 Correct 5 ms 2140 KB Output is correct
37 Correct 43 ms 19808 KB Output is correct
38 Correct 289 ms 53168 KB Output is correct
39 Correct 400 ms 65968 KB Output is correct
40 Correct 406 ms 66660 KB Output is correct
41 Correct 375 ms 62164 KB Output is correct
42 Correct 348 ms 57148 KB Output is correct
43 Correct 108 ms 20872 KB Output is correct
44 Correct 773 ms 78476 KB Output is correct
45 Correct 868 ms 80808 KB Output is correct
46 Correct 882 ms 88240 KB Output is correct
47 Correct 957 ms 90012 KB Output is correct
48 Correct 823 ms 81880 KB Output is correct
49 Correct 362 ms 48696 KB Output is correct
50 Correct 305 ms 49980 KB Output is correct
51 Correct 684 ms 75120 KB Output is correct
52 Correct 413 ms 60268 KB Output is correct
53 Correct 6 ms 3416 KB Output is correct
54 Correct 750 ms 81828 KB Output is correct
55 Correct 682 ms 87832 KB Output is correct
56 Correct 861 ms 92188 KB Output is correct
57 Correct 544 ms 73120 KB Output is correct
58 Correct 560 ms 77216 KB Output is correct
59 Correct 655 ms 87456 KB Output is correct
60 Correct 555 ms 72940 KB Output is correct
61 Correct 565 ms 75948 KB Output is correct
62 Correct 319 ms 48504 KB Output is correct
63 Correct 17 ms 7260 KB Output is correct
64 Correct 530 ms 68396 KB Output is correct
65 Correct 478 ms 66724 KB Output is correct
66 Correct 386 ms 58720 KB Output is correct
67 Correct 417 ms 64476 KB Output is correct
68 Correct 380 ms 58272 KB Output is correct
69 Correct 838 ms 92212 KB Output is correct
70 Correct 658 ms 87460 KB Output is correct
71 Correct 738 ms 82672 KB Output is correct
72 Correct 303 ms 48564 KB Output is correct
73 Correct 623 ms 74072 KB Output is correct
74 Correct 341 ms 56744 KB Output is correct
75 Correct 375 ms 60588 KB Output is correct
76 Correct 469 ms 61120 KB Output is correct
77 Correct 450 ms 67320 KB Output is correct
78 Correct 383 ms 56504 KB Output is correct
79 Correct 298 ms 50348 KB Output is correct
80 Correct 405 ms 63160 KB Output is correct
81 Correct 395 ms 63836 KB Output is correct
82 Correct 388 ms 59304 KB Output is correct
83 Correct 306 ms 54332 KB Output is correct
84 Correct 31 ms 9752 KB Output is correct
85 Execution timed out 4059 ms 892548 KB Time limit exceeded
86 Halted 0 ms 0 KB -