#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9;
const int M = 1e5 + 10;
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, -1, 1};
vector<int> adj[M];
int comp[M], w[M], sub[M];
int ans = 0;
void dfs(int u, int par) {
sub[u] = w[u];
for(int v : adj[u]) if(v - par) {
dfs(v, u);
sub[u] += sub[v];
if(sub[u] >= mod)
sub[u] -= mod;
}
}
void solve(int N, int *X, int *Y) {
vector<pair<int, int> > v;
map<pair<int, int>, int> mp;
for(int i = 0; i < N; i++)
v.emplace_back(X[i], Y[i]);
sort(v.begin(), v.end());
for(int i = 0; i < N; i++)
mp[{v[i].first, v[i].second}] = i;
int idx = 0;
for(int i = 0; i < N; ) {
int x = v[i].first, j = i + 1;
comp[i] = idx;
while(j < N && v[j].second == v[j - 1].second + 1)
comp[j++] = idx;
w[idx++] = j - i;
i = j;
}
set<pair<int, int> > st;
for(int i = 0; i < N; i++) {
int x = v[i].first, y = v[i].second;
for(int j = 0; j < 4; j++) {
int xx = x + dx[j], yy = y + dy[j];
if(mp.count({xx, yy})) {
int a = comp[i], b = comp[mp[{xx, yy}]];
if(a > b) swap(a, b);
if(a != b) st.insert({a, b});
}
}
}
for(int i = 0; i < idx; i++)
adj[i].clear();
for(auto it : st) {
int u = it.first, v = it.second;
adj[u].push_back(v);
adj[v].push_back(u);
}
dfs(0, -1);
for(int i = 1; i < idx; i++) {
ans += sub[i] * 1ll * (N - sub[i]) % mod;
if(ans >= mod) ans -= mod;
}
}
int DistanceSum(int N, int *X, int *Y) {
solve(N, X, Y);
solve(N, Y, X);
return ans;
}
Compilation message
city.cpp: In function 'void solve(int, int*, int*)':
city.cpp:36:13: warning: unused variable 'x' [-Wunused-variable]
int x = v[i].first, j = i + 1;
^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
2680 KB |
Output is correct |
2 |
Correct |
4 ms |
2756 KB |
Output is correct |
3 |
Correct |
5 ms |
2892 KB |
Output is correct |
4 |
Correct |
5 ms |
2892 KB |
Output is correct |
5 |
Correct |
5 ms |
2892 KB |
Output is correct |
6 |
Correct |
5 ms |
3016 KB |
Output is correct |
7 |
Correct |
7 ms |
3016 KB |
Output is correct |
8 |
Correct |
5 ms |
3016 KB |
Output is correct |
9 |
Correct |
6 ms |
3016 KB |
Output is correct |
10 |
Correct |
6 ms |
3024 KB |
Output is correct |
11 |
Correct |
6 ms |
3024 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
3240 KB |
Output is correct |
2 |
Correct |
5 ms |
3240 KB |
Output is correct |
3 |
Correct |
8 ms |
3308 KB |
Output is correct |
4 |
Correct |
8 ms |
3308 KB |
Output is correct |
5 |
Correct |
10 ms |
3348 KB |
Output is correct |
6 |
Correct |
10 ms |
3360 KB |
Output is correct |
7 |
Correct |
9 ms |
3376 KB |
Output is correct |
8 |
Correct |
8 ms |
3412 KB |
Output is correct |
9 |
Correct |
11 ms |
3424 KB |
Output is correct |
10 |
Correct |
10 ms |
3436 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
65 ms |
5320 KB |
Output is correct |
2 |
Correct |
71 ms |
5384 KB |
Output is correct |
3 |
Correct |
211 ms |
8380 KB |
Output is correct |
4 |
Correct |
192 ms |
8796 KB |
Output is correct |
5 |
Correct |
389 ms |
13784 KB |
Output is correct |
6 |
Correct |
405 ms |
14648 KB |
Output is correct |
7 |
Correct |
446 ms |
15452 KB |
Output is correct |
8 |
Correct |
399 ms |
15944 KB |
Output is correct |
9 |
Correct |
437 ms |
17084 KB |
Output is correct |
10 |
Correct |
453 ms |
21888 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
74 ms |
21888 KB |
Output is correct |
2 |
Correct |
78 ms |
21888 KB |
Output is correct |
3 |
Correct |
182 ms |
21888 KB |
Output is correct |
4 |
Correct |
225 ms |
21888 KB |
Output is correct |
5 |
Correct |
402 ms |
22696 KB |
Output is correct |
6 |
Correct |
444 ms |
22696 KB |
Output is correct |
7 |
Correct |
416 ms |
24628 KB |
Output is correct |
8 |
Correct |
408 ms |
24628 KB |
Output is correct |
9 |
Correct |
438 ms |
24628 KB |
Output is correct |
10 |
Correct |
416 ms |
24628 KB |
Output is correct |