#include <bits/stdc++.h>
using namespace std;
#define f first
#define s second
//#define int long long
#define pii pair <int, int>
#define pb push_back
const int N = 3e5 + 5, inf = 1e9, mod = 1e9;
int n, x[N], y[N], mnx, mny, nx, ny;
long long ans,sz[N];
vector <int> vx[N], vy[N],v[N];
map <int, int> vert[N], vert1[N];
void dfs(int a, int p) {
for (int to : v[a]) {
if (to == p) continue;
dfs(to, a);
sz[a] += sz[to];
}
ans += sz[a] * (n - sz[a]) % mod; ans %= mod;
}
int DistanceSum(int N, int *X, int *Y) {
n = N;
int mnx = inf, mny = inf;
for (int i = 1; i <= n; i++) {
x[i] = X[i - 1]; y[i] = Y[i - 1];
mnx = min(mnx, x[i]); mny = min(mny, y[i]);
}
for (int i = 1; i <= n; i++) {
x[i] -= mnx; y[i] -= mny;
nx = max(nx, x[i]); ny = max(ny, y[i]);
}
for (int i = 1; i <= n; i++) {
vx[x[i]].pb(y[i]);
}
int cnt = 0;
for (int i = 0; i <= nx; i++) {
sort(vx[i].begin(), vx[i].end());
for (int j = 0; j < vx[i].size(); j++) {
int cy = vx[i][j];
if (j == 0 || cy != vx[i][j - 1] + 1) cnt++;
sz[cnt]++;
vert[i][cy] = cnt;
}
}
for (int i = 1; i <= nx; i++) {
for (int j = 0; j < vx[i].size(); j++) {
int cy = vx[i][j];
if (vert[i - 1][cy]) {
int a = vert[i - 1][cy]; int b = vert[i][cy];
if (v[a].size() && v[a].back() == b) continue;
v[a].pb(b); v[b].pb(a);
}
}
}
dfs(1, 0);
for (int i = 1; i <= cnt; i++) {
v[i].clear(); sz[i] = 0;
}
for (int i = 1; i <= n; i++) {
vy[y[i]].pb(x[i]);
}
cnt = 0;
for (int i = 0; i <= ny; i++) {
sort(vy[i].begin(), vy[i].end());
for (int j = 0; j < vy[i].size(); j++) {
int cx = vy[i][j];
if (j == 0 || cx != vy[i][j - 1] + 1) cnt++;
sz[cnt]++;
vert1[cx][i] = cnt;
}
}
for (int i = 1; i <= ny; i++) {
for (int j = 0; j < vy[i].size(); j++) {
int cx = vy[i][j];
if (vert1[cx][i - 1]) {
int a = vert1[cx][i - 1]; int b = vert1[cx][i];
if (v[a].size() && v[a].back() == b) continue;
v[a].pb(b); v[b].pb(a);
}
}
}
dfs(1, 0);
return ans;
}
Compilation message
city.cpp: In function 'int DistanceSum(int, int*, int*)':
city.cpp:38:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
38 | for (int j = 0; j < vx[i].size(); j++) {
| ~~^~~~~~~~~~~~~~
city.cpp:47:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
47 | for (int j = 0; j < vx[i].size(); j++) {
| ~~^~~~~~~~~~~~~~
city.cpp:66:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
66 | for (int j = 0; j < vy[i].size(); j++) {
| ~~^~~~~~~~~~~~~~
city.cpp:74:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
74 | for (int j = 0; j < vy[i].size(); j++) {
| ~~^~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
26 ms |
49620 KB |
Output is correct |
2 |
Correct |
26 ms |
49624 KB |
Output is correct |
3 |
Correct |
24 ms |
49628 KB |
Output is correct |
4 |
Correct |
24 ms |
49608 KB |
Output is correct |
5 |
Correct |
23 ms |
49628 KB |
Output is correct |
6 |
Correct |
23 ms |
49608 KB |
Output is correct |
7 |
Correct |
22 ms |
49620 KB |
Output is correct |
8 |
Correct |
23 ms |
49624 KB |
Output is correct |
9 |
Correct |
24 ms |
49616 KB |
Output is correct |
10 |
Correct |
24 ms |
49644 KB |
Output is correct |
11 |
Correct |
23 ms |
49668 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
26 ms |
49816 KB |
Output is correct |
2 |
Correct |
25 ms |
49748 KB |
Output is correct |
3 |
Correct |
24 ms |
49768 KB |
Output is correct |
4 |
Correct |
24 ms |
49860 KB |
Output is correct |
5 |
Correct |
25 ms |
49952 KB |
Output is correct |
6 |
Correct |
25 ms |
49888 KB |
Output is correct |
7 |
Correct |
26 ms |
50004 KB |
Output is correct |
8 |
Correct |
28 ms |
50040 KB |
Output is correct |
9 |
Correct |
28 ms |
49936 KB |
Output is correct |
10 |
Correct |
30 ms |
49844 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
36 ms |
52300 KB |
Output is correct |
2 |
Correct |
34 ms |
52300 KB |
Output is correct |
3 |
Correct |
56 ms |
56096 KB |
Output is correct |
4 |
Correct |
50 ms |
56204 KB |
Output is correct |
5 |
Correct |
96 ms |
62808 KB |
Output is correct |
6 |
Correct |
81 ms |
62828 KB |
Output is correct |
7 |
Correct |
85 ms |
63100 KB |
Output is correct |
8 |
Correct |
83 ms |
62692 KB |
Output is correct |
9 |
Correct |
79 ms |
62668 KB |
Output is correct |
10 |
Correct |
82 ms |
66524 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
40 ms |
53324 KB |
Output is correct |
2 |
Correct |
40 ms |
52948 KB |
Output is correct |
3 |
Correct |
70 ms |
58768 KB |
Output is correct |
4 |
Correct |
77 ms |
57880 KB |
Output is correct |
5 |
Correct |
136 ms |
67940 KB |
Output is correct |
6 |
Correct |
106 ms |
64792 KB |
Output is correct |
7 |
Correct |
125 ms |
68324 KB |
Output is correct |
8 |
Correct |
127 ms |
64976 KB |
Output is correct |
9 |
Correct |
114 ms |
64292 KB |
Output is correct |
10 |
Correct |
107 ms |
64076 KB |
Output is correct |