#include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9;
const int MAX_N = 1e5;
using ll = long long;
set <int> gr[1 + MAX_N];
int sz[1 + MAX_N];
void clean(int n) {
for (int i = 1; i <= n; i++)
gr[i].clear(), sz[i] = 0;
}
void dfs_cnt(int node, int par, int &ans, int total_sz) {
for (int son : gr[node])
if (son != par) {
dfs_cnt(son, node, ans, total_sz);
sz[node] += sz[son];
}
ans += 1ll * sz[node] * (total_sz - sz[node]) % MOD;
ans %= MOD;
}
int solve(vector <pair <pair <int, int>, int>> v) {
int n = v.size();
sort(v.begin(), v.end());
int tag = 0;
for (int i = 0; i < n; i++) {
if (i > 0 && v[i].first.first == v[i - 1].first.first && v[i].first.second == v[i - 1].first.second + 1)
v[i].second = tag;
else
v[i].second = ++tag;
sz[tag]++;
}
for (int i = 0; i < n; i++)
swap(v[i].first.first, v[i].first.second);
sort(v.begin(), v.end());
for (int i = 0; i < n; i++) {
if (i > 0 && v[i].first.first == v[i - 1].first.first && v[i].first.second == v[i - 1].first.second + 1) {
int x = v[i].second, y = v[i - 1].second;
gr[x].insert(y);
gr[y].insert(x);
}
}
int ans = 0;
dfs_cnt(1, 0, ans, n);
clean(n);
return ans;
}
int DistanceSum(int N, int *X, int *Y) {
vector <pair <pair <int, int>, int>> v;
for (int i = 0; i < N; i++)
v.push_back({{X[i], Y[i]}, 0});
int ans = 0;
ans += solve(v);
for (int i = 0; i < N; i++)
swap(v[i].first.first, v[i].first.second);
ans += solve(v);
return ans % MOD;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4940 KB |
Output is correct |
2 |
Correct |
2 ms |
4992 KB |
Output is correct |
3 |
Correct |
2 ms |
4940 KB |
Output is correct |
4 |
Correct |
3 ms |
4940 KB |
Output is correct |
5 |
Correct |
2 ms |
4940 KB |
Output is correct |
6 |
Correct |
2 ms |
4940 KB |
Output is correct |
7 |
Correct |
2 ms |
4940 KB |
Output is correct |
8 |
Correct |
2 ms |
4940 KB |
Output is correct |
9 |
Correct |
3 ms |
4940 KB |
Output is correct |
10 |
Correct |
2 ms |
4996 KB |
Output is correct |
11 |
Correct |
2 ms |
5000 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5068 KB |
Output is correct |
2 |
Correct |
3 ms |
5068 KB |
Output is correct |
3 |
Correct |
3 ms |
5068 KB |
Output is correct |
4 |
Correct |
3 ms |
5068 KB |
Output is correct |
5 |
Correct |
4 ms |
5068 KB |
Output is correct |
6 |
Correct |
4 ms |
5128 KB |
Output is correct |
7 |
Correct |
4 ms |
5136 KB |
Output is correct |
8 |
Correct |
4 ms |
5068 KB |
Output is correct |
9 |
Correct |
4 ms |
5012 KB |
Output is correct |
10 |
Correct |
3 ms |
5068 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
6016 KB |
Output is correct |
2 |
Correct |
13 ms |
6012 KB |
Output is correct |
3 |
Correct |
29 ms |
7288 KB |
Output is correct |
4 |
Correct |
29 ms |
7352 KB |
Output is correct |
5 |
Correct |
60 ms |
9400 KB |
Output is correct |
6 |
Correct |
58 ms |
9496 KB |
Output is correct |
7 |
Correct |
56 ms |
9756 KB |
Output is correct |
8 |
Correct |
62 ms |
9412 KB |
Output is correct |
9 |
Correct |
63 ms |
9804 KB |
Output is correct |
10 |
Correct |
66 ms |
14648 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
6684 KB |
Output is correct |
2 |
Correct |
15 ms |
6456 KB |
Output is correct |
3 |
Correct |
39 ms |
9068 KB |
Output is correct |
4 |
Correct |
38 ms |
8432 KB |
Output is correct |
5 |
Correct |
81 ms |
13160 KB |
Output is correct |
6 |
Correct |
70 ms |
10920 KB |
Output is correct |
7 |
Correct |
92 ms |
13356 KB |
Output is correct |
8 |
Correct |
80 ms |
11068 KB |
Output is correct |
9 |
Correct |
68 ms |
10500 KB |
Output is correct |
10 |
Correct |
70 ms |
10416 KB |
Output is correct |