#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)
{
int cur_node = node_of[u];
vis[cur_node] = true;
int cur_x = u.first;
sub_sz[cur_node] = sz[cur_node];
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[cur_node] += dfs({cur_x, u.second - 1});
if (exists[{cur_x, u.second + 1}] == true && !vis[node_of[{cur_x, u.second + 1}]])
sub_sz[cur_node] += 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[cur_node] += dfs({cur_x, u.second - 1});
if (exists[{cur_x, u.second + 1}] == true && !vis[node_of[{cur_x, u.second + 1}]])
sub_sz[cur_node] += dfs({cur_x, u.second + 1});
}
return sub_sz[cur_node];
}
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 (int) 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 |
0 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 |
1 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 |
5 ms |
596 KB |
Output is correct |
5 |
Correct |
7 ms |
596 KB |
Output is correct |
6 |
Correct |
6 ms |
596 KB |
Output is correct |
7 |
Correct |
7 ms |
724 KB |
Output is correct |
8 |
Correct |
5 ms |
596 KB |
Output is correct |
9 |
Correct |
5 ms |
596 KB |
Output is correct |
10 |
Correct |
6 ms |
596 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
69 ms |
3028 KB |
Output is correct |
2 |
Correct |
78 ms |
3112 KB |
Output is correct |
3 |
Correct |
227 ms |
7040 KB |
Output is correct |
4 |
Correct |
222 ms |
7200 KB |
Output is correct |
5 |
Incorrect |
626 ms |
13868 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
88 ms |
4352 KB |
Output is correct |
2 |
Correct |
93 ms |
4016 KB |
Output is correct |
3 |
Correct |
303 ms |
10080 KB |
Output is correct |
4 |
Correct |
265 ms |
9012 KB |
Output is correct |
5 |
Incorrect |
877 ms |
19856 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |