#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;
}
long pdiff(int n, int* arr) {
sort(arr, arr + n);
long ans = 0, psum = 0;
for (int i = 0; i < n; i++) {
ans += long(i) * arr[i] - psum;
psum += arr[i];
}
return ans;
}
int DistanceSum(int n, int *arrx, int *arry) {
if (n <= 2000) {
return bf(n, arrx, arry);
}
return (pdiff(n, arrx) + pdiff(n, arry)) % mod;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 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 |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 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 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
30 ms |
340 KB |
Output is correct |
2 |
Correct |
30 ms |
340 KB |
Output is correct |
3 |
Correct |
67 ms |
496 KB |
Output is correct |
4 |
Correct |
69 ms |
516 KB |
Output is correct |
5 |
Correct |
116 ms |
576 KB |
Output is correct |
6 |
Correct |
143 ms |
604 KB |
Output is correct |
7 |
Correct |
122 ms |
468 KB |
Output is correct |
8 |
Correct |
136 ms |
604 KB |
Output is correct |
9 |
Correct |
145 ms |
596 KB |
Output is correct |
10 |
Correct |
121 ms |
596 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
468 KB |
Output is correct |
2 |
Correct |
5 ms |
596 KB |
Output is correct |
3 |
Correct |
12 ms |
1092 KB |
Output is correct |
4 |
Correct |
12 ms |
980 KB |
Output is correct |
5 |
Correct |
26 ms |
1856 KB |
Output is correct |
6 |
Correct |
25 ms |
1748 KB |
Output is correct |
7 |
Correct |
33 ms |
1960 KB |
Output is correct |
8 |
Correct |
27 ms |
1844 KB |
Output is correct |
9 |
Correct |
23 ms |
1848 KB |
Output is correct |
10 |
Correct |
23 ms |
1836 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
5 ms |
468 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |