Submission #577238

#TimeUsernameProblemLanguageResultExecution timeMemory
577238Justin1Ideal city (IOI12_city)C++14
23 / 100
96 ms41512 KiB
#include <bits/stdc++.h> #define f first #define s second #define ll long long #define pii pair<int,int> using namespace std; unordered_map<int,int> ar[100005], v[100005]; ll ans = 0, n; ll cntX[100005], cntY[100005]; void dfs(int x, int y, ll sum) { ans += sum; v[x][y] = 1; if (!v[x-1][y] && ar[x-1][y]) dfs(x-1,y,sum+cntX[n]-2*cntX[x-1]); if (!v[x+1][y] && ar[x+1][y]) dfs(x+1,y,sum-cntX[n]+2*cntX[x]); if (!v[x][y-1] && ar[x][y-1]) dfs(x,y-1,sum+cntY[n]-2*cntY[y-1]); if (!v[x][y+1] && ar[x][y+1]) dfs(x,y+1,sum-cntY[n]+2*cntY[y]); } int DistanceSum(int N, int *X, int *Y) { n = N; int mnX = INT_MAX, mnY = INT_MAX; for (int i = 0; i < N; i++) mnX = min(mnX, X[i]); for (int i = 0; i < N; i++) mnY = min(mnY, Y[i]); for (int i = 0; i < N; i++) X[i] -= mnX-1; for (int i = 0; i < N; i++) Y[i] -= mnY-1; for (int i = 0; i < N; i++) cntX[X[i]]++, cntY[Y[i]]++; for (int i = 1; i <= N; i++) cntX[i] += cntX[i-1], cntY[i] += cntY[i-1]; for (int i = 0; i < N; i++) ar[X[i]][Y[i]] = 1; int tmp = 0; for (int i = 1; i < N; i++) tmp += abs(X[i]-X[0]) + abs(Y[i]-Y[0]); dfs(X[0], Y[0], tmp); return (ans/2) % 1'000'000'000; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...