Submission #103256

#TimeUsernameProblemLanguageResultExecution timeMemory
103256wxh010910Ideal city (IOI12_city)C++17
32 / 100
17 ms2936 KiB
#include <bits/stdc++.h> using namespace std; struct point { int x, y, id; point(int x = 0, int y = 0, int id = 0): x(x), y(y), id(id) { } }; int DistanceSum(int N, int* X, int* Y) { vector<point> p(N); for (int i = 0; i < N; ++i) { p[i] = point(X[i], Y[i], i); } sort(p.begin(), p.end(), [&](const point &a, const point &b) { return a.x < b.x || (a.x == b.x && a.y < b.y); }); vector<int> to_down(N); vector<int> down(N, -1); for (int i = 0; i < N; ++i) { if (i && p[i].x == p[i - 1].x && p[i].y == p[i - 1].y + 1) { to_down[p[i].id] = to_down[p[i - 1].id]; down[p[i].id] = p[i - 1].id; } else { to_down[p[i].id] = p[i].id; } } sort(p.begin(), p.end(), [&](const point &a, const point &b) { return a.y < b.y || (a.y == b.y && a.x < b.x); }); vector<int> to_left(N); vector<int> left(N, -1); for (int i = 0; i < N; ++i) { if (i && p[i].y == p[i - 1].y && p[i].x == p[i - 1].x + 1) { to_left[p[i].id] = to_left[p[i - 1].id]; left[p[i].id] = p[i - 1].id; } else { to_left[p[i].id] = p[i].id; } } vector<int> sz_down(N); for (int i = 0; i < N; ++i) { ++sz_down[to_down[i]]; } vector<int> sz_left(N); for (int i = 0; i < N; ++i) { ++sz_left[to_left[i]]; } long long ans = 0; vector<vector<int>> hor(N); for (int i = 0; i < N; ++i) { if (left[i] != -1 && (to_down[i] == i || to_down[left[i]] == left[i])) { hor[to_down[left[i]]].push_back(to_down[i]); hor[to_down[i]].push_back(to_down[left[i]]); } } function<void(int, int)> dfs_hor = [&](int x, int pr) { for (auto y : hor[x]) { if (y != pr) { dfs_hor(y, x); ans += (long long) sz_down[y] * (N - sz_down[y]); sz_down[x] += sz_down[y]; } } }; for (int i = 0; i < N; ++i) { if (sz_down[i]) { dfs_hor(i, -1); break; } } vector<vector<int>> ver(N); for (int i = 0; i < N; ++i) { if (down[i] != -1 && (to_left[i] == i || to_left[down[i]] == down[i])) { ver[to_left[down[i]]].push_back(to_left[i]); ver[to_left[i]].push_back(to_left[down[i]]); } } function<void(int, int)> dfs_ver = [&](int x, int pr) { for (auto y : ver[x]) { if (y != pr) { dfs_ver(y, x); ans += (long long) sz_left[y] * (N - sz_left[y]); sz_left[x] += sz_left[y]; } } }; for (int i = 0; i < N; ++i) { if (sz_left[i]) { dfs_ver(i, -1); break; } } return ans; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...