#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
map<pair<int,int>, bool> exists;
map<pair<int,int>, int> node_of;
int sz[100000];
int sub_sz[100000];
bool vis[100000];
int dfs(pair<int,int> u)
{
vis[node_of[u]] = true;
int cur_x = u.first;
sub_sz[node_of[u]] = sz[node_of[u]];
while (exists[{++cur_x, u.second}] == true)
{
if (exists[{cur_x, u.second - 1}] == true && !vis[node_of[{cur_x, u.second - 1}]])
sub_sz[node_of[u]] += dfs({cur_x, u.second - 1});
if (exists[{cur_x, u.second + 1}] == true && !vis[node_of[{cur_x, u.second + 1}]])
sub_sz[node_of[u]] += dfs({cur_x, u.second + 1});
}
cur_x = u.first + 1;
while (exists[{--cur_x, u.second}] == true)
{
if (exists[{cur_x, u.second - 1}] == true && !vis[node_of[{cur_x, u.second - 1}]])
sub_sz[node_of[u]] += dfs({cur_x, u.second - 1});
if (exists[{cur_x, u.second + 1}] == true && !vis[node_of[{cur_x, u.second + 1}]])
sub_sz[node_of[u]] += dfs({cur_x, u.second + 1});
}
return sub_sz[node_of[u]];
}
int solve(int N, int *X, int *Y)
{
exists.clear();
node_of.clear();
for (int i = 0; i < N; i++)
exists[{X[i], Y[i]}] = true;
int node = 0;
for (int i = 0; i < N; i++)
{
if (node_of[{X[i], Y[i]}] != 0) continue;
node_of[{X[i], Y[i]}] = ++node;
int cur_x = X[i];
while (exists[{--cur_x, Y[i]}] == true)
node_of[{cur_x, Y[i]}] = node;
cur_x = X[i];
while (exists[{++cur_x, Y[i]}] == true)
node_of[{cur_x, Y[i]}] = node;
}
for (int i = 1; i <= node; i++)
sz[i] = sub_sz[i] = vis[i] = 0;
for (int i = 0; i < N; i++)
sz[node_of[{X[i], Y[i]}]]++;
dfs({X[0], Y[0]});
ll res = 0;
for (int i = 2; i <= node; i++)
res += sub_sz[i] * (N - sub_sz[i]) % 1'000'000'000, res %= 1'000'000'000;
return res;
}
int DistanceSum(int N, int *X, int *Y)
{
return (solve(N, X, Y) + solve(N, Y, X)) % 1'000'000'000;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 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 |
340 KB |
Output is correct |
7 |
Correct |
2 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
468 KB |
Output is correct |
2 |
Correct |
3 ms |
468 KB |
Output is correct |
3 |
Correct |
5 ms |
596 KB |
Output is correct |
4 |
Correct |
4 ms |
596 KB |
Output is correct |
5 |
Correct |
8 ms |
632 KB |
Output is correct |
6 |
Correct |
7 ms |
596 KB |
Output is correct |
7 |
Correct |
7 ms |
724 KB |
Output is correct |
8 |
Correct |
7 ms |
596 KB |
Output is correct |
9 |
Correct |
7 ms |
596 KB |
Output is correct |
10 |
Correct |
6 ms |
596 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
82 ms |
3228 KB |
Output is correct |
2 |
Correct |
88 ms |
3196 KB |
Output is correct |
3 |
Correct |
205 ms |
7140 KB |
Output is correct |
4 |
Correct |
228 ms |
7256 KB |
Output is correct |
5 |
Incorrect |
601 ms |
13932 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
87 ms |
4296 KB |
Output is correct |
2 |
Correct |
84 ms |
3912 KB |
Output is correct |
3 |
Correct |
388 ms |
10156 KB |
Output is correct |
4 |
Correct |
278 ms |
9056 KB |
Output is correct |
5 |
Incorrect |
968 ms |
19976 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |