#include <bits/stdc++.h>
using namespace std;
struct point {
int x, y, id;
point(int x = 0, int y = 0, int id = 0): x(x), y(y), id(id) {
}
};
int DistanceSum(int N, int* X, int* Y) {
vector<point> p(N);
for (int i = 0; i < N; ++i) {
p[i] = point(X[i], Y[i], i);
}
sort(p.begin(), p.end(), [&](const point &a, const point &b) {
return a.x < b.x || (a.x == b.x && a.y < b.y);
});
vector<int> to_down(N);
vector<int> down(N, -1);
for (int i = 0; i < N; ++i) {
if (i && p[i].x == p[i - 1].x && p[i].y == p[i - 1].y + 1) {
to_down[p[i].id] = to_down[p[i - 1].id];
down[p[i].id] = p[i - 1].id;
} else {
to_down[p[i].id] = p[i].id;
}
}
sort(p.begin(), p.end(), [&](const point &a, const point &b) {
return a.y < b.y || (a.y == b.y && a.x < b.x);
});
vector<int> to_left(N);
vector<int> left(N, -1);
for (int i = 0; i < N; ++i) {
if (i && p[i].y == p[i - 1].y && p[i].x == p[i - 1].x + 1) {
to_left[p[i].id] = to_left[p[i - 1].id];
left[p[i].id] = p[i - 1].id;
} else {
to_left[p[i].id] = p[i].id;
}
}
vector<int> sz_down(N);
for (int i = 0; i < N; ++i) {
++sz_down[to_down[i]];
}
vector<int> sz_left(N);
for (int i = 0; i < N; ++i) {
++sz_left[to_left[i]];
}
long long ans = 0;
vector<vector<int>> hor(N);
for (int i = 0; i < N; ++i) {
if (left[i] != -1 && (to_down[i] == i || to_down[left[i]] == left[i])) {
hor[to_down[left[i]]].push_back(to_down[i]);
hor[to_down[i]].push_back(to_down[left[i]]);
}
}
function<void(int, int)> dfs_hor = [&](int x, int pr) {
for (auto y : hor[x]) {
if (y != pr) {
dfs_hor(y, x);
ans += (long long) sz_down[y] * (N - sz_down[y]);
sz_down[x] += sz_down[y];
}
}
};
for (int i = 0; i < N; ++i) {
if (sz_down[i]) {
dfs_hor(i, -1);
break;
}
}
vector<vector<int>> ver(N);
for (int i = 0; i < N; ++i) {
if (down[i] != -1 && (to_left[i] == i || to_left[down[i]] == down[i])) {
ver[to_left[down[i]]].push_back(to_left[i]);
ver[to_left[i]].push_back(to_left[down[i]]);
}
}
function<void(int, int)> dfs_ver = [&](int x, int pr) {
for (auto y : ver[x]) {
if (y != pr) {
dfs_ver(y, x);
ans += (long long) sz_left[y] * (N - sz_left[y]);
sz_left[x] += sz_left[y];
}
}
};
for (int i = 0; i < N; ++i) {
if (sz_left[i]) {
dfs_ver(i, -1);
break;
}
}
return ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
256 KB |
Output is correct |
2 |
Correct |
2 ms |
256 KB |
Output is correct |
3 |
Correct |
4 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
384 KB |
Output is correct |
8 |
Correct |
3 ms |
384 KB |
Output is correct |
9 |
Correct |
2 ms |
384 KB |
Output is correct |
10 |
Correct |
2 ms |
384 KB |
Output is correct |
11 |
Correct |
3 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
3 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
512 KB |
Output is correct |
4 |
Correct |
5 ms |
512 KB |
Output is correct |
5 |
Correct |
4 ms |
512 KB |
Output is correct |
6 |
Correct |
4 ms |
584 KB |
Output is correct |
7 |
Correct |
3 ms |
640 KB |
Output is correct |
8 |
Correct |
3 ms |
512 KB |
Output is correct |
9 |
Correct |
4 ms |
640 KB |
Output is correct |
10 |
Correct |
4 ms |
508 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
14 ms |
2432 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
17 ms |
2936 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |