Submission #145197

# Submission time Handle Problem Language Result Execution time Memory
145197 2019-08-19T08:44:50 Z ecnerwala Sky Walking (IOI19_walk) C++14
100 / 100
1565 ms 132924 KB
#include "walk.h"

#include<bits/stdc++.h>

using namespace std;
using ll = long long;
using height_t = pair<int, int>;

ll min_distance(vector<int> X_, vector<int> H_, vector<int> L, vector<int> R, vector<int> Y_, int S, int T) {
	int N = int(X_.size());
	int M = int(Y_.size());

	vector<ll> X(X_.begin(), X_.end());
	vector<height_t> H(N);
	for (int i = 0; i < N; i++) {
		H[i] = {H_[i], i};
	}

	vector<height_t> Y(M);
	for (int e = 0; e < M; e++) {
		Y[e] = {Y_[e], -1-e};
	}

	vector<vector<height_t>> needs(N);

	vector<height_t> heights;
	heights.insert(heights.end(), H.begin(), H.end());
	heights.insert(heights.end(), Y.begin(), Y.end());
	sort(heights.begin(), heights.end());
	reverse(heights.begin(), heights.end());

	set<int> posts;
	for (height_t h : heights) {
		if (h.second >= 0) {
			// it's a post
			posts.insert(h.second);
		} else {
			int e = -1-h.second;
			set<int> eNeeds;
			eNeeds.insert(L[e]);
			eNeeds.insert(R[e]);
			for (int p : {S, T}) {
				if (L[e] <= p && p <= R[e]) {
					{
						auto it = posts.lower_bound(p);
						if (it != posts.end()) {
							eNeeds.insert(*it);
						}
					}
					{
						auto it = posts.upper_bound(p);
						if (it != posts.begin()) {
							eNeeds.insert(*--it);
						}
					}
				}
			}
			for (int i : eNeeds) {
				needs[i].push_back(h);
			}
		}
	}

	int V = 0;
	vector<vector<pair<int, ll>>> adj;

	map<height_t, pair<int, int>> prvVert;
	set<height_t> inters;
	vector<vector<height_t>> lefts(N);
	vector<vector<height_t>> rights(N);
	for (int e = 0; e < M; e++) {
		lefts[L[e]].push_back(Y[e]);
		rights[R[e]].push_back(Y[e]);
	}

	const height_t SBOTTOM(0, -1-M);
	lefts[S].push_back(SBOTTOM);
	rights[S].push_back(SBOTTOM);
	needs[S].push_back(SBOTTOM);

	const height_t TBOTTOM(0, -1-(M+1));
	lefts[T].push_back(TBOTTOM);
	rights[T].push_back(TBOTTOM);
	needs[T].push_back(TBOTTOM);


	int Svert = -1, Tvert = -1;
	for (int i = 0; i < N; i++) {
		for (height_t h : lefts[i]) {
			inters.insert(h);
		}

		reverse(needs[i].begin(), needs[i].end()); // should now be sorted

		vector<height_t> realNeeds;
		for (height_t h : needs[i]) {
			auto it = inters.find(h);
			assert(it != inters.end());

			auto kt = it;
			if (kt != inters.begin()) {
				--kt;
				if (realNeeds.empty() || realNeeds.back() < *kt) {
					realNeeds.push_back(*kt);
				}
			}

			if (realNeeds.empty() || realNeeds.back() < *it) {
				realNeeds.push_back(*it);
			}

			auto jt = it; ++jt;
			if (jt != inters.end()) {
				if (realNeeds.empty() || realNeeds.back() < *jt) {
					realNeeds.push_back(*jt);
				}
			}
		}

		while (!realNeeds.empty() && realNeeds.back() > H[i]) realNeeds.pop_back();

		assert(V == int(adj.size()));
		adj.resize(V + int(realNeeds.size()));

		for (int z = 0; z < int(realNeeds.size()); z++) {
			height_t h = realNeeds[z];
			int v = V + z;

			auto it = prvVert.find(h);
			if (it != prvVert.end()) {
				int u = it->second.second;
				ll d = X[i] - X[it->second.first];
				adj[v].emplace_back(u, d);
				adj[u].emplace_back(v, d);

				it->second = {i, v};
			} else {
				prvVert.emplace(h, pair<int, int>{i, v});
			}
		}

		for (int z = 0; z+1 < int(realNeeds.size()); z++) {
			assert(realNeeds[z] < realNeeds[z+1]);
			int v = V + z + 1;
			int u = V + z;
			ll d = realNeeds[z+1].first - realNeeds[z].first;
			adj[v].emplace_back(u, d);
			adj[u].emplace_back(v, d);
		}

		if (i == S) {
			assert(!realNeeds.empty());
			assert(realNeeds[0] == SBOTTOM);
			Svert = V + 0;
		}
		if (i == T) {
			assert(!realNeeds.empty());
			assert(realNeeds[0] == TBOTTOM);
			Tvert = V + 0;
		}

		V += int(realNeeds.size());

		for (height_t h : rights[i]) {
			inters.erase(h);
		}
	}

	const ll INF = 1e18;
	vector<ll> dist(V, INF);
	priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<pair<ll, int>>> pq;
	dist[Svert] = 0;
	pq.emplace(0, Svert);
	while (!pq.empty()) {
		int cur = pq.top().second;
		ll d = pq.top().first;
		pq.pop();
		if (dist[cur] != d) continue;

		if (cur == Tvert) return d;

		for (auto it : adj[cur]) {
			int nxt = it.first;
			ll nd = d + it.second;
			if (nd < dist[nxt]) {
				dist[nxt] = nd;
				pq.emplace(nd, nxt);
			}
		}
	}

	return -1;
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 256 KB Output is correct
3 Correct 2 ms 256 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 2 ms 348 KB Output is correct
9 Correct 2 ms 376 KB Output is correct
10 Correct 3 ms 376 KB Output is correct
11 Correct 2 ms 376 KB Output is correct
12 Correct 2 ms 376 KB Output is correct
13 Correct 2 ms 376 KB Output is correct
14 Correct 2 ms 376 KB Output is correct
15 Correct 2 ms 376 KB Output is correct
16 Correct 2 ms 376 KB Output is correct
17 Correct 2 ms 376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 348 KB Output is correct
2 Correct 2 ms 256 KB Output is correct
3 Correct 524 ms 65172 KB Output is correct
4 Correct 782 ms 84028 KB Output is correct
5 Correct 486 ms 67852 KB Output is correct
6 Correct 479 ms 64100 KB Output is correct
7 Correct 497 ms 68104 KB Output is correct
8 Correct 606 ms 75772 KB Output is correct
9 Correct 693 ms 77932 KB Output is correct
10 Correct 837 ms 85608 KB Output is correct
11 Correct 522 ms 60988 KB Output is correct
12 Correct 597 ms 55404 KB Output is correct
13 Correct 827 ms 89344 KB Output is correct
14 Correct 440 ms 51300 KB Output is correct
15 Correct 508 ms 53468 KB Output is correct
16 Correct 437 ms 54068 KB Output is correct
17 Correct 460 ms 52408 KB Output is correct
18 Correct 410 ms 61104 KB Output is correct
19 Correct 15 ms 2936 KB Output is correct
20 Correct 177 ms 25588 KB Output is correct
21 Correct 424 ms 50200 KB Output is correct
22 Correct 439 ms 54520 KB Output is correct
23 Correct 503 ms 65240 KB Output is correct
24 Correct 462 ms 53780 KB Output is correct
25 Correct 521 ms 53008 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 177 ms 21572 KB Output is correct
2 Correct 851 ms 79816 KB Output is correct
3 Correct 1008 ms 85064 KB Output is correct
4 Correct 1213 ms 107424 KB Output is correct
5 Correct 1460 ms 110044 KB Output is correct
6 Correct 1258 ms 102628 KB Output is correct
7 Correct 561 ms 63020 KB Output is correct
8 Correct 446 ms 55388 KB Output is correct
9 Correct 1453 ms 100752 KB Output is correct
10 Correct 461 ms 66164 KB Output is correct
11 Correct 41 ms 8944 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 177 ms 21572 KB Output is correct
2 Correct 851 ms 79816 KB Output is correct
3 Correct 1008 ms 85064 KB Output is correct
4 Correct 1213 ms 107424 KB Output is correct
5 Correct 1460 ms 110044 KB Output is correct
6 Correct 1258 ms 102628 KB Output is correct
7 Correct 561 ms 63020 KB Output is correct
8 Correct 446 ms 55388 KB Output is correct
9 Correct 1453 ms 100752 KB Output is correct
10 Correct 461 ms 66164 KB Output is correct
11 Correct 41 ms 8944 KB Output is correct
12 Correct 1048 ms 86120 KB Output is correct
13 Correct 1123 ms 103968 KB Output is correct
14 Correct 1463 ms 109884 KB Output is correct
15 Correct 955 ms 86024 KB Output is correct
16 Correct 957 ms 89724 KB Output is correct
17 Correct 1135 ms 108108 KB Output is correct
18 Correct 996 ms 85952 KB Output is correct
19 Correct 979 ms 89668 KB Output is correct
20 Correct 633 ms 61800 KB Output is correct
21 Correct 122 ms 18900 KB Output is correct
22 Correct 643 ms 83192 KB Output is correct
23 Correct 610 ms 78716 KB Output is correct
24 Correct 484 ms 60476 KB Output is correct
25 Correct 551 ms 74044 KB Output is correct
26 Correct 421 ms 51292 KB Output is correct
27 Correct 1446 ms 109208 KB Output is correct
28 Correct 939 ms 102320 KB Output is correct
29 Correct 1565 ms 102316 KB Output is correct
30 Correct 601 ms 64792 KB Output is correct
31 Correct 1266 ms 100364 KB Output is correct
32 Correct 437 ms 58036 KB Output is correct
33 Correct 436 ms 60112 KB Output is correct
34 Correct 459 ms 64696 KB Output is correct
35 Correct 540 ms 65904 KB Output is correct
36 Correct 502 ms 58236 KB Output is correct
37 Correct 445 ms 50076 KB Output is correct
38 Correct 500 ms 54452 KB Output is correct
39 Correct 503 ms 65288 KB Output is correct
40 Correct 419 ms 53932 KB Output is correct
41 Correct 484 ms 52844 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 256 KB Output is correct
3 Correct 2 ms 256 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 2 ms 348 KB Output is correct
9 Correct 2 ms 376 KB Output is correct
10 Correct 3 ms 376 KB Output is correct
11 Correct 2 ms 376 KB Output is correct
12 Correct 2 ms 376 KB Output is correct
13 Correct 2 ms 376 KB Output is correct
14 Correct 2 ms 376 KB Output is correct
15 Correct 2 ms 376 KB Output is correct
16 Correct 2 ms 376 KB Output is correct
17 Correct 2 ms 376 KB Output is correct
18 Correct 2 ms 348 KB Output is correct
19 Correct 2 ms 256 KB Output is correct
20 Correct 524 ms 65172 KB Output is correct
21 Correct 782 ms 84028 KB Output is correct
22 Correct 486 ms 67852 KB Output is correct
23 Correct 479 ms 64100 KB Output is correct
24 Correct 497 ms 68104 KB Output is correct
25 Correct 606 ms 75772 KB Output is correct
26 Correct 693 ms 77932 KB Output is correct
27 Correct 837 ms 85608 KB Output is correct
28 Correct 522 ms 60988 KB Output is correct
29 Correct 597 ms 55404 KB Output is correct
30 Correct 827 ms 89344 KB Output is correct
31 Correct 440 ms 51300 KB Output is correct
32 Correct 508 ms 53468 KB Output is correct
33 Correct 437 ms 54068 KB Output is correct
34 Correct 460 ms 52408 KB Output is correct
35 Correct 410 ms 61104 KB Output is correct
36 Correct 15 ms 2936 KB Output is correct
37 Correct 177 ms 25588 KB Output is correct
38 Correct 424 ms 50200 KB Output is correct
39 Correct 439 ms 54520 KB Output is correct
40 Correct 503 ms 65240 KB Output is correct
41 Correct 462 ms 53780 KB Output is correct
42 Correct 521 ms 53008 KB Output is correct
43 Correct 177 ms 21572 KB Output is correct
44 Correct 851 ms 79816 KB Output is correct
45 Correct 1008 ms 85064 KB Output is correct
46 Correct 1213 ms 107424 KB Output is correct
47 Correct 1460 ms 110044 KB Output is correct
48 Correct 1258 ms 102628 KB Output is correct
49 Correct 561 ms 63020 KB Output is correct
50 Correct 446 ms 55388 KB Output is correct
51 Correct 1453 ms 100752 KB Output is correct
52 Correct 461 ms 66164 KB Output is correct
53 Correct 41 ms 8944 KB Output is correct
54 Correct 1048 ms 86120 KB Output is correct
55 Correct 1123 ms 103968 KB Output is correct
56 Correct 1463 ms 109884 KB Output is correct
57 Correct 955 ms 86024 KB Output is correct
58 Correct 957 ms 89724 KB Output is correct
59 Correct 1135 ms 108108 KB Output is correct
60 Correct 996 ms 85952 KB Output is correct
61 Correct 979 ms 89668 KB Output is correct
62 Correct 633 ms 61800 KB Output is correct
63 Correct 122 ms 18900 KB Output is correct
64 Correct 643 ms 83192 KB Output is correct
65 Correct 610 ms 78716 KB Output is correct
66 Correct 484 ms 60476 KB Output is correct
67 Correct 551 ms 74044 KB Output is correct
68 Correct 421 ms 51292 KB Output is correct
69 Correct 1446 ms 109208 KB Output is correct
70 Correct 939 ms 102320 KB Output is correct
71 Correct 1565 ms 102316 KB Output is correct
72 Correct 601 ms 64792 KB Output is correct
73 Correct 1266 ms 100364 KB Output is correct
74 Correct 437 ms 58036 KB Output is correct
75 Correct 436 ms 60112 KB Output is correct
76 Correct 459 ms 64696 KB Output is correct
77 Correct 540 ms 65904 KB Output is correct
78 Correct 502 ms 58236 KB Output is correct
79 Correct 445 ms 50076 KB Output is correct
80 Correct 500 ms 54452 KB Output is correct
81 Correct 503 ms 65288 KB Output is correct
82 Correct 419 ms 53932 KB Output is correct
83 Correct 484 ms 52844 KB Output is correct
84 Correct 142 ms 17656 KB Output is correct
85 Correct 948 ms 89968 KB Output is correct
86 Correct 1468 ms 119640 KB Output is correct
87 Correct 112 ms 23920 KB Output is correct
88 Correct 146 ms 24364 KB Output is correct
89 Correct 126 ms 23952 KB Output is correct
90 Correct 45 ms 7572 KB Output is correct
91 Correct 3 ms 632 KB Output is correct
92 Correct 30 ms 5120 KB Output is correct
93 Correct 393 ms 47940 KB Output is correct
94 Correct 115 ms 18892 KB Output is correct
95 Correct 690 ms 86860 KB Output is correct
96 Correct 578 ms 79076 KB Output is correct
97 Correct 474 ms 60284 KB Output is correct
98 Correct 575 ms 74144 KB Output is correct
99 Correct 1512 ms 132924 KB Output is correct
100 Correct 1129 ms 109288 KB Output is correct
101 Correct 1240 ms 109032 KB Output is correct
102 Correct 518 ms 62824 KB Output is correct
103 Correct 427 ms 58160 KB Output is correct
104 Correct 431 ms 60180 KB Output is correct
105 Correct 473 ms 64876 KB Output is correct
106 Correct 472 ms 59532 KB Output is correct
107 Correct 509 ms 57936 KB Output is correct
108 Correct 71 ms 8644 KB Output is correct
109 Correct 1066 ms 84820 KB Output is correct
110 Correct 950 ms 102120 KB Output is correct
111 Correct 890 ms 101200 KB Output is correct
112 Correct 515 ms 65208 KB Output is correct
113 Correct 518 ms 61396 KB Output is correct