#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
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 |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
296 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
2 ms |
212 KB |
Output is correct |
7 |
Correct |
2 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
2 ms |
212 KB |
Output is correct |
10 |
Correct |
2 ms |
212 KB |
Output is correct |
11 |
Correct |
2 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
27 ms |
432 KB |
Output is correct |
2 |
Correct |
32 ms |
340 KB |
Output is correct |
3 |
Correct |
72 ms |
468 KB |
Output is correct |
4 |
Correct |
70 ms |
468 KB |
Output is correct |
5 |
Correct |
119 ms |
584 KB |
Output is correct |
6 |
Correct |
121 ms |
616 KB |
Output is correct |
7 |
Correct |
121 ms |
596 KB |
Output is correct |
8 |
Correct |
141 ms |
612 KB |
Output is correct |
9 |
Correct |
141 ms |
628 KB |
Output is correct |
10 |
Correct |
120 ms |
612 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1092 ms |
3448 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1086 ms |
3276 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |