From b4b896930c5ad219de7e91b12ffbe0ef6c3bdfad Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 19 May 2024 17:29:53 +0000 Subject: [PATCH] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- content/5_Plat/Geo_Pri.mdx | 244 +++++++++++++++++++------------------ 1 file changed, 125 insertions(+), 119 deletions(-) diff --git a/content/5_Plat/Geo_Pri.mdx b/content/5_Plat/Geo_Pri.mdx index 249f9c288c..63230714ab 100644 --- a/content/5_Plat/Geo_Pri.mdx +++ b/content/5_Plat/Geo_Pri.mdx @@ -397,21 +397,24 @@ will run on $n^2$ edges, which causes TLE. ```cpp -#include #include +#include #include #include using namespace std; -struct point { int x, y, idx; }; +struct point { + int x, y, idx; +}; // BeginCodeSnip{DSU} class DisjointSets { -private: + private: vector parents; vector sizes; -public: + + public: DisjointSets(int size) : parents(size), sizes(size, 1) { for (int i = 0; i < size; i++) { parents[i] = i; } } @@ -438,137 +441,140 @@ public: }; // EncCodeSnip - // BeginCodeSnip{Binary jumping} class BinaryJumping { -private: - const int MAX_LOG = 20; - vector> up; - vector> val; - vector depth; -public: - BinaryJumping(vector>> &tree) { - up.resize((int)tree.size()); - depth.resize((int)tree.size()); - val.resize((int)tree.size()); - for(int i = 0; i < (int)tree.size(); i++) { - up[i].resize(MAX_LOG); - val[i].resize(MAX_LOG); - } - dfs(0, 0, tree); - } - void dfs(int node, int parent, vector>> &tree) { - depth[node] = depth[parent] + 1; - up[node][0] = parent; - for(int i = 1; i < MAX_LOG; i++) { - up[node][i] = up[up[node][i - 1]][i - 1]; - val[node][i] = max(val[node][i - 1], val[up[node][i - 1]][i - 1]); - } - for(auto [son, dist] : tree[node]) { - if(son == parent) { continue; } - val[son][0] = dist; - dfs(son, node, tree); - } - } - int jump(int x, int d) { - for(int i = 0; (1 << i) <= d; i++) { - if((1 << i) & d) { - x = up[x][i]; - } - } - return x; - } - int get_lca(int x, int y) { - if(depth[x] < depth[y]) { swap(x, y); } - x = jump(x, depth[x] - depth[y]); - if(x == y) { return x; } - for(int i = MAX_LOG; i >= 0; i--){ - if(up[x][i] != up[y][i]) { - x = up[x][i]; - y = up[y][i]; - } - } - return up[x][1]; - } - int query(int x, int y) { - int z = get_lca(x, y), ans = 0, d; - d = depth[x] - depth[z]; - for(int i = 0; (1 << i) <= d; i++){ - if((1 << i) & d) { - ans = max(ans, val[x][i]); - x = up[x][i]; - } - } - d = depth[y] - depth[z]; - for(int i = 0; (1 << i) <= d; i++){ - if((1 << i) & d) { - ans = max(ans, val[y][i]); - y = up[y][i]; - } - } - return ans; - } + private: + const int MAX_LOG = 20; + vector> up; + vector> val; + vector depth; + + public: + BinaryJumping(vector>> &tree) { + up.resize((int)tree.size()); + depth.resize((int)tree.size()); + val.resize((int)tree.size()); + for (int i = 0; i < (int)tree.size(); i++) { + up[i].resize(MAX_LOG); + val[i].resize(MAX_LOG); + } + dfs(0, 0, tree); + } + void dfs(int node, int parent, vector>> &tree) { + depth[node] = depth[parent] + 1; + up[node][0] = parent; + for (int i = 1; i < MAX_LOG; i++) { + up[node][i] = up[up[node][i - 1]][i - 1]; + val[node][i] = max(val[node][i - 1], val[up[node][i - 1]][i - 1]); + } + for (auto [son, dist] : tree[node]) { + if (son == parent) { continue; } + val[son][0] = dist; + dfs(son, node, tree); + } + } + int jump(int x, int d) { + for (int i = 0; (1 << i) <= d; i++) { + if ((1 << i) & d) { x = up[x][i]; } + } + return x; + } + int get_lca(int x, int y) { + if (depth[x] < depth[y]) { swap(x, y); } + x = jump(x, depth[x] - depth[y]); + if (x == y) { return x; } + for (int i = MAX_LOG; i >= 0; i--) { + if (up[x][i] != up[y][i]) { + x = up[x][i]; + y = up[y][i]; + } + } + return up[x][1]; + } + int query(int x, int y) { + int z = get_lca(x, y), ans = 0, d; + d = depth[x] - depth[z]; + for (int i = 0; (1 << i) <= d; i++) { + if ((1 << i) & d) { + ans = max(ans, val[x][i]); + x = up[x][i]; + } + } + d = depth[y] - depth[z]; + for (int i = 0; (1 << i) <= d; i++) { + if ((1 << i) & d) { + ans = max(ans, val[y][i]); + y = up[y][i]; + } + } + return ans; + } }; // EndCodeSnip // Manhattan distance int dist(const point &a, const point &b) { - return abs(a.x - b.x) + abs(a.y - b.y); + return abs(a.x - b.x) + abs(a.y - b.y); } vector> compute_mst(vector> edges) { - sort(edges.begin(), edges.end(), [&](pair a, pair b) { - return dist(a.first, a.second) < dist(b.first, b.second); - }); - DisjointSets ds((int)edges.size()); - vector> mst; - for(int i = 0; i < (int)edges.size(); i++) { - if(ds.connected(edges[i].first.idx, edges[i].second.idx)) { continue; } - ds.unite(edges[i].first.idx, edges[i].second.idx); - mst.push_back(edges[i]); - } - return mst; + sort(edges.begin(), edges.end(), + [&](pair a, pair b) { + return dist(a.first, a.second) < dist(b.first, b.second); + }); + DisjointSets ds((int)edges.size()); + vector> mst; + for (int i = 0; i < (int)edges.size(); i++) { + if (ds.connected(edges[i].first.idx, edges[i].second.idx)) { continue; } + ds.unite(edges[i].first.idx, edges[i].second.idx); + mst.push_back(edges[i]); + } + return mst; } void test_case() { - int n; - cin >> n; - vector points; - for(int i = 0; i < n; i++) { - int x, y; - cin >> x >> y; - points.push_back({x, y, i}); - } - vector> mst; - for(int i = 0; i < n; i++) { - for(int j = i + 1; j < n; j++) { - mst.push_back({points[i], points[j]}); - } - } - mst = compute_mst(mst); - vector>> tree(n); - for(pair edge : mst) { - tree[edge.first.idx].push_back({edge.second.idx, dist(points[edge.first.idx], points[edge.second.idx])}); - tree[edge.second.idx].push_back({edge.first.idx, dist(points[edge.first.idx], points[edge.second.idx])}); - // cout << edge.first.idx << ' ' << edge.second.idx << '\n'; - } - int q; - BinaryJumping lca(tree); - cin >> q; - while (q--) { - int x, y; - cin >> x >> y; - x--; y--; - cout << lca.query(x, y) << '\n'; - } - + int n; + cin >> n; + vector points; + for (int i = 0; i < n; i++) { + int x, y; + cin >> x >> y; + points.push_back({x, y, i}); + } + vector> mst; + for (int i = 0; i < n; i++) { + for (int j = i + 1; j < n; j++) { + mst.push_back({points[i], points[j]}); + } + } + mst = compute_mst(mst); + vector>> tree(n); + for (pair edge : mst) { + tree[edge.first.idx].push_back( + {edge.second.idx, + dist(points[edge.first.idx], points[edge.second.idx])}); + tree[edge.second.idx].push_back( + {edge.first.idx, + dist(points[edge.first.idx], points[edge.second.idx])}); + // cout << edge.first.idx << ' ' << edge.second.idx << '\n'; + } + int q; + BinaryJumping lca(tree); + cin >> q; + while (q--) { + int x, y; + cin >> x >> y; + x--; + y--; + cout << lca.query(x, y) << '\n'; + } } int main() { - int t; - cin >> t; - while(t--) { test_case(); } - return 0; + int t; + cin >> t; + while (t--) { test_case(); } + return 0; } ```