This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
typedef long long i64;
const int mod = 1e9;
const int nmax = 1e5;
const int dl[4] = {-1, 0, 0, 1};
const int dc[4] = {0, -1, 1, 0};
queue<pair<int, int>> q;
bool viz[2000 + 1];
int ind[2000 + 1][2000 + 1];
int brut (int N, int *X, int *Y) {
int mnx = 1 << 30;
int mny = 1 << 30;
for (int i = 0; i < N; ++ i) {
mnx = min(mnx, X[i]);
mny = min(mny, Y[i]);
}
for (int i = 0; i < N +1; ++ i)
for (int j = 0; j < N + 1; ++ j)
ind[i][j] = -1;
for (int i = 0; i < N; ++ i) {
X[i] -= (mnx - 1);
Y[i] -= (mny - 1);
ind[X[i]][Y[i]] = i;
}
i64 ans = 0;
for (int i = 0; i < N; ++ i) {
memset(viz, 0, sizeof(viz));
q.push({i, 0});
viz[i] = 1;
while (!q.empty()) {
int x = q.front().first;
int y = q.front().second;
q.pop();
ans += y;
for (int k = 0; k <4; ++ k) {
int a = X[x] + dl[k], b = Y[x] + dc[k];
if (ind[a][b] != -1) {
int t = ind[a][b];
if (viz[t] == 1)
continue;
viz[t] = 1;
q.push({t, y + 1});
}
}
}
}
ans /= 2;
return ans % mod;
}
i64 solve (vector<int> &v) {
sort(v.begin(), v.end());
i64 ans = 0;
i64 sum = 0;
for (auto i : v)
sum += i;
i64 m = 0;
for (int i = 0; i < v.size(); ++ i) {
sum -= v[i];
ans += 1LL * v[i] * i - m + sum - 1LL * v[i] * (v.size() - 1 - i);
m += v[i];
}
return ans;
}
int DistanceSum(int N, int *X, int *Y) {
if (N <= 2000) {
return brut(N, X, Y);
} else {
i64 ans = 0;
vector<int> v;
for (int i = 0; i < N; ++ i)
v.push_back(X[i]);
ans += solve(v);
v.clear();
for (int i = 0; i < N; ++ i)
v.push_back(Y[i]);
ans += solve(v);
return (ans / 2) % mod;
}
}
Compilation message (stderr)
city.cpp: In function 'i64 solve(std::vector<int>&)':
city.cpp:75:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < v.size(); ++ i) {
~~^~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |