#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MOD = 1e9;
const int MAXN = 2e5 + 3e2;
vector<int> g[MAXN];
vector<int> dx = {0, 1, 0, -1}, dy = {1, 0, -1, 0};
int DistanceSum(int N, int *X, int *Y) {
if (N <= 2000) {
map<pair<int, int>, int> mp;
for (int i = 0; i < N; i++) {
mp[{X[i], Y[i]}] = i;
}
for (int i = 0; i < N; i++) {
for (int j = 0; j < 4; j++) {
if (mp.find({X[i] + dx[j], Y[i] + dy[j]}) != mp.end()) {
g[i].push_back(mp[{X[i] + dx[j], Y[i] + dy[j]}]);
}
}
}
ll ans = 0;
for (int i = 0; i < N; i++) {
queue<int> q;
q.push(i);
vector<int> d(N, 1e9);
d[i] = 0;
while (!q.empty()) {
int x = q.front();
q.pop();
ans += d[x];
for (int y : g[x]) {
if (d[y] == 1e9) {
d[y] = d[x] + 1;
q.push(y);
}
}
}
}
ans >>= 1;
return ans % MOD;
}
sort(X, X + N);
sort(Y, Y + N);
ll ans = 0;
ll lf = 0, rg = accumulate(X, X + N, 0ll);
for (int i = 0; i < N; i++){
rg -= X[i];
ans += 1ll * i * X[i] - lf;
ans += rg - 1ll * (N - i - 1) * X[i];
lf += X[i];
}
lf = 0, rg = accumulate(Y, Y + N, 0ll);
for (int i = 0; i < N; i++){
rg -= Y[i];
ans += 1ll * i * Y[i] - lf;
ans += rg - 1ll * (N - i - 1) * Y[i];
lf += Y[i];
}
ans >>= 1;
return ans % MOD;
}
/*
7
3 3
4 3
4 4
4 5
4 6
5 3
5 4
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
4940 KB |
Output is correct |
3 |
Correct |
3 ms |
4940 KB |
Output is correct |
4 |
Correct |
3 ms |
4940 KB |
Output is correct |
5 |
Correct |
4 ms |
5008 KB |
Output is correct |
6 |
Correct |
4 ms |
5020 KB |
Output is correct |
7 |
Correct |
5 ms |
4940 KB |
Output is correct |
8 |
Correct |
4 ms |
4940 KB |
Output is correct |
9 |
Correct |
4 ms |
4940 KB |
Output is correct |
10 |
Correct |
4 ms |
4944 KB |
Output is correct |
11 |
Correct |
4 ms |
4960 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
29 ms |
5068 KB |
Output is correct |
2 |
Correct |
30 ms |
5108 KB |
Output is correct |
3 |
Correct |
66 ms |
5068 KB |
Output is correct |
4 |
Correct |
66 ms |
5068 KB |
Output is correct |
5 |
Correct |
140 ms |
5196 KB |
Output is correct |
6 |
Correct |
115 ms |
5200 KB |
Output is correct |
7 |
Correct |
125 ms |
5316 KB |
Output is correct |
8 |
Correct |
111 ms |
5196 KB |
Output is correct |
9 |
Correct |
107 ms |
5204 KB |
Output is correct |
10 |
Correct |
108 ms |
5316 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
5196 KB |
Output is correct |
2 |
Correct |
9 ms |
5348 KB |
Output is correct |
3 |
Correct |
18 ms |
5836 KB |
Output is correct |
4 |
Correct |
17 ms |
5708 KB |
Output is correct |
5 |
Correct |
35 ms |
6604 KB |
Output is correct |
6 |
Correct |
32 ms |
6468 KB |
Output is correct |
7 |
Correct |
34 ms |
6700 KB |
Output is correct |
8 |
Correct |
40 ms |
6540 KB |
Output is correct |
9 |
Correct |
33 ms |
6508 KB |
Output is correct |
10 |
Correct |
33 ms |
6524 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
9 ms |
5196 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |