답안 #831263

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
831263 2023-08-20T03:08:52 Z PurpleCrayon Sky Walking (IOI19_walk) C++17
33 / 100
245 ms 37064 KB
#include "walk.h"
#include <bits/stdc++.h>
using namespace std;
 
#define sz(v) int(v.size())
#define ar array
typedef long long ll;
const int N = 2e5+10;
const ll INF = 1e18+10;
 
template <class T>
using min_pq = priority_queue<T, vector<T>, greater<T>>;

struct Solver {
	int n, m;
	vector<pair<int, int>> adj[N];
	ll dist[N];

	void clear() {
		for (int i = 0; i < N; i++) adj[i].clear();
	}
	
	void add_edge(int a, int b, int w) {
		// cerr << "add_edge: " << a << ' ' << b << endl;
		adj[a].emplace_back(b, w);
		adj[b].emplace_back(a, w);
	}
	
	void dijkstra(int S) {
		fill(dist, dist + N, INF);
	
		min_pq<pair<ll, int>> pq;
		pq.push({dist[S] = 0, S});
	
		while (!pq.empty()) {
			auto [d_c, c] = pq.top(); pq.pop();
			if (dist[c] != d_c) continue;
			for (auto [nxt, w] : adj[c]) {
				if (dist[nxt] > dist[c] + w) {
					dist[nxt] = dist[c] + w;
					pq.push({dist[nxt], nxt});
				}
			}
		}
	}
	
	template <class T>
	vector<ll> solve(vector<int> h, vector<int> l, vector<int> r, vector<int> y, vector<T> base) {
		n = sz(h), m = sz(l);
		
		auto build = [&]() {
			vector<vector<pair<int, int>>> ev(n);
			for (int i = 0; i < m; i++) {
				ev[l[i]].push_back({i, 0});
				ev[r[i]].push_back({i, 1});
			}

			set<pair<int, int>> s;
			for (int i = 0; i < n; i++) {
				for (auto [x, b] : ev[i]) if (b == 0) {
					auto me = s.insert({y[x], x}).first;
	
					{
						auto it = next(me);
						if (it != s.end() && it->first <= h[i]) {
							add_edge(x, it->second, it->first - y[x]);
						}
					}
	
					{
						if (me != s.begin()) {
							auto it = prev(me);
							add_edge(x, it->second, y[x] - it->first);
						}
					}
				}
	
				for (auto [x, b] : ev[i]) if (b == 1) {
					auto me = s.find({y[x], x});
					{
						auto it = next(me);
						if (it != s.end() && it->first <= h[i]) {
							add_edge(x, it->second, it->first - y[x]);
						}
					}
	
					{
						if (me != s.begin()) {
							auto it = prev(me);
							add_edge(x, it->second, y[x] - it->first);
						}
					}
	
					s.erase({y[x], x});
				}
			}
		};
	
		build();
		int S = m;
		for (int i = 0; i < m; i++) if (l[i] == 0 && base[i] < INF) {
			add_edge(S, i, base[i]);
		}
		dijkstra(S);

		vector<ll> ans(m);
		for (int i = 0; i < m; i++) {
			ans[i] = dist[i];
		}
		return ans;
	}
};


Solver solver;

