Submission #31121

#TimeUsernameProblemLanguageResultExecution timeMemory
31121h0ngjun7Ideal city (IOI12_city)C++14
100 / 100
436 ms19068 KiB
#include <vector> #include <map> #include <set> #include <algorithm> using namespace std; const int mod = 1e9; const int MAXN = 1e5 + 5; typedef long long ll; int N; struct XY { int x, y; XY() {} XY(int _x, int _y) { x = _x, y = _y; } bool operator < (const XY &a) const { if (x != a.x) return x < a.x; return y < a.y; } }; struct PT { int x, y, k; PT() {} PT(int _x, int _y, int _k) { x = _x, y = _y; k = _k; } } p[MAXN]; struct SEG { int s, e; }; int u[MAXN], us[MAXN]; int f(int x) { if (x == u[x]) return x; return (u[x] = f(u[x])); } ll ans = 0; int sub[MAXN]; vector <int> G[MAXN]; set <pair<int, int>> S; void dfs(int x, int par) { for (auto &y : G[x]) { if (y == par) continue; dfs(y, x); sub[x] += sub[y]; } sub[x] += us[x]; ans += 1LL * sub[x] * (N - sub[x]); ans %= mod; } void solve() { map <XY, int> mp; sort(p, p + N, [&](PT a, PT b) { return (a.x != b.x) ? a.x < b.x : a.y < b.y; }); for (int i = 1; i <= N; i++) { u[i] = i, us[i] = 1; G[i].clear(); sub[i] = 0; } S.clear(); for (int i = 0; i < N; i++) mp[XY(p[i].x, p[i].y)] = p[i].k; for (int i = 0; i < N; i++) { int j = i; vector <int> Y; while (j < N && p[i].x == p[j].x) Y.push_back(p[j++].y); SEG L; L.s = L.e = Y[0]; for (int j = 1; j < Y.size(); j++) { if (L.e + 1 == Y[j]) { L.e++; int A = f(mp[XY(p[i].x, Y[j] - 1)]); int B = f(mp[XY(p[i].x, Y[j])]); if (A != B) { u[A] = B; us[B] += us[A]; } } else L.s = L.e = Y[j]; } for (int k = i; k < j; k++) { if (mp.count(XY(p[i].x - 1, p[k].y))) { int A = f(mp[XY(p[i].x - 1, p[k].y)]); int B = f(p[k].k); if (A != B && !S.count({ A, B })) { G[A].push_back(B); G[B].push_back(A); S.insert({ A, B }); S.insert({ B, A }); } } } i = j - 1; } for (int i = 1; i <= N; i++) { if (f(i) == i) { dfs(i, -1); break; } } } int DistanceSum(int N, int *X, int *Y) { ::N = N; ans = 0; int mx = X[0], my = Y[0]; for (int i = 0; i < N; i++) { mx = min(mx, X[i]); my = min(my, Y[i]); } for (int i = 0; i < N; i++) p[i] = PT(X[i] - mx, Y[i] - my, i + 1); solve(); for (int i = 0; i < N; i++) swap(p[i].x, p[i].y); solve(); return (int)(ans); }

Compilation message (stderr)

city.cpp: In function 'void solve()':
city.cpp:65:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for (int j = 1; j < Y.size(); j++) {
                     ^
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...