#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll res = 0;
void sum(vector<ll> &x, vector<ll> &preX, ll X) {
ll idx = lower_bound(x.begin(), x.end(), X) - x.begin();
if (idx) {
res += (idx + 1) * X - preX[idx];
}
if (idx != preX.size()) {
res += (preX.back() - (!idx ? 0 : preX[idx - 1])) - 1ll * (preX.size() - idx) * X;
}
}
vector<ll> preX, preY, x, y;
map< pair<int, int> , int > cost, got;
int dx[4] = {0, 0, -1, 1};
int dy[4] = { -1, 1, 0, 0};
int DistanceSum(int N, int *X, int *Y) {
for (int i = 0; i < N; ++i) {
preX.push_back(X[i]);
x.push_back(X[i]);
}
for (int i = 0; i < N; ++i) {
preY.push_back(Y[i]);
y.push_back(Y[i]);
}
if (N <= 2000) {
for (int i = 0 ; i < N ; i++) {
got[make_pair(X[i], Y[i])] = 1;
}
for (int i = 0 ; i < N ; i++) {
queue< pair<int, int> > q;
cost.clear();
q.push(make_pair(X[i], Y[i]));
cost[make_pair(X[i], Y[i])];
while (q.size()) {
pair<int, int> src = q.front();
q.pop();
for (int p = 0 ; p < 4 ; p++) {
int newX = src.first + dx[p];
int newY = src.second + dy[p];
if (got.find(make_pair(newX, newY)) == got.end())
continue;
if (cost.find(make_pair(newX, newY)) != cost.end())
continue;
cost[make_pair(newX, newY)] = cost[src] + 1;
q.push(make_pair(newX, newY));
}
}
for (int j = i + 1 ; j < N ; j++) {
res += cost[make_pair(X[j], Y[j])];
}
}
return res;
}
sort(x.begin(), x.end());
sort(y.begin(), y.end());
sort(preX.begin(), preX.end());
sort(preY.begin(), preY.end());
for (int i = 1 ; i < preX.size(); ++i) {
preX[i] += preX[i - 1];
}
for (int i = 1 ; i < preY.size(); ++i) {
preY[i] += preY[i - 1];
}
for (int i = 0 ; i < N ; i++) {
sum(x, preX, X[i]);
sum(y, preY, Y[i]);
}
return (res / 2) % 1000000000;
}
Compilation message
city.cpp: In function 'void sum(std::vector<long long int>&, std::vector<long long int>&, ll)':
city.cpp:13:10: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if (idx != preX.size()) {
^
city.cpp: In function 'int DistanceSum(int, int*, int*)':
city.cpp:66:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 1 ; i < preX.size(); ++i) {
^
city.cpp:69:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 1 ; i < preY.size(); ++i) {
^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
2168 KB |
Output is correct |
2 |
Correct |
0 ms |
2168 KB |
Output is correct |
3 |
Correct |
0 ms |
2168 KB |
Output is correct |
4 |
Correct |
3 ms |
2168 KB |
Output is correct |
5 |
Correct |
6 ms |
2168 KB |
Output is correct |
6 |
Correct |
26 ms |
2168 KB |
Output is correct |
7 |
Correct |
26 ms |
2168 KB |
Output is correct |
8 |
Correct |
26 ms |
2168 KB |
Output is correct |
9 |
Correct |
26 ms |
2168 KB |
Output is correct |
10 |
Correct |
29 ms |
2168 KB |
Output is correct |
11 |
Correct |
29 ms |
2168 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
913 ms |
2300 KB |
Output is correct |
2 |
Correct |
886 ms |
2300 KB |
Output is correct |
3 |
Execution timed out |
1000 ms |
2444 KB |
Execution timed out |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
3556 KB |
Output is correct |
2 |
Correct |
13 ms |
3556 KB |
Output is correct |
3 |
Correct |
43 ms |
5024 KB |
Output is correct |
4 |
Correct |
36 ms |
5024 KB |
Output is correct |
5 |
Correct |
93 ms |
8496 KB |
Output is correct |
6 |
Correct |
83 ms |
8496 KB |
Output is correct |
7 |
Correct |
93 ms |
8496 KB |
Output is correct |
8 |
Correct |
73 ms |
8496 KB |
Output is correct |
9 |
Correct |
73 ms |
8496 KB |
Output is correct |
10 |
Correct |
73 ms |
8496 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
13 ms |
3556 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |