Submission #421475

# Submission time Handle Problem Language Result Execution time Memory
421475 2021-06-09T07:59:11 Z Mlxa Sky Walking (IOI19_walk) C++14
100 / 100
2495 ms 203976 KB
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define all(x) x.begin(), x.end()
#define mp make_pair
#define mt make_tuple0
#define x first
#define y second
#include "walk.h"

namespace my {

const int T    = 1 << 17;
const int N    = 1 << 20;
const int INF  = 0x3f3f3f3f;
const ll LLINF = 0x3f3f3f3f3f3f3f3fLL;
const int NONE = -19;

struct str_1 {
	str_1() {
		fill_n(ass, 2 * T, NONE);
	}
	int ass[2 * T];
	int ls(int v) {
		return 2 * v + 1;
	}
	int rs(int v) {
		return 2 * v + 2;
	}
	void push(int v) {
		if (ass[v] == NONE) {
			return;
		}
		ass[ls(v)] = ass[rs(v)] = ass[v];
		ass[v] = NONE;
	}
	void upd(int v, int vl, int vr, int ql, int qr, int val) {
		if (ql <= vl && vr <= qr) {
			ass[v] = val;
			return;
		}
		if (qr <= vl || vr <= ql) {
			return;
		}
		int vm = (vl + vr) / 2;
		push(v);
		upd(ls(v), vl, vm, ql, qr, val);
		upd(rs(v), vm, vr, ql, qr, val);
	}
	void upd(int l, int r, int v) {
		// for (int i = l; i <= r; ++i) {
		// 	arr[i] = v;
		// }
		upd(0, 0, T, l, r + 1, v);
	}
	int query(int v, int vl, int vr, int pos) {
		assert(vl <= pos && pos < vr);
		if (vr - vl == 1) {
			return ass[v];
		}
		int vm = (vl + vr) / 2;
		push(v);
		if (pos < vm) {
			return query(ls(v), vl, vm, pos);
		} else {
			return query(rs(v), vm, vr, pos);
		}
	}
	int query(int i) {
		// return arr[i];
		return query(0, 0, T, i);
	}
	
} assigner;

struct str_2 {
	int mx[2 * T];
	void upd(int i, int v) {
		mx[i += T] = v;
		for (i /= 2; i > 0; i /= 2) {
			mx[i] = max(mx[i + i], mx[i + i + 1]);
		}
	}
	int to_rig(int v, int vl, int vr, int pos, int val) {
		// cout << "to_rig " << v << " " << vl << " " << vr << " " << pos << " " << val << endl;
		if (mx[v] < val || vr <= pos) {
			return -1;
		}
		if (vr - vl == 1) {
			assert(vl >= pos && mx[v] >= val);
			return vl;
		}
		int vm = (vl + vr) / 2;
		int res = to_rig(v + v, vl, vm, pos, val);
		if (res != -1) {
			return res;
		}
		return to_rig(v + v + 1, vm, vr, pos, val);
	}
	int to_rig(int pos, int val) {
		return to_rig(1, 0, T, pos, val);
		while (mx[T + pos] < val) {
			++pos;
		}
		return pos;
	}
	int to_lef(int v, int vl, int vr, int pos, int val) {
		if (mx[v] < val || vl > pos) {
			return -1;
		}
		if (vr - vl == 1) {
			assert(vl <= pos && mx[v] >= val);
			return vl;
		}
		int vm = (vl + vr) / 2;
		int res = to_lef(v + v + 1, vm, vr, pos, val);
		if (res != -1) {
			return res;
		}
		return to_lef(v + v, vl, vm, pos, val);
	}
	int to_lef(int i, int v) {
		return to_lef(1, 0, T, i, v);
	}
} gfinder;

struct walk {
	int l, r, y, i;
	walk(int a, int b, int c) : l(a), r(b), y(c), i(-1) {}
};
bool operator<(walk a, walk b) {
	return a.y < b.y;
}
vector<walk> was;
vector<int> ord[N];
vector<pair<int, int>> pt;
map<pair<int, int>, int> ma;

vector<pair<int, int>> g[N];
ll dist[N];

int _get_id(int x, int y) {
	auto p = make_pair(x, y);
	if (ma.count(p)) {
		return ma[p];
	} else {
		int v = (int)ma.size();
		assert((int)pt.size() < N);
		pt.emplace_back(x, y);
		return ma[p] = v;
	}
	// int i = (int)(find(all(pt), mp(x, y)) - pt.begin());
	// return i;
}

int get_id(int x, int y) {
	int i = _get_id(x, y);
	assert(pt[i].x == x);
	assert(pt[i].y == y);
	return i;
}

void add_edge(int v, int u) {
	assert(pt[v].x == pt[u].x || pt[v].y == pt[u].y);
	if (v == u) {
		return;
	}
	// cout << "edge " << v << " " << u << endl;
	int w = abs(pt[v].x - pt[u].x) + abs(pt[v].y - pt[u].y);
	// cout << "edge " << pt[v].x << " " << pt[v].y << " " << pt[u].x << " " << pt[u].y << " " << w << endl;
	g[v].emplace_back(u, w);
	g[u].emplace_back(v, w);
}

void vert_edge(int x, int i, int j) {
	assert(i != -1);
	// cout << "vert " << x << " " << i << " " << j << endl;
	if (j == -1) {
		int v = get_id(x, was[i].y);
		ord[i].push_back(v);
		return;
	}
	int v = get_id(x, was[i].y);
	int u = get_id(x, was[j].y);

	ord[i].emplace_back(v);
	ord[j].emplace_back(u);
	add_edge(v, u);
}
}

