답안 #422829

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
422829 2021-06-10T12:52:01 Z KoD Sky Walking (IOI19_walk) C++17
57 / 100
4000 ms 839864 KB
#include <bits/stdc++.h>
#include "walk.h"

using ll = long long;
template <class T> using Vec = std::vector<T>;
template <class T> using Heap = std::priority_queue<T, Vec<T>, std::greater<T>>;

constexpr ll INF = std::numeric_limits<ll>::max() / 2;

struct RMQ {
	int size;
	Vec<ll> min;
	RMQ(const int n): size(n), min(2 * n, INF) {}
	void assign(int i, const ll x) {
		i += size;
		min[i] = x;
		while (i > 1) {
			i >>= 1;
			min[i] = std::min(min[2 * i], min[2 * i + 1]);
		}
	}
	ll fold(int l, int r) const {
		l += size;
		r += size;
		ll x = INF;
		while (l < r) {
			if (l & 1) x = std::min(x, min[l++]);
			if (r & 1) x = std::min(x, min[--r]);
			l >>= 1;
			r >>= 1;
		}
		return x;
	}
};

ll min_distance(Vec<int> x, Vec<int> h, Vec<int> l, Vec<int> r, Vec<int> y, int s, int g) {
	const int n = (int) x.size();
	const int m = (int) y.size();
	if (s == 0 and g == n - 1) {
		Vec<Vec<int>> start(n), end(n);
		for (int i = 0; i < m; ++i) {
			start[l[i]].push_back(i);
			end[r[i]].push_back(i);
		}
		Vec<int> cmp = y;
		std::sort(cmp.begin(), cmp.end());
		cmp.erase(std::unique(cmp.begin(), cmp.end()), cmp.end());
		Vec<int> idx(m);
		for (int i = 0; i < m; ++i) {
			idx[i] = std::lower_bound(cmp.begin(), cmp.end(), y[i]) - cmp.begin();
		}
		const int sz = (int) cmp.size();
		RMQ down(sz), up(sz);
		Vec<int> alive(sz);
		for (const int j: start[0]) {
			down.assign(idx[j], y[j] - y[j]);
			up.assign(idx[j], y[j] + y[j]);
			alive[idx[j]] += 1;			
		}
		for (int i = 1; i < n; ++i) {
			const int height = std::upper_bound(cmp.begin(), cmp.end(), h[i]) - cmp.begin();
			for (const auto j: start[i]) {
				const ll a = down.fold(0, idx[j] + 1);
				const ll b = up.fold(idx[j], sz);
				ll d = INF;
				if (a != INF) {
					d = std::min(d, a + y[j]);
				}
				if (b != INF) {
					d = std::min(d, b - y[j]);
				}
				if (d != INF) {
					down.assign(idx[j], d - y[j]);
					up.assign(idx[j], d + y[j]);
				} else {
					down.assign(idx[j], INF);
					down.assign(idx[j], INF);
				}
				alive[idx[j]] += 1;
			}
			if (i != n - 1) {
				for (const auto j: end[i]) {
					alive[idx[j]] -= 1;
					if (alive[idx[j]] == 0) {
						down.assign(idx[j], INF);
						up.assign(idx[j], INF);
					}
				}
			}
		}
		ll ret = up.fold(0, sz);
		return ret == INF ? -1 : (ret + x[n - 1] - x[0]);
	}
	Vec<std::tuple<int, int, int>> query;
	query.reserve(n + m);
	for (int i = 0; i < n; ++i) {
		query.emplace_back(h[i], n, i);
	}
	for (int i = 0; i < m; ++i) {
		query.emplace_back(y[i], l[i], r[i]);
	}
	Vec<Vec<ll>> height(n);
	Vec<Vec<Vec<std::pair<int, int>>>> graph(n);
	std::sort(query.rbegin(), query.rend());
	std::map<int, int> alive;
	for (const auto [t, u, v]: query) {
		if (u == n) {
			alive.emplace(x[v], v);
		} else {
			const auto end = alive.upper_bound(x[v]);
			int li = -1, lj = -1;
			for (auto itr = alive.lower_bound(x[u]); itr != end; ++itr) {
				const auto i = itr -> second;
				if (height[i].empty() or height[i].back() != t) {
					height[i].push_back(t);
					graph[i].emplace_back();
				}
				const auto j = (int) height[i].size() - 1;
				if (li != -1) {
					graph[i][j].emplace_back(li, lj);
					graph[li][lj].emplace_back(i, j);
				}
				li = i, lj = j;
			}
		}
	}
	Vec<Vec<ll>> dist(n);
	for (int i = 0; i < n; ++i) {
		height[i].push_back(0);
		graph[i].emplace_back();
		dist[i].assign(height[i].size(), INF);
	}
	Heap<std::tuple<ll, int, int>> heap;
	const auto push = [&](const int i, const int j, const ll d) {
		if (dist[i][j] > d) {
			dist[i][j] = d;
			heap.emplace(d, i, j);
		}
	};
	push(s, (int) height[s].size() - 1, 0);
	while (!heap.empty()) {
		const auto [d, i, j] = heap.top();
		heap.pop();
		if (dist[i][j] < d) {
			continue;
		}
		for (const auto [ni, nj]: graph[i][j]) {
			push(ni, nj, d + std::abs(x[i] - x[ni]));
		}
		if (j > 0) {
			push(i, j - 1, d + std::abs(height[i][j] - height[i][j - 1]));
		}
		if (j + 1 < (int) height[i].size()) {
			push(i, j + 1, d + std::abs(height[i][j + 1] - height[i][j]));
		}
	}
	const auto ret = dist[g][height[g].size() - 1];
	return ret == INF ? -1 : ret;
}