long long min_distance(vector<int> x, vector<int> h, vector<int> l, vector<int> r, vector<int> y, int s, int g) {
	int n = sz(x), m = sz(l);
	// assert(s == 0 && g == n-1);

	vector<ll> dist_left(m, INF);
	{
		vector<int> nh;
		for (int i = s; i >= 0; i--) {
			nh.push_back(h[i]);
		}

		vector<int> nl, nr, ny, ids;
		for (int i = 0; i < m; i++) if (l[i] <= s) {
			int L = l[i], R = min(s, r[i]);
			while (h[R] < y[i]) R--; // TODO optimize
			dist_left[i] = 2 * (x[s] - x[R]);

			nl.push_back(s - R); nr.push_back(s - L); ny.push_back(y[i]);
			ids.push_back(i);
		}

		solver.clear();
		auto calc = solver.solve(nh, nl, nr, ny, ny);
		for (int i = 0; i < sz(ids); i++) {
			dist_left[ids[i]] += calc[i];
		}
	}

	vector<ll> dist_right(m, INF);
	{
		vector<int> nh;
		for (int i = g; i < n; i++) {
			nh.push_back(h[i]);
		}

		vector<int> nl, nr, ny, ids;
		for (int i = 0; i < m; i++) if (g <= r[i]) {
			int L = max(g, l[i]), R = r[i];
			while (h[L] < y[i]) L++; // TODO optimize
			dist_right[i] = 2 * (x[L] - x[g]);
			// cerr << "> " << dist_right[i] << endl;

			nl.push_back(L - g); nr.push_back(R - g); ny.push_back(y[i]);
			ids.push_back(i);
		}

		solver.clear();
		auto calc = solver.solve(nh, nl, nr, ny, ny);
		for (int i = 0; i < sz(ids); i++) {
			dist_right[ids[i]] += calc[i];
		}
	}

	auto dist_mid = dist_left;
	{
		vector<int> nh;
		for (int i = s; i <= g; i++) {
			nh.push_back(h[i]);
		}

		vector<int> nl, nr, ny, ids;
		vector<ll> base;
		for (int i = 0; i < m; i++) if (r[i] >= s && l[i] <= g) {
			int L = max(s, l[i]), R = min(g, r[i]);
			while (L <= R && h[L] < y[i]) L++;
			while (R >= L && h[R] < y[i]) R--;
			if (L > R) continue;

			nl.push_back(L - s); nr.push_back(R - s); ny.push_back(y[i]);
			ids.push_back(i);
			base.push_back(dist_left[i]);

			// cerr << "> " << L << ' ' << R << ' ' << y[i] << ' ' << dist_left[i] << endl;
		}

		solver.clear();
		auto calc = solver.solve(nh, nl, nr, ny, base);
		for (int i = 0; i < sz(ids); i++) {
			int c = ids[i];
			// cerr << "> " << calc[i] << endl;
			dist_mid[c] = min(dist_mid[c], calc[i]);
		}
	}
	
	// cerr << "dists\n";
	// for (ll x : dist_left) cerr << x << ' ';
	// cerr << endl;

	// for (ll x : dist_mid) cerr << x << ' ';
	// cerr << endl;

	// for (ll x : dist_right) cerr << x << ' ';
	// cerr << endl;
	// cerr << "end dists\n";


	ll ans = INF;
	for (int i = 0; i < m; i++) {
		ans = min(ans, dist_mid[i] + dist_right[i]);
	}

	// cerr << x[n-1] - x[0] << ' ' << ans << endl;
	if (ans >= INF) return -1;
	return ans + x[g] - x[s];
}
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 6484 KB Output is correct
2 Correct 3 ms 6484 KB Output is correct
3 Incorrect 3 ms 6484 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 6484 KB Output is correct
2 Correct 3 ms 6484 KB Output is correct
3 Incorrect 97 ms 20424 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 47 ms 16876 KB Output is correct
2 Correct 145 ms 28008 KB Output is correct
3 Correct 136 ms 29540 KB Output is correct
4 Correct 167 ms 34140 KB Output is correct
5 Correct 206 ms 35948 KB Output is correct
6 Correct 222 ms 36236 KB Output is correct
7 Correct 76 ms 23064 KB Output is correct
8 Correct 82 ms 28140 KB Output is correct
9 Correct 245 ms 37064 KB Output is correct
10 Correct 154 ms 32896 KB Output is correct
11 Correct 11 ms 8996 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 47 ms 16876 KB Output is correct
2 Correct 145 ms 28008 KB Output is correct
3 Correct 136 ms 29540 KB Output is correct
4 Correct 167 ms 34140 KB Output is correct
5 Correct 206 ms 35948 KB Output is correct
6 Correct 222 ms 36236 KB Output is correct
7 Correct 76 ms 23064 KB Output is correct
8 Correct 82 ms 28140 KB Output is correct
9 Correct 245 ms 37064 KB Output is correct
10 Correct 154 ms 32896 KB Output is correct
11 Correct 11 ms 8996 KB Output is correct
12 Correct 132 ms 28820 KB Output is correct
13 Correct 154 ms 34048 KB Output is correct
14 Correct 236 ms 35984 KB Output is correct
15 Correct 125 ms 30456 KB Output is correct
16 Correct 145 ms 34728 KB Output is correct
17 Correct 140 ms 32596 KB Output is correct
18 Correct 125 ms 30556 KB Output is correct
19 Correct 138 ms 34412 KB Output is correct
20 Correct 88 ms 23324 KB Output is correct
21 Correct 21 ms 11468 KB Output is correct
22 Correct 103 ms 29860 KB Output is correct
23 Correct 99 ms 30276 KB Output is correct
24 Correct 106 ms 28572 KB Output is correct
25 Correct 106 ms 29288 KB Output is correct
26 Correct 84 ms 27064 KB Output is correct
27 Correct 203 ms 36140 KB Output is correct
28 Correct 130 ms 33912 KB Output is correct
29 Correct 222 ms 36140 KB Output is correct
30 Correct 74 ms 23156 KB Output is correct
31 Correct 236 ms 37064 KB Output is correct
32 Correct 125 ms 30556 KB Output is correct
33 Correct 104 ms 29800 KB Output is correct
34 Correct 148 ms 33236 KB Output is correct
35 Correct 105 ms 30728 KB Output is correct
36 Correct 93 ms 29552 KB Output is correct
37 Correct 83 ms 28200 KB Output is correct
38 Correct 80 ms 28160 KB Output is correct
39 Correct 181 ms 35388 KB Output is correct
40 Correct 83 ms 28440 KB Output is correct
41 Correct 84 ms 28780 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 6484 KB Output is correct
2 Correct 3 ms 6484 KB Output is correct
3 Incorrect 3 ms 6484 KB Output isn't correct
4 Halted 0 ms 0 KB -