ll min_distance(vector<int> x, vector<int> h, vector<int> l, vector<int> r, vector<int> y, int st, int fi) {
	using namespace my;
	int n = (int)x.size(), m = (int)l.size();
	for (int i = 0; i < n; ++i) {
		gfinder.upd(i, h[i]);
	}
	vector<walk> temp;
	for (int i = 0; i < m; ++i) {
		was.emplace_back(l[i], r[i], y[i]);
	}
	was.emplace_back(0, n - 1, 0);
	sort(all(was));
	for (auto e : was) {
		if (e.l <= st && st <= e.r) {
			int a = gfinder.to_lef(st, e.y);
			int b = gfinder.to_rig(st, e.y);
			// cout << e.l << " " << a << " " << b << " " << e.r << endl;
			assert(e.l <= a && a <= b && b <= e.r);
			if (e.l < a) {
				temp.emplace_back(e.l, a, e.y);
			}
			if (a < b) {
				temp.emplace_back(a, b, e.y);
			}
			if (b < e.r) {
				temp.emplace_back(b, e.r, e.y);
			}
		} else {
			temp.push_back(e);
		}
	}
	was = temp;
	temp.clear();
	for (auto e : was) {
		if (e.l <= fi && fi <= e.r) {
			int a = gfinder.to_lef(fi, e.y);
			int b = gfinder.to_rig(fi, e.y);
			assert(e.l <= a && a <= b && b <= e.r);
			if (e.l < a) {
				temp.emplace_back(e.l, a, e.y);
			}
			if (a < b) {
				temp.emplace_back(a, b, e.y);
			}
			if (b < e.r) {
				temp.emplace_back(b, e.r, e.y);
			}
		} else {
			temp.push_back(e);
		}
	}
	was = temp;
	temp.clear();
	assert(is_sorted(all(was)));
	m = (int)was.size();
	for (int i = 0; i < m; ++i) {
		// cout << was[i].l << " " << was[i].r << " " << was[i].y << endl;
		was[i].i = i;
	}

	assigner.upd(0, n - 1, -1);
	for (int i = 0; i < m; ++i) {
		auto e = was[i];
		vert_edge(x[e.l], e.i, assigner.query(e.l));
		vert_edge(x[e.r], e.i, assigner.query(e.r));
		assigner.upd(e.l, e.r, e.i);
	}
	assigner.upd(0, n - 1, -1);
		for (int i = m; i--; ) {
		auto e = was[i];
		int j = assigner.query(e.l);
		if (j != -1 && h[e.l] >= was[j].y) {
			vert_edge(x[e.l], e.i, j);
		}
		j = assigner.query(e.r);
		if (j != -1 && h[e.r] >= was[j].y) {
			vert_edge(x[e.r], e.i, j);
		}
		assigner.upd(e.l, e.r, e.i);
	}
	for (int i = 0; i < m; ++i) {
		if (was[i].y == 0) {
			continue;
		}
		sort(all(ord[i]), [&](int a, int b) {
			return pt[a].x < pt[b].x;
		});
		for (int j = 0; j + 1 < (int)ord[i].size(); ++j) {
			add_edge(ord[i][j], ord[i][j + 1]);
		}
	}

	int from = get_id(x[st], 0), to = get_id(x[fi], 0);
	fill_n(dist, N, LLINF);
	dist[from] = 0;
	using T = pair<ll, int>;
	priority_queue<T, vector<T>, greater<T>> q;
	q.emplace(dist[from], from);
	// cout << "from " << pt[from].x << " " << pt[from].y << endl;
	// cout << "to   " << pt[to].x << " " << pt[to].y << endl;
	while (q.size()) {
		ll d;
		int v;
		tie(d, v) = q.top(); q.pop();
		if (dist[v] != d) {
			continue;
		}
		for (auto e : g[v]) {
			int u = e.x, w = e.y;
			if (dist[u] > dist[v] + w) {
				dist[u] = dist[v] + w;
				q.emplace(dist[u], u);
			}
		}
	}
	if (dist[to] > LLINF / 2) {
		return -1;
	} else {
		return dist[to];
	}
}