Compilation message

walk.cpp: In function 'll min_distance(Vec<int>, Vec<int>, Vec<int>, Vec<int>, Vec<int>, int, int)':
walk.cpp:61:14: warning: unused variable 'height' [-Wunused-variable]
   61 |    const int height = std::upper_bound(cmp.begin(), cmp.end(), h[i]) - cmp.begin();
      |              ^~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 0 ms 204 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 1 ms 204 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 0 ms 204 KB Output is correct
16 Correct 1 ms 204 KB Output is correct
17 Correct 1 ms 236 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 613 ms 67736 KB Output is correct
4 Correct 930 ms 87492 KB Output is correct
5 Correct 569 ms 76692 KB Output is correct
6 Correct 538 ms 69444 KB Output is correct
7 Correct 596 ms 76844 KB Output is correct
8 Correct 790 ms 82244 KB Output is correct
9 Correct 663 ms 71740 KB Output is correct
10 Correct 1258 ms 110384 KB Output is correct
11 Correct 461 ms 42072 KB Output is correct
12 Correct 227 ms 19480 KB Output is correct
13 Correct 215 ms 19392 KB Output is correct
14 Correct 315 ms 42028 KB Output is correct
15 Correct 257 ms 43396 KB Output is correct
16 Correct 229 ms 42052 KB Output is correct
17 Correct 226 ms 40636 KB Output is correct
18 Correct 92 ms 14136 KB Output is correct
19 Correct 9 ms 2380 KB Output is correct
20 Correct 112 ms 21648 KB Output is correct
21 Correct 131 ms 12808 KB Output is correct
22 Correct 130 ms 15932 KB Output is correct
23 Correct 147 ms 16320 KB Output is correct
24 Correct 131 ms 15056 KB Output is correct
25 Correct 102 ms 13772 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 29 ms 3448 KB Output is correct
2 Correct 111 ms 8132 KB Output is correct
3 Correct 152 ms 9104 KB Output is correct
4 Correct 216 ms 17088 KB Output is correct
5 Correct 171 ms 15556 KB Output is correct
6 Correct 187 ms 16880 KB Output is correct
7 Correct 107 ms 12788 KB Output is correct
8 Correct 211 ms 19396 KB Output is correct
9 Correct 183 ms 18100 KB Output is correct
10 Correct 124 ms 14916 KB Output is correct
11 Correct 14 ms 3380 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 29 ms 3448 KB Output is correct
2 Correct 111 ms 8132 KB Output is correct
3 Correct 152 ms 9104 KB Output is correct
4 Correct 216 ms 17088 KB Output is correct
5 Correct 171 ms 15556 KB Output is correct
6 Correct 187 ms 16880 KB Output is correct
7 Correct 107 ms 12788 KB Output is correct
8 Correct 211 ms 19396 KB Output is correct
9 Correct 183 ms 18100 KB Output is correct
10 Correct 124 ms 14916 KB Output is correct
11 Correct 14 ms 3380 KB Output is correct
12 Correct 150 ms 9100 KB Output is correct
13 Correct 199 ms 17072 KB Output is correct
14 Correct 175 ms 15624 KB Output is correct
15 Correct 141 ms 14020 KB Output is correct
16 Correct 154 ms 14068 KB Output is correct
17 Correct 150 ms 14092 KB Output is correct
18 Correct 145 ms 14020 KB Output is correct
19 Correct 160 ms 14068 KB Output is correct
20 Correct 120 ms 12596 KB Output is correct
21 Correct 31 ms 6580 KB Output is correct
22 Correct 143 ms 14264 KB Output is correct
23 Correct 150 ms 14536 KB Output is correct
24 Correct 191 ms 15152 KB Output is correct
25 Correct 151 ms 14992 KB Output is correct
26 Correct 155 ms 16180 KB Output is correct
27 Correct 178 ms 16164 KB Output is correct
28 Correct 209 ms 17076 KB Output is correct
29 Correct 183 ms 16852 KB Output is correct
30 Correct 103 ms 12740 KB Output is correct
31 Correct 244 ms 17980 KB Output is correct
32 Correct 122 ms 13892 KB Output is correct
33 Correct 121 ms 15408 KB Output is correct
34 Correct 135 ms 15452 KB Output is correct
35 Correct 174 ms 15120 KB Output is correct
36 Correct 165 ms 14712 KB Output is correct
37 Correct 95 ms 12740 KB Output is correct
38 Correct 129 ms 15952 KB Output is correct
39 Correct 146 ms 16308 KB Output is correct
40 Correct 134 ms 15044 KB Output is correct
41 Correct 102 ms 13768 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 0 ms 204 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 1 ms 204 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 0 ms 204 KB Output is correct
16 Correct 1 ms 204 KB Output is correct
17 Correct 1 ms 236 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Correct 0 ms 204 KB Output is correct
20 Correct 613 ms 67736 KB Output is correct
21 Correct 930 ms 87492 KB Output is correct
22 Correct 569 ms 76692 KB Output is correct
23 Correct 538 ms 69444 KB Output is correct
24 Correct 596 ms 76844 KB Output is correct
25 Correct 790 ms 82244 KB Output is correct
26 Correct 663 ms 71740 KB Output is correct
27 Correct 1258 ms 110384 KB Output is correct
28 Correct 461 ms 42072 KB Output is correct
29 Correct 227 ms 19480 KB Output is correct
30 Correct 215 ms 19392 KB Output is correct
31 Correct 315 ms 42028 KB Output is correct
32 Correct 257 ms 43396 KB Output is correct
33 Correct 229 ms 42052 KB Output is correct
34 Correct 226 ms 40636 KB Output is correct
35 Correct 92 ms 14136 KB Output is correct
36 Correct 9 ms 2380 KB Output is correct
37 Correct 112 ms 21648 KB Output is correct
38 Correct 131 ms 12808 KB Output is correct
39 Correct 130 ms 15932 KB Output is correct
40 Correct 147 ms 16320 KB Output is correct
41 Correct 131 ms 15056 KB Output is correct
42 Correct 102 ms 13772 KB Output is correct
43 Correct 29 ms 3448 KB Output is correct
44 Correct 111 ms 8132 KB Output is correct
45 Correct 152 ms 9104 KB Output is correct
46 Correct 216 ms 17088 KB Output is correct
47 Correct 171 ms 15556 KB Output is correct
48 Correct 187 ms 16880 KB Output is correct
49 Correct 107 ms 12788 KB Output is correct
50 Correct 211 ms 19396 KB Output is correct
51 Correct 183 ms 18100 KB Output is correct
52 Correct 124 ms 14916 KB Output is correct
53 Correct 14 ms 3380 KB Output is correct
54 Correct 150 ms 9100 KB Output is correct
55 Correct 199 ms 17072 KB Output is correct
56 Correct 175 ms 15624 KB Output is correct
57 Correct 141 ms 14020 KB Output is correct
58 Correct 154 ms 14068 KB Output is correct
59 Correct 150 ms 14092 KB Output is correct
60 Correct 145 ms 14020 KB Output is correct
61 Correct 160 ms 14068 KB Output is correct
62 Correct 120 ms 12596 KB Output is correct
63 Correct 31 ms 6580 KB Output is correct
64 Correct 143 ms 14264 KB Output is correct
65 Correct 150 ms 14536 KB Output is correct
66 Correct 191 ms 15152 KB Output is correct
67 Correct 151 ms 14992 KB Output is correct
68 Correct 155 ms 16180 KB Output is correct
69 Correct 178 ms 16164 KB Output is correct
70 Correct 209 ms 17076 KB Output is correct
71 Correct 183 ms 16852 KB Output is correct
72 Correct 103 ms 12740 KB Output is correct
73 Correct 244 ms 17980 KB Output is correct
74 Correct 122 ms 13892 KB Output is correct
75 Correct 121 ms 15408 KB Output is correct
76 Correct 135 ms 15452 KB Output is correct
77 Correct 174 ms 15120 KB Output is correct
78 Correct 165 ms 14712 KB Output is correct
79 Correct 95 ms 12740 KB Output is correct
80 Correct 129 ms 15952 KB Output is correct
81 Correct 146 ms 16308 KB Output is correct
82 Correct 134 ms 15044 KB Output is correct
83 Correct 102 ms 13768 KB Output is correct
84 Correct 49 ms 8424 KB Output is correct
85 Execution timed out 4083 ms 839864 KB Time limit exceeded
86 Halted 0 ms 0 KB -