#include <bits/stdc++.h>
#define long long long
#define all(x) (x).begin(), (x).end()
#define pii pair<int, int>
#define x first
#define y second
using namespace std;
const int M = 1e9;
const int N = 1e5+5;
vector<set<int> > g;
int ans, sz[N];
pii dfs(int u, int p) {
pii now(sz[u], 0);
for(auto v : g[u]) if(v != p) {
pii z = dfs(v, u);
ans = (ans + 1ll * now.x * z.y + 1ll * z.x * now.y) % M;
now.x = (now.x + z.x) % M, now.y = (now.y + z.y) % M;
}
return pii(now.x, (now.y + now.x) % M);
}
int f(int n, int x[], int y[]) {
g = vector<set<int> >(n+1);
memset(sz, 0, sizeof sz);
vector<pii> vec;
for(int i = 0; i < n; ++i) vec.emplace_back(x[i], y[i]);
sort(all(vec));
map<pii, int> Mp;
for(int i = 1, p = 0; p != n; ++i) {
do {
int z = Mp[pii(vec[p].x-1, vec[p].y)];
if(z) g[z].emplace(i), g[i].emplace(z);
Mp[vec[p++]] = i, sz[i]++;
} while(p < n && vec[p-1].x == vec[p].x && vec[p-1].y + 1 == vec[p].y);
}
ans = 0, dfs(1, 0);
return ans % M;
}
int DistanceSum(int n, int x[], int y[]) {
return (f(n, x, y) + f(n, y, x)) % M;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
768 KB |
Output is correct |
2 |
Correct |
2 ms |
768 KB |
Output is correct |
3 |
Correct |
2 ms |
768 KB |
Output is correct |
4 |
Correct |
3 ms |
768 KB |
Output is correct |
5 |
Correct |
3 ms |
768 KB |
Output is correct |
6 |
Correct |
3 ms |
768 KB |
Output is correct |
7 |
Correct |
3 ms |
768 KB |
Output is correct |
8 |
Correct |
4 ms |
768 KB |
Output is correct |
9 |
Correct |
3 ms |
768 KB |
Output is correct |
10 |
Correct |
1 ms |
768 KB |
Output is correct |
11 |
Correct |
3 ms |
768 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
1024 KB |
Output is correct |
2 |
Correct |
3 ms |
896 KB |
Output is correct |
3 |
Correct |
4 ms |
1152 KB |
Output is correct |
4 |
Correct |
5 ms |
1024 KB |
Output is correct |
5 |
Correct |
4 ms |
1152 KB |
Output is correct |
6 |
Correct |
6 ms |
1152 KB |
Output is correct |
7 |
Correct |
6 ms |
1152 KB |
Output is correct |
8 |
Correct |
6 ms |
1152 KB |
Output is correct |
9 |
Correct |
5 ms |
1152 KB |
Output is correct |
10 |
Correct |
6 ms |
1152 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
62 ms |
4080 KB |
Output is correct |
2 |
Correct |
25 ms |
4188 KB |
Output is correct |
3 |
Correct |
67 ms |
9196 KB |
Output is correct |
4 |
Correct |
76 ms |
9196 KB |
Output is correct |
5 |
Correct |
122 ms |
17504 KB |
Output is correct |
6 |
Correct |
129 ms |
17496 KB |
Output is correct |
7 |
Correct |
149 ms |
17716 KB |
Output is correct |
8 |
Correct |
156 ms |
17376 KB |
Output is correct |
9 |
Correct |
172 ms |
17860 KB |
Output is correct |
10 |
Correct |
174 ms |
22692 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
38 ms |
5340 KB |
Output is correct |
2 |
Correct |
38 ms |
4956 KB |
Output is correct |
3 |
Correct |
92 ms |
12140 KB |
Output is correct |
4 |
Correct |
83 ms |
10988 KB |
Output is correct |
5 |
Correct |
197 ms |
23524 KB |
Output is correct |
6 |
Correct |
156 ms |
19808 KB |
Output is correct |
7 |
Correct |
226 ms |
23668 KB |
Output is correct |
8 |
Correct |
157 ms |
19936 KB |
Output is correct |
9 |
Correct |
152 ms |
19300 KB |
Output is correct |
10 |
Correct |
156 ms |
19004 KB |
Output is correct |