117082024-11-06 23:37:59peti1234Drónfutárcpp17Accepted 100/100256ms9928 KiB
// https://sotanishy.github.io/cp-library-cpp/graph/manhattan_mst.hpp.html
// https://judge.yosupo.jp/submission/167321
#include <bits/stdc++.h>
using namespace std;
#define all(x) x.begin(), x.end()
typedef long long ll;
typedef pair<int, int> pi;
#define x first
#define y second
typedef tuple<int, int, int> ti3;
struct UF {
	int n;
	vector<int> t, r;
	UF(int _n) : n{ _n } {
		t.resize(n); iota(all(t), 0);
		r.resize(n);
	}
	int f(int x) { return t[x] == x ? x : (t[x] = f(t[x])); }
	bool merge(int a, int b) {
		a = f(a), b = f(b);
		if (a == b) return false;
		if (r[a] < r[b]) swap(a, b);
		t[b] = a;
		r[b] += r[a] == r[b];
		return true;
	}
};
void manhattan_mst(vector<pi>& pts) {
	int n = pts.size();
	vector<int> idx(n); iota(all(idx), 0);
	vector<ti3> edges; edges.reserve(4 * n);

	for (int i = 0; i < 2; i++) {
		for(auto&& [x,y]:pts) x=-x;
		for (int j = 0; j < 2; j++) {
			for(auto&& [x, y] : pts) swap(x,y);
			sort(all(idx), [&](int a, int b)->bool {
				return pts[a].x + pts[a].y < pts[b].x + pts[b].y;
				});

			map<int, int> mp;
			for (int id : idx) {
				auto&& [x, y] = pts[id];
				for (auto itr = mp.lower_bound(-y); itr != mp.end(); itr = mp.erase(itr)) {
					int to = itr->second;
					auto&& [x0, y0] = pts[to];
					int dx = x - x0, dy = y - y0;
					if (dy > dx) break;
					edges.push_back({ dx + dy, id, to });
				}
				mp[-y] = id;
			}
		}
	}
	sort(all(edges));
	ll ans = 0;
	UF uf(n);
	vector<pi> ret;
	for (auto& [c, u, v] : edges) {
		if (uf.merge(u, v)) {
			ans = c;
			ret.push_back(pi(u, v));
		}
	}
	cout << ans << "\n";
	/*for (auto& [u, v] : ret)
		cout << u << " " << v << "\n";*/
}
int main() {
	ios::sync_with_stdio(0);
	cin.tie(0), cout.tie(0);
	int n; cin >> n;
	vector<pi> arr(n);
	for (auto& [x, y] : arr) cin >> x >> y;
	manhattan_mst(arr);
	return 0;
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted1ms320 KiB
2Accepted254ms9924 KiB
subtask215/15
3Accepted3ms320 KiB
4Accepted3ms524 KiB
5Accepted3ms320 KiB
6Accepted3ms320 KiB
7Accepted1ms320 KiB
subtask315/15
8Accepted3ms320 KiB
9Accepted3ms524 KiB
10Accepted3ms320 KiB
11Accepted3ms320 KiB
12Accepted1ms320 KiB
13Accepted2ms432 KiB
14Accepted3ms460 KiB
15Accepted3ms468 KiB
16Accepted3ms320 KiB
17Accepted3ms320 KiB
subtask435/35
18Accepted3ms320 KiB
19Accepted3ms524 KiB
20Accepted3ms320 KiB
21Accepted3ms320 KiB
22Accepted1ms320 KiB
23Accepted2ms432 KiB
24Accepted3ms460 KiB
25Accepted3ms468 KiB
26Accepted3ms320 KiB
27Accepted3ms320 KiB
28Accepted3ms460 KiB
29Accepted45ms2360 KiB
30Accepted68ms3288 KiB
31Accepted68ms3316 KiB
32Accepted50ms3660 KiB
subtask535/35
33Accepted3ms320 KiB
34Accepted3ms524 KiB
35Accepted3ms320 KiB
36Accepted3ms320 KiB
37Accepted1ms320 KiB
38Accepted2ms432 KiB
39Accepted3ms460 KiB
40Accepted3ms468 KiB
41Accepted3ms320 KiB
42Accepted3ms320 KiB
43Accepted3ms460 KiB
44Accepted45ms2360 KiB
45Accepted68ms3288 KiB
46Accepted68ms3316 KiB
47Accepted50ms3660 KiB
48Accepted119ms5048 KiB
49Accepted256ms9924 KiB
50Accepted254ms9928 KiB
51Accepted254ms9924 KiB
52Accepted254ms9924 KiB