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/extc++.h"
using namespace std;
template <typename T>
void dbgh(const T& t) {
cerr << t << endl;
}
template <typename T, typename... U>
void dbgh(const T& t, const U&... u) {
cerr << t << " | ";
dbgh(u...);
}
#ifdef DEBUG
#define dbg(...) \
cerr << "L" << __LINE__ << " [" << #__VA_ARGS__ << "]: "; \
dbgh(__VA_ARGS__);
#else
#define dbg(...)
#define cerr \
if (false) \
cerr
#endif
#define endl "\n"
#define long int64_t
#define sz(x) int((x).size())
const long mod = 1e9;
const int dx[] = {-1, 1, 0, 0}, dy[] = {0, 0, -1, 1};
int bf(int n, int* arrx, int* arry) {
map<pair<int, int>, int> m;
for (int i = 0; i < n; i++) {
m[{arrx[i], arry[i]}] = i;
}
vector<int> graph[n];
for (int i = 0; i < n; i++) {
int x = arrx[i], y = arry[i];
for (int k = 0; k < 4; k++) {
auto it = m.find({x + dx[k], y + dy[k]});
if (it != m.end()) {
int u = it->second;
graph[i].push_back(u);
graph[u].push_back(i);
}
}
}
long ans = 0;
for (int i = 0; i < n; i++) {
int dist[n];
memset(dist, -1, sizeof(dist));
queue<int> q;
auto push = [&](int u, int d) -> void {
if (dist[u] == -1) {
dist[u] = d;
q.push(u);
}
};
push(i, 0);
while (sz(q)) {
int u = q.front();
q.pop();
for (auto& v : graph[u]) {
push(v, dist[u] + 1);
}
}
ans += accumulate(dist, dist + n, 0);
}
return ans / 2 % mod;
}
int DistanceSum(int n, int *arrx, int *arry) {
if (n <= 2000) {
return bf(n, arrx, arry);
}
}
Compilation message (stderr)
city.cpp: In function 'int DistanceSum(int, int*, int*)':
city.cpp:79:1: warning: control reaches end of non-void function [-Wreturn-type]
79 | }
| ^
# | 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... |