#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;; ++i) {
if(p == n) break;
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;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
640 KB |
Output is correct |
2 |
Correct |
5 ms |
768 KB |
Output is correct |
3 |
Correct |
3 ms |
768 KB |
Output is correct |
4 |
Correct |
4 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 |
3 ms |
768 KB |
Output is correct |
9 |
Correct |
3 ms |
768 KB |
Output is correct |
10 |
Correct |
3 ms |
768 KB |
Output is correct |
11 |
Correct |
4 ms |
768 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
896 KB |
Output is correct |
2 |
Correct |
4 ms |
896 KB |
Output is correct |
3 |
Correct |
6 ms |
1016 KB |
Output is correct |
4 |
Correct |
5 ms |
1024 KB |
Output is correct |
5 |
Correct |
5 ms |
1152 KB |
Output is correct |
6 |
Correct |
5 ms |
1152 KB |
Output is correct |
7 |
Correct |
5 ms |
1280 KB |
Output is correct |
8 |
Correct |
5 ms |
1200 KB |
Output is correct |
9 |
Correct |
5 ms |
1152 KB |
Output is correct |
10 |
Correct |
6 ms |
1152 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
36 ms |
4180 KB |
Output is correct |
2 |
Correct |
35 ms |
4316 KB |
Output is correct |
3 |
Correct |
89 ms |
9452 KB |
Output is correct |
4 |
Correct |
64 ms |
9580 KB |
Output is correct |
5 |
Correct |
118 ms |
18148 KB |
Output is correct |
6 |
Correct |
135 ms |
18152 KB |
Output is correct |
7 |
Correct |
149 ms |
18528 KB |
Output is correct |
8 |
Correct |
134 ms |
18036 KB |
Output is correct |
9 |
Correct |
145 ms |
18400 KB |
Output is correct |
10 |
Correct |
187 ms |
23392 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
39 ms |
5492 KB |
Output is correct |
2 |
Correct |
34 ms |
5188 KB |
Output is correct |
3 |
Correct |
114 ms |
12524 KB |
Output is correct |
4 |
Correct |
90 ms |
11536 KB |
Output is correct |
5 |
Correct |
220 ms |
24292 KB |
Output is correct |
6 |
Correct |
162 ms |
20448 KB |
Output is correct |
7 |
Correct |
218 ms |
24544 KB |
Output is correct |
8 |
Correct |
181 ms |
20832 KB |
Output is correct |
9 |
Correct |
185 ms |
20064 KB |
Output is correct |
10 |
Correct |
141 ms |
19804 KB |
Output is correct |