#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};
map<pair<int, int>, int> mp;
queue<pair<int, int>> q;
bool viz[2000 + 1];
int brut (int N, int *X, int *Y) {
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 (mp.find({a, b}) != mp.end()) {
int t = mp[{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) {
for (int i = 0; i < N; ++ i)
mp[{X[i], Y[i]}] = i;
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
city.cpp: In function 'i64 solve(std::vector<int>&)':
city.cpp:56:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < v.size(); ++ i) {
~~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
256 KB |
Output is correct |
2 |
Correct |
2 ms |
256 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
4 ms |
256 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
16 ms |
540 KB |
Output is correct |
7 |
Correct |
16 ms |
384 KB |
Output is correct |
8 |
Correct |
14 ms |
384 KB |
Output is correct |
9 |
Correct |
16 ms |
384 KB |
Output is correct |
10 |
Correct |
15 ms |
384 KB |
Output is correct |
11 |
Correct |
15 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
448 ms |
460 KB |
Output is correct |
2 |
Correct |
452 ms |
508 KB |
Output is correct |
3 |
Execution timed out |
1039 ms |
512 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
2176 KB |
Output is correct |
2 |
Correct |
21 ms |
2176 KB |
Output is correct |
3 |
Correct |
43 ms |
4748 KB |
Output is correct |
4 |
Correct |
52 ms |
4728 KB |
Output is correct |
5 |
Correct |
125 ms |
8928 KB |
Output is correct |
6 |
Correct |
118 ms |
8860 KB |
Output is correct |
7 |
Correct |
125 ms |
9032 KB |
Output is correct |
8 |
Correct |
99 ms |
8820 KB |
Output is correct |
9 |
Correct |
95 ms |
8792 KB |
Output is correct |
10 |
Correct |
101 ms |
8820 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
19 ms |
2236 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |