55292023-07-11 15:49:53111Drónfutárcpp14Runtime error 30/100601ms66404 KiB
#include <bits/stdc++.h>
using namespace std;

#define rand() (rand() | rand() << 16)

#define INF (INT_MAX - 5)

int prim(vector<vector<pair<int, int>>>& graph, int startVertex) {
    int N = graph.size();
    // Priority queue to store the edges and their weights
    priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
    // Create a vector to track visited vertices
    vector<bool> visited(N, false);
    // Add the starting vertex to the priority queue
    pq.push(make_pair(0, startVertex));
    vector<pair<int, int>> mst;  // Minimum spanning tree
    while (!pq.empty()) {
        // Extract the minimum weight edge from the priority queue
        pair<int, int> curr = pq.top();
        pq.pop();
        int weight = curr.first;
        int vertex = curr.second;
        if (visited[vertex]) {
            continue;
        }
        // Mark the current vertex as visited
        visited[vertex] = true;
        // Add the current edge to the minimum spanning tree
        mst.push_back(make_pair(weight, vertex));
        // Iterate through all adjacent vertices of the current vertex
        for (pair<int, int>& edge : graph[vertex]) {
            int adjVertex = edge.first;
            int adjWeight = edge.second;
            if (!visited[adjVertex]) {
                // Add the adjacent vertex and its weight to the priority queue
                pq.push(make_pair(adjWeight, adjVertex));
            }
        }
    }
    // Find the maximum weight among the edges in the MST
    int maxWeight = 0;
    for (pair<int, int>& edge : mst) {
        maxWeight = max(maxWeight, edge.first);
    }
    return maxWeight;
}

struct Pt { int x, y; };

int c0(Pt p) { return - p.x + p.y; }
int c1(Pt p) { return + p.x - p.y; }
int c2(Pt p) { return - p.x - p.y; }
int c3(Pt p) { return + p.x + p.y; }
int c4(Pt p) { return + p.x - p.y; }
int c5(Pt p) { return - p.x + p.y; }
int c6(Pt p) { return + p.x + p.y; }
int c7(Pt p) { return - p.x - p.y; }

int d0(Pt p) { return + p.x + p.y; }
int d1(Pt p) { return + p.x + p.y; }
int d2(Pt p) { return - p.x + p.y; }
int d3(Pt p) { return - p.x + p.y; }
int d4(Pt p) { return - p.x - p.y; }
int d5(Pt p) { return - p.x - p.y; }
int d6(Pt p) { return + p.x - p.y; }
int d7(Pt p) { return + p.x - p.y; }

int l0(Pt p) { return p.y;	}
int h0(Pt p) { return INF;	}

int l1(Pt p) { return p.x;	}
int h1(Pt p) { return INF;	}

int l2(Pt p) { return -INF;	}
int h2(Pt p) { return p.x;	}

int l3(Pt p) { return p.y;	}
int h3(Pt p) { return INF;	}

int l4(Pt p) { return -INF;	}
int h4(Pt p) { return p.y;	}

int l5(Pt p) { return -INF;	}
int h5(Pt p) { return p.x;	}

int l6(Pt p) { return p.x;	}
int h6(Pt p) { return INF;	}

int l7(Pt p) { return -INF;	}
int h7(Pt p) { return p.y;	}

int (* cv[8])(Pt p) = {c0, c1, c2, c3, c4, c5, c6, c7};
int (* dv[8])(Pt p) = {d0, d1, d2, d3, d4, d5, d6, d7};
int (* lv[8])(Pt p) = {l0, l1, l2, l3, l4, l5, l6, l7};
int (* hv[8])(Pt p) = {h0, h1, h2, h3, h4, h5, h6, h7};
int (* ev[8])(Pt p) = {l0, l1, h2, l3, h4, h5, l6, h7};

int dist(Pt a, Pt b) { return abs(a.x - b.x) + abs(a.y - b.y); }

int bsgi(const vector<Pt>& v,  const vector<int>& vi, int (* e)(Pt p), int x) {
	int l = 0, h = vi.size();
	while (l != h) {
		int m = (l + h) / 2;
		if (e(v[vi[m]]) > x) {
			h = m;
		}
		else {
			l = m + 1;
		}
	}
	return h;
}

int bsli(const vector<Pt>& v, const vector<int>& vi, int (* e)(Pt p), int x) {
	int l = -1, h = vi.size() - 1;
	while (l != h) {
		int m = (l + h + 1) / 2;
		if (e(v[vi[m]]) < x) {
			l = m;
		}
		else {
			h = m - 1;
		}
	}
	return h;
}

int main() {
	int N = 40000;
	cin >> N;
	vector<Pt> v(N);
	vector<int> w(N);
	iota(w.begin(), w.end(), 0);
	vector<vector<pair<int, int>>> g(N);
	vector<vector<int>> st(N * 4);
	vector<vector<int>> pref(N * 4);
	for (int i = 0; i < N; i++) {
//		v[i].x = rand() % 1000000000 + 1, v[i].y = rand() % 1000000000 + 1;
		cin >> v[i].x >> v[i].y;
	}
	for (int q = 0; q < 8; q++) {
		if (q == 0 || q == 3 || q == 4 || q == 7) {
			sort(w.begin(), w.end(), [&](auto a, auto b) { return v[a].y < v[b].y; });
		}
		else {
			sort(w.begin(), w.end(), [&](auto a, auto b) { return v[a].x < v[b].x; });
		}
		function<void(int, int, int)> init = [&](int i, int l, int r) {
			st[i].assign(w.begin() + l, w.begin() + r + 1);
			sort(st[i].begin(), st[i].end(), [&](auto a, auto b) { return cv[q](v[a]) < cv[q](v[b]); });
			pref[i].resize(r - l + 1);
			for (int j = 0, m = j; j < r - l + 1; j++) {
				if (dv[q](v[st[i][j]]) < dv[q](v[st[i][m]])) {
					m = j;
				}
				pref[i][j] = st[i][m];
			}
			if (l == r) {
				return;
			}
			init(i * 2 + 1, l, (l + r) / 2);
			init(i * 2 + 2, (l + r) / 2 + 1, r);
		};
		init(0, 0, N - 1);
		for (int p = 0; p < N; p++) {
			int L = bsgi(v, w, ev[q], lv[q](v[p]) - (q % 2 == 0));
			int R = bsli(v, w, ev[q], hv[q](v[p]) + (q % 2 == 0));
			if (L > R) {
				continue;
			}
			int res = -1;
			function<void(int, int, int)> query = [&](int i, int l, int r) {
				if (r < L || l > R) {
					return;
				}
				if (l >= L && r <= R) {
					int a = bsli(v, st[i], cv[q], cv[q](v[p]) + (q % 2 == 1));
					if (a >= 0 && (res == -1 || dv[q](v[pref[i][a]]) < dv[q](v[res]))) {
						res = pref[i][a];
					}
					return;
				}
				query(i * 2 + 1, l, (l + r) / 2);
				query(i * 2 + 2, (l + r) / 2 + 1, r);
			};
			query(0, 0, N - 1);
			if (res != -1) {
				g[p].push_back({res, dv[q](v[res]) - dv[q](v[p])});
				g[res].push_back({p, dv[q](v[res]) - dv[q](v[p])});
			}
		}
	}
	cout << prim(g, 0) << '\n';
	return 0;
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted3ms2160 KiB
2Runtime error231ms66404 KiB
subtask215/15
3Accepted19ms3804 KiB
4Accepted19ms4204 KiB
5Accepted19ms4396 KiB
6Accepted19ms4292 KiB
7Accepted6ms3596 KiB
subtask315/15
8Accepted19ms3804 KiB
9Accepted19ms4204 KiB
10Accepted19ms4396 KiB
11Accepted19ms4292 KiB
12Accepted6ms3596 KiB
13Accepted9ms4288 KiB
14Accepted19ms4880 KiB
15Accepted19ms4916 KiB
16Accepted19ms4904 KiB
17Accepted19ms5208 KiB
subtask40/35
18Accepted19ms3804 KiB
19Accepted19ms4204 KiB
20Accepted19ms4396 KiB
21Accepted19ms4292 KiB
22Accepted6ms3596 KiB
23Accepted9ms4288 KiB
24Accepted19ms4880 KiB
25Accepted19ms4916 KiB
26Accepted19ms4904 KiB
27Accepted19ms5208 KiB
28Accepted19ms5448 KiB
29Time limit exceeded600ms17668 KiB
30Time limit exceeded565ms21096 KiB
31Time limit exceeded565ms21532 KiB
32Time limit exceeded601ms26600 KiB
subtask50/35
33Accepted19ms3804 KiB
34Accepted19ms4204 KiB
35Accepted19ms4396 KiB
36Accepted19ms4292 KiB
37Accepted6ms3596 KiB
38Accepted9ms4288 KiB
39Accepted19ms4880 KiB
40Accepted19ms4916 KiB
41Accepted19ms4904 KiB
42Accepted19ms5208 KiB
43Accepted19ms5448 KiB
44Time limit exceeded600ms17668 KiB
45Time limit exceeded565ms21096 KiB
46Time limit exceeded565ms21532 KiB
47Time limit exceeded601ms26600 KiB
48Time limit exceeded573ms31864 KiB
49Runtime error218ms64100 KiB
50Runtime error212ms64076 KiB
51Runtime error217ms64056 KiB
52Runtime error218ms64028 KiB