#ifdef LC
#include "grader.cpp"
#endif
# Verdict Execution time Memory Grader output
1 Correct 34 ms 58776 KB Output is correct
2 Correct 34 ms 58700 KB Output is correct
3 Correct 34 ms 58752 KB Output is correct
4 Correct 35 ms 58716 KB Output is correct
5 Correct 35 ms 58816 KB Output is correct
6 Correct 39 ms 58792 KB Output is correct
7 Correct 34 ms 58740 KB Output is correct
8 Correct 34 ms 58816 KB Output is correct
9 Correct 34 ms 58772 KB Output is correct
10 Correct 34 ms 58776 KB Output is correct
11 Correct 37 ms 58760 KB Output is correct
12 Correct 34 ms 58820 KB Output is correct
13 Correct 34 ms 58828 KB Output is correct
14 Correct 33 ms 58720 KB Output is correct
15 Correct 34 ms 58744 KB Output is correct
16 Correct 37 ms 58828 KB Output is correct
17 Correct 35 ms 58796 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 34 ms 58720 KB Output is correct
2 Correct 34 ms 58792 KB Output is correct
3 Correct 1293 ms 125372 KB Output is correct
4 Correct 1391 ms 128928 KB Output is correct
5 Correct 926 ms 113652 KB Output is correct
6 Correct 933 ms 110908 KB Output is correct
7 Correct 1002 ms 113880 KB Output is correct
8 Correct 1419 ms 129748 KB Output is correct
9 Correct 1423 ms 125500 KB Output is correct
10 Correct 1895 ms 131600 KB Output is correct
11 Correct 1408 ms 115868 KB Output is correct
12 Correct 999 ms 109224 KB Output is correct
13 Correct 1474 ms 133288 KB Output is correct
14 Correct 706 ms 105196 KB Output is correct
15 Correct 796 ms 112224 KB Output is correct
16 Correct 812 ms 111688 KB Output is correct
17 Correct 857 ms 110032 KB Output is correct
18 Correct 553 ms 115524 KB Output is correct
19 Correct 53 ms 61380 KB Output is correct
20 Correct 279 ms 84220 KB Output is correct
21 Correct 956 ms 110988 KB Output is correct
22 Correct 913 ms 112124 KB Output is correct
23 Correct 596 ms 117508 KB Output is correct
24 Correct 859 ms 111876 KB Output is correct
25 Correct 944 ms 109872 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 329 ms 75320 KB Output is correct
2 Correct 1203 ms 143996 KB Output is correct
3 Correct 1476 ms 147844 KB Output is correct
4 Correct 1688 ms 150312 KB Output is correct
5 Correct 1641 ms 152924 KB Output is correct
6 Correct 1503 ms 145544 KB Output is correct
7 Correct 692 ms 105364 KB Output is correct
8 Correct 1098 ms 109492 KB Output is correct
9 Correct 1250 ms 138532 KB Output is correct
10 Correct 859 ms 119412 KB Output is correct
11 Correct 49 ms 59972 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 329 ms 75320 KB Output is correct
2 Correct 1203 ms 143996 KB Output is correct
3 Correct 1476 ms 147844 KB Output is correct
4 Correct 1688 ms 150312 KB Output is correct
5 Correct 1641 ms 152924 KB Output is correct
6 Correct 1503 ms 145544 KB Output is correct
7 Correct 692 ms 105364 KB Output is correct
8 Correct 1098 ms 109492 KB Output is correct
9 Correct 1250 ms 138532 KB Output is correct
10 Correct 859 ms 119412 KB Output is correct
11 Correct 49 ms 59972 KB Output is correct
12 Correct 1528 ms 147532 KB Output is correct
13 Correct 1439 ms 149968 KB Output is correct
14 Correct 1626 ms 152928 KB Output is correct
15 Correct 1355 ms 139552 KB Output is correct
16 Correct 1432 ms 134596 KB Output is correct
17 Correct 1589 ms 147900 KB Output is correct
18 Correct 1331 ms 139580 KB Output is correct
19 Correct 1421 ms 134480 KB Output is correct
20 Correct 678 ms 105208 KB Output is correct
21 Correct 87 ms 61892 KB Output is correct
22 Correct 904 ms 128516 KB Output is correct
23 Correct 996 ms 123912 KB Output is correct
24 Correct 836 ms 110612 KB Output is correct
25 Correct 1018 ms 120176 KB Output is correct
26 Correct 746 ms 105160 KB Output is correct
27 Correct 1468 ms 152924 KB Output is correct
28 Correct 1319 ms 149720 KB Output is correct
29 Correct 1373 ms 145664 KB Output is correct
30 Correct 704 ms 105260 KB Output is correct
31 Correct 1306 ms 138484 KB Output is correct
32 Correct 876 ms 113652 KB Output is correct
33 Correct 848 ms 114044 KB Output is correct
34 Correct 707 ms 116200 KB Output is correct
35 Correct 872 ms 118624 KB Output is correct
36 Correct 963 ms 111652 KB Output is correct
37 Correct 978 ms 108196 KB Output is correct
38 Correct 892 ms 109052 KB Output is correct
39 Correct 596 ms 114460 KB Output is correct
40 Correct 844 ms 108856 KB Output is correct
41 Correct 931 ms 107248 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 34 ms 58776 KB Output is correct
2 Correct 34 ms 58700 KB Output is correct
3 Correct 34 ms 58752 KB Output is correct
4 Correct 35 ms 58716 KB Output is correct
5 Correct 35 ms 58816 KB Output is correct
6 Correct 39 ms 58792 KB Output is correct
7 Correct 34 ms 58740 KB Output is correct
8 Correct 34 ms 58816 KB Output is correct
9 Correct 34 ms 58772 KB Output is correct
10 Correct 34 ms 58776 KB Output is correct
11 Correct 37 ms 58760 KB Output is correct
12 Correct 34 ms 58820 KB Output is correct
13 Correct 34 ms 58828 KB Output is correct
14 Correct 33 ms 58720 KB Output is correct
15 Correct 34 ms 58744 KB Output is correct
16 Correct 37 ms 58828 KB Output is correct
17 Correct 35 ms 58796 KB Output is correct
18 Correct 34 ms 58720 KB Output is correct
19 Correct 34 ms 58792 KB Output is correct
20 Correct 1293 ms 125372 KB Output is correct
21 Correct 1391 ms 128928 KB Output is correct
22 Correct 926 ms 113652 KB Output is correct
23 Correct 933 ms 110908 KB Output is correct
24 Correct 1002 ms 113880 KB Output is correct
25 Correct 1419 ms 129748 KB Output is correct
26 Correct 1423 ms 125500 KB Output is correct
27 Correct 1895 ms 131600 KB Output is correct
28 Correct 1408 ms 115868 KB Output is correct
29 Correct 999 ms 109224 KB Output is correct
30 Correct 1474 ms 133288 KB Output is correct
31 Correct 706 ms 105196 KB Output is correct
32 Correct 796 ms 112224 KB Output is correct
33 Correct 812 ms 111688 KB Output is correct
34 Correct 857 ms 110032 KB Output is correct
35 Correct 553 ms 115524 KB Output is correct
36 Correct 53 ms 61380 KB Output is correct
37 Correct 279 ms 84220 KB Output is correct
38 Correct 956 ms 110988 KB Output is correct
39 Correct 913 ms 112124 KB Output is correct
40 Correct 596 ms 117508 KB Output is correct
41 Correct 859 ms 111876 KB Output is correct
42 Correct 944 ms 109872 KB Output is correct
43 Correct 329 ms 75320 KB Output is correct
44 Correct 1203 ms 143996 KB Output is correct
45 Correct 1476 ms 147844 KB Output is correct
46 Correct 1688 ms 150312 KB Output is correct
47 Correct 1641 ms 152924 KB Output is correct
48 Correct 1503 ms 145544 KB Output is correct
49 Correct 692 ms 105364 KB Output is correct
50 Correct 1098 ms 109492 KB Output is correct
51 Correct 1250 ms 138532 KB Output is correct
52 Correct 859 ms 119412 KB Output is correct
53 Correct 49 ms 59972 KB Output is correct
54 Correct 1528 ms 147532 KB Output is correct
55 Correct 1439 ms 149968 KB Output is correct
56 Correct 1626 ms 152928 KB Output is correct
57 Correct 1355 ms 139552 KB Output is correct
58 Correct 1432 ms 134596 KB Output is correct
59 Correct 1589 ms 147900 KB Output is correct
60 Correct 1331 ms 139580 KB Output is correct
61 Correct 1421 ms 134480 KB Output is correct
62 Correct 678 ms 105208 KB Output is correct
63 Correct 87 ms 61892 KB Output is correct
64 Correct 904 ms 128516 KB Output is correct
65 Correct 996 ms 123912 KB Output is correct
66 Correct 836 ms 110612 KB Output is correct
67 Correct 1018 ms 120176 KB Output is correct
68 Correct 746 ms 105160 KB Output is correct
69 Correct 1468 ms 152924 KB Output is correct
70 Correct 1319 ms 149720 KB Output is correct
71 Correct 1373 ms 145664 KB Output is correct
72 Correct 704 ms 105260 KB Output is correct
73 Correct 1306 ms 138484 KB Output is correct
74 Correct 876 ms 113652 KB Output is correct
75 Correct 848 ms 114044 KB Output is correct
76 Correct 707 ms 116200 KB Output is correct
77 Correct 872 ms 118624 KB Output is correct
78 Correct 963 ms 111652 KB Output is correct
79 Correct 978 ms 108196 KB Output is correct
80 Correct 892 ms 109052 KB Output is correct
81 Correct 596 ms 114460 KB Output is correct
82 Correct 844 ms 108856 KB Output is correct
83 Correct 931 ms 107248 KB Output is correct
84 Correct 292 ms 73216 KB Output is correct
85 Correct 1517 ms 152416 KB Output is correct
86 Correct 1816 ms 174204 KB Output is correct
87 Correct 130 ms 70720 KB Output is correct
88 Correct 142 ms 70168 KB Output is correct
89 Correct 132 ms 70764 KB Output is correct
90 Correct 88 ms 64036 KB Output is correct
91 Correct 35 ms 58908 KB Output is correct
92 Correct 65 ms 62328 KB Output is correct
93 Correct 494 ms 93900 KB Output is correct
94 Correct 76 ms 64196 KB Output is correct
95 Correct 961 ms 136216 KB Output is correct
96 Correct 932 ms 128244 KB Output is correct
97 Correct 864 ms 114348 KB Output is correct
98 Correct 1086 ms 123780 KB Output is correct
99 Correct 2495 ms 203976 KB Output is correct
100 Correct 1513 ms 153152 KB Output is correct
101 Correct 1897 ms 164748 KB Output is correct
102 Correct 704 ms 108488 KB Output is correct
103 Correct 885 ms 116736 KB Output is correct
104 Correct 855 ms 117268 KB Output is correct
105 Correct 778 ms 119156 KB Output is correct
106 Correct 818 ms 117044 KB Output is correct
107 Correct 666 ms 113920 KB Output is correct
108 Correct 106 ms 66052 KB Output is correct
109 Correct 1123 ms 133592 KB Output is correct
110 Correct 1277 ms 153676 KB Output is correct
111 Correct 1272 ms 153396 KB Output is correct
112 Correct 954 ms 120348 KB Output is correct
113 Correct 971 ms 116604 KB Output is correct