답안 #285981

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
285981 2020-08-29T20:21:02 Z cookiedoth Sky Walking (IOI19_walk) C++14
57 / 100
4000 ms 304376 KB
#include "walk.h"
#include <iostream>
#include <fstream>
#include <vector>
#include <set>
#include <map>
#include <bitset>
#include <iomanip>
#include <deque>
#include <queue>
#include <algorithm>
#include <string>
#include <cassert>
#include <memory>
#include <numeric>
#include <functional>
#include <random>
#define ll long long
#define null NULL
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define length(a) ((int)a.size())

using namespace std;

template<class iterator> void output(iterator begin, iterator end, ostream &out = cerr) {
	while (begin != end) {
		out << (*begin) << " ";
		begin++;
	}
	out << endl;
}

template<class T> void output(const T &x, ostream &out = cerr) {
	output(all(x), out);
}

template<class T> int chkmin(T &a, const T &b) {
	if (b < a) {
		a = b;
		return 1;
	}
	return 0;
}

template<class T> int chkmax(T &a, const T &b) {
	if (b > a) {
		a = b;
		return 1;
	}
	return 0;
}

struct min_st {
	min_st() {}

	vector<pair<ll, int> > t;
	int n;

	void build(const vector<ll> &h, int v, int tl, int tr) {
		if (tl == tr) {
			t[v] = {h[tl], tl};
		} else {
			int tm = (tl + tr) >> 1;
			build(h, v * 2, tl, tm);
			build(h, v * 2 + 1, tm + 1, tr);
			t[v] = max(t[v * 2], t[v * 2 + 1]);
		}
	}

	void init(const vector<ll> &h) {
		n = h.size();
		t.resize(4 * n);
		build(h, 1, 0, n - 1);
	}

	pair<ll, int> get(int l, int r, int v, int tl, int tr) {
		if (r < tl || tr < l) {
			return {-1, -1};
		}
		if (l <= tl && tr <= r) {
			return t[v];
		} else {
			int tm = (tl + tr) >> 1;
			pair<ll, int> res_l = get(l, r, v * 2, tl, tm);
			pair<ll, int> res_r = get(l, r, v * 2 + 1, tm + 1, tr);
			return max(res_l, res_r);
		}
	}

	pair<ll, int> get(int l, int r) {
		pair<ll, int> res = get(l, r, 1, 0, n - 1);
		return res;
	}
};

struct bridge {
	int l, r;
	ll y;
};

const ll INF = 1e18;
int n, m;
vector<ll> x, h;
vector<bridge> E;
map<ll, ll> mp;
vector<vector<pair<int, ll> > > events;

ll solve(int s, int g) {
	events.resize(n);
	for (int i = 0; i < E.size(); ++i) {
		events[E[i].l].emplace_back(0, E[i].y);
		events[E[i].r].emplace_back(1, E[i].y);
	}
	ll ans = INF;
	set<int> new_chel;
	int it = 0;
	for (int i = 0; i < n; ++i) {
		sort(all(events[i]));
		new_chel.clear();
		for (auto pp : events[i]) {
			it++;
			ll y = pp.second;
			if (pp.first == 0) {
				// cerr << "insert " << y << endl;
				new_chel.insert(y);
				auto it = mp.lower_bound(y);
				ll val = INF;
				if (i == 0) {
					val = y;
				}
				if (it != mp.end()) {
					chkmin(val, it->second + it->first - y);
				}
				if (it != mp.begin()) {
					it--;
					chkmin(val, it->second + y - it->first);
				}
				mp[y] = val;
				// cerr << "dp = " << val << endl;
			} else {
				if (new_chel.find(y) != new_chel.end()) {
					continue;
				}
				// cerr << "erase " << y << endl;
				auto it = mp.find(y);
				assert(it != mp.end());
				if (next(it) != mp.end()) {
					ll y1 = next(it)->first;
					chkmin(mp[y1], mp[y] + y1 - y);
				}
				if (it != mp.begin()) {
					ll y1 = prev(it)->first;
					chkmin(mp[y1], mp[y] + y - y1);
				}
				if (i == n - 1) {
					chkmin(ans, it->first + it->second + x[n - 1] - x[0]);
				}
				mp.erase(it);
			}
		}
		// cerr << "i = " << i << endl;
		// for (auto pp : mp) {
		// 	cerr << pp.first << " " << pp.second << endl;
		// }
	}
	for (auto pp : mp) {
		chkmin(ans, pp.first + pp.second);
	}
	if (ans == INF) {
		ans = -1;
	}
	return ans;
}

namespace djkstra_sol {
	vector<vector<ll> > good_y;
	vector<vector<pair<int, int> > > fwd, bck;
	min_st T;

	void add(int l, int r, ll x, vector<int> &res) {
		if (l > r) {
			return;
		}
		pair<ll, int> opt = T.get(l, r);
		// cerr << "opt = " << opt.first << " " << opt.second << endl;
		if (opt.first >= x) {
			// cerr << "add " << opt.second << endl;
			res.push_back(opt.second);
			add(l, opt.second - 1, x, res);
			add(opt.second + 1, r, x, res);
		}
	}

	vector<int> find_x(bridge B) {
		// cerr << "find_x " << B.l << " " << B.r << " " << B.y << endl;
		vector<int> res;
		add(B.l, B.r, B.y, res);
		return res;
	}

	vector<vector<int> > xc;

	void build_xc(int s, int g) {
		T.init(h);
		// output(all(h));
		// cerr << "sg = " << s << " " << g << endl;
		xc.resize(m);
		good_y.resize(n);
		for (int i = 0; i < m; ++i) {
			xc[i] = find_x(E[i]);
			sort(all(xc[i]));
			for (auto id : xc[i]) {
				good_y[id].push_back(E[i].y);
			}
		}
		good_y[s].push_back(0);
		good_y[g].push_back(0);
		for (int i = 0; i < n; ++i) {
			sort(all(good_y[i]));
			good_y[i].erase(unique(all(good_y[i])), good_y[i].end());
		}
	}

	void build_graph() {
		fwd.resize(n);
		bck.resize(n);
		for (int i = 0; i < n; ++i) {
			fwd[i].resize(good_y[i].size(), make_pair(-1, -1));
			bck[i].resize(good_y[i].size(), make_pair(-1, -1));
		}
		for (int i = 0; i < m; ++i) {
			for (int j = 0; j < (int)xc[i].size() - 1; ++j) {
				int l = xc[i][j], r = xc[i][j + 1];
				int pl = lower_bound(all(good_y[l]), E[i].y) - good_y[l].begin();
				int pr = lower_bound(all(good_y[r]), E[i].y) - good_y[r].begin();
				fwd[l][pl] = {r, pr};
				bck[r][pr] = {l, pl};
			}
		}
	}

	const ll INF = 1e18;
	vector<vector<ll> > d;
	vector<vector<int> > used;

	void djkstra(int s) {
		d.resize(n);
		used.resize(n);
		for (int i = 0; i < n; ++i) {
			d[i].resize(good_y[i].size(), INF);
			used[i].resize(good_y[i].size(), 0);
		}
		set<pair<ll, pair<int, int> > > S;
		d[s][0] = 0;
		S.insert({0, {s, 0}});
		vector<pair<ll, pair<int, int> > > go;
		while (!S.empty()) {
			int id = S.begin()->second.first;
			int level = S.begin()->second.second;
			S.erase(S.begin());
			if (used[id][level]) {
				continue;
			}
			used[id][level] = 1;
			go.clear();
			if (fwd[id][level] != make_pair(-1, -1)) {
				go.emplace_back(x[fwd[id][level].first] - x[id], fwd[id][level]);
			}
			if (bck[id][level] != make_pair(-1, -1)) {
				go.emplace_back(x[id] - x[bck[id][level].first], bck[id][level]);
			}
			if (level < (int)good_y[id].size() - 1) {
				go.emplace_back(good_y[id][level + 1] - good_y[id][level], make_pair(id, level + 1));
			}
			if (level > 0) {
				go.emplace_back(good_y[id][level] - good_y[id][level - 1], make_pair(id, level - 1));
			}
			for (auto ppp : go) {
				if (chkmin(d[ppp.second.first][ppp.second.second], d[id][level] + ppp.first)) {
					S.insert({d[ppp.second.first][ppp.second.second], ppp.second});
				}
			}
		}
	}

	ll solve(int s, int g) {
		build_xc(s, g);
		// cerr << "build_xc" << endl;			
		build_graph();
		djkstra(s);
		return (d[g][0] == INF ? -1 : d[g][0]);
	}
}

ll min_distance(vector<int> _x, vector<int> _h, vector<int> l, vector<int> r, vector<int> y, int s, int g) {
	n = _x.size();
	x.resize(n);
	h.resize(n);
	for (int i = 0; i < n; ++i) {
		x[i] = (ll)_x[i];
		h[i] = (ll)_h[i];
	}
	m = l.size();
	for (int i = 0; i < m; ++i) {
		E.push_back({l[i], r[i], y[i]});
	}
	ll res;
	if (s == 0 && g == n - 1) {
		res = solve(s, g);
	} else {
		res = djkstra_sol::solve(s, g);
	}
	return res;
}

Compilation message

walk.cpp: In function 'long long int solve(int, int)':
walk.cpp:111:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<bridge>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  111 |  for (int i = 0; i < E.size(); ++i) {
      |                  ~~^~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 256 KB Output is correct
2 Correct 1 ms 256 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 1 ms 384 KB Output is correct
12 Correct 1 ms 384 KB Output is correct
13 Correct 1 ms 384 KB Output is correct
14 Correct 1 ms 384 KB Output is correct
15 Correct 1 ms 512 KB Output is correct
16 Correct 1 ms 384 KB Output is correct
17 Correct 1 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 256 KB Output is correct
2 Correct 1 ms 256 KB Output is correct
3 Correct 708 ms 44648 KB Output is correct
4 Correct 749 ms 69992 KB Output is correct
5 Correct 445 ms 63788 KB Output is correct
6 Correct 452 ms 60716 KB Output is correct
7 Correct 454 ms 63848 KB Output is correct
8 Correct 908 ms 53480 KB Output is correct
9 Correct 527 ms 60392 KB Output is correct
10 Correct 1071 ms 84768 KB Output is correct
11 Correct 395 ms 37436 KB Output is correct
12 Correct 137 ms 18920 KB Output is correct
13 Correct 141 ms 19048 KB Output is correct
14 Correct 287 ms 49384 KB Output is correct
15 Correct 392 ms 49760 KB Output is correct
16 Correct 357 ms 46440 KB Output is correct
17 Correct 279 ms 45160 KB Output is correct
18 Correct 157 ms 26856 KB Output is correct
19 Correct 12 ms 2688 KB Output is correct
20 Correct 113 ms 24428 KB Output is correct
21 Correct 119 ms 16756 KB Output is correct
22 Correct 120 ms 18152 KB Output is correct
23 Correct 171 ms 20584 KB Output is correct
24 Correct 136 ms 18152 KB Output is correct
25 Correct 112 ms 17128 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 33 ms 5356 KB Output is correct
2 Correct 153 ms 8680 KB Output is correct
3 Correct 170 ms 9960 KB Output is correct
4 Correct 214 ms 15208 KB Output is correct
5 Correct 277 ms 21096 KB Output is correct
6 Correct 237 ms 17640 KB Output is correct
7 Correct 98 ms 10604 KB Output is correct
8 Correct 119 ms 14952 KB Output is correct
9 Correct 224 ms 17768 KB Output is correct
10 Correct 134 ms 18280 KB Output is correct
11 Correct 16 ms 3072 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 33 ms 5356 KB Output is correct
2 Correct 153 ms 8680 KB Output is correct
3 Correct 170 ms 9960 KB Output is correct
4 Correct 214 ms 15208 KB Output is correct
5 Correct 277 ms 21096 KB Output is correct
6 Correct 237 ms 17640 KB Output is correct
7 Correct 98 ms 10604 KB Output is correct
8 Correct 119 ms 14952 KB Output is correct
9 Correct 224 ms 17768 KB Output is correct
10 Correct 134 ms 18280 KB Output is correct
11 Correct 16 ms 3072 KB Output is correct
12 Correct 173 ms 9960 KB Output is correct
13 Correct 220 ms 15336 KB Output is correct
14 Correct 324 ms 21224 KB Output is correct
15 Correct 171 ms 15976 KB Output is correct
16 Correct 181 ms 16104 KB Output is correct
17 Correct 179 ms 15976 KB Output is correct
18 Correct 166 ms 15912 KB Output is correct
19 Correct 180 ms 15976 KB Output is correct
20 Correct 125 ms 10604 KB Output is correct
21 Correct 40 ms 5880 KB Output is correct
22 Correct 152 ms 13544 KB Output is correct
23 Correct 147 ms 14056 KB Output is correct
24 Correct 148 ms 15336 KB Output is correct
25 Correct 148 ms 13444 KB Output is correct
26 Correct 152 ms 17392 KB Output is correct
27 Correct 303 ms 20100 KB Output is correct
28 Correct 201 ms 14952 KB Output is correct
29 Correct 237 ms 17512 KB Output is correct
30 Correct 104 ms 10604 KB Output is correct
31 Correct 223 ms 17800 KB Output is correct
32 Correct 119 ms 15976 KB Output is correct
33 Correct 124 ms 16616 KB Output is correct
34 Correct 149 ms 16616 KB Output is correct
35 Correct 141 ms 14952 KB Output is correct
36 Correct 136 ms 14568 KB Output is correct
37 Correct 105 ms 14212 KB Output is correct
38 Correct 112 ms 15124 KB Output is correct
39 Correct 166 ms 17768 KB Output is correct
40 Correct 114 ms 15080 KB Output is correct
41 Correct 113 ms 14312 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 256 KB Output is correct
2 Correct 1 ms 256 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 1 ms 384 KB Output is correct
12 Correct 1 ms 384 KB Output is correct
13 Correct 1 ms 384 KB Output is correct
14 Correct 1 ms 384 KB Output is correct
15 Correct 1 ms 512 KB Output is correct
16 Correct 1 ms 384 KB Output is correct
17 Correct 1 ms 384 KB Output is correct
18 Correct 1 ms 256 KB Output is correct
19 Correct 1 ms 256 KB Output is correct
20 Correct 708 ms 44648 KB Output is correct
21 Correct 749 ms 69992 KB Output is correct
22 Correct 445 ms 63788 KB Output is correct
23 Correct 452 ms 60716 KB Output is correct
24 Correct 454 ms 63848 KB Output is correct
25 Correct 908 ms 53480 KB Output is correct
26 Correct 527 ms 60392 KB Output is correct
27 Correct 1071 ms 84768 KB Output is correct
28 Correct 395 ms 37436 KB Output is correct
29 Correct 137 ms 18920 KB Output is correct
30 Correct 141 ms 19048 KB Output is correct
31 Correct 287 ms 49384 KB Output is correct
32 Correct 392 ms 49760 KB Output is correct
33 Correct 357 ms 46440 KB Output is correct
34 Correct 279 ms 45160 KB Output is correct
35 Correct 157 ms 26856 KB Output is correct
36 Correct 12 ms 2688 KB Output is correct
37 Correct 113 ms 24428 KB Output is correct
38 Correct 119 ms 16756 KB Output is correct
39 Correct 120 ms 18152 KB Output is correct
40 Correct 171 ms 20584 KB Output is correct
41 Correct 136 ms 18152 KB Output is correct
42 Correct 112 ms 17128 KB Output is correct
43 Correct 33 ms 5356 KB Output is correct
44 Correct 153 ms 8680 KB Output is correct
45 Correct 170 ms 9960 KB Output is correct
46 Correct 214 ms 15208 KB Output is correct
47 Correct 277 ms 21096 KB Output is correct
48 Correct 237 ms 17640 KB Output is correct
49 Correct 98 ms 10604 KB Output is correct
50 Correct 119 ms 14952 KB Output is correct
51 Correct 224 ms 17768 KB Output is correct
52 Correct 134 ms 18280 KB Output is correct
53 Correct 16 ms 3072 KB Output is correct
54 Correct 173 ms 9960 KB Output is correct
55 Correct 220 ms 15336 KB Output is correct
56 Correct 324 ms 21224 KB Output is correct
57 Correct 171 ms 15976 KB Output is correct
58 Correct 181 ms 16104 KB Output is correct
59 Correct 179 ms 15976 KB Output is correct
60 Correct 166 ms 15912 KB Output is correct
61 Correct 180 ms 15976 KB Output is correct
62 Correct 125 ms 10604 KB Output is correct
63 Correct 40 ms 5880 KB Output is correct
64 Correct 152 ms 13544 KB Output is correct
65 Correct 147 ms 14056 KB Output is correct
66 Correct 148 ms 15336 KB Output is correct
67 Correct 148 ms 13444 KB Output is correct
68 Correct 152 ms 17392 KB Output is correct
69 Correct 303 ms 20100 KB Output is correct
70 Correct 201 ms 14952 KB Output is correct
71 Correct 237 ms 17512 KB Output is correct
72 Correct 104 ms 10604 KB Output is correct
73 Correct 223 ms 17800 KB Output is correct
74 Correct 119 ms 15976 KB Output is correct
75 Correct 124 ms 16616 KB Output is correct
76 Correct 149 ms 16616 KB Output is correct
77 Correct 141 ms 14952 KB Output is correct
78 Correct 136 ms 14568 KB Output is correct
79 Correct 105 ms 14212 KB Output is correct
80 Correct 112 ms 15124 KB Output is correct
81 Correct 166 ms 17768 KB Output is correct
82 Correct 114 ms 15080 KB Output is correct
83 Correct 113 ms 14312 KB Output is correct
84 Correct 70 ms 9836 KB Output is correct
85 Execution timed out 4046 ms 304376 KB Time limit exceeded
86 Halted 0 ms 0 KB -