#include <bits/stdc++.h>
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define pb push_back
#define FIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)
using namespace std;
typedef long long ll;
typedef pair <ll, ll> pii;
const int maxn = 1e5 + 7, mod = 1e9;
int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};
int n;
int siz[maxn], bio[maxn];
vector <int> adj[maxn];
int add(int x, int y) {return (x+y < mod) ? x+y : x+y-mod;}
int mul(int x, int y) {return (ll)x*y%mod;}
struct UF {
vector <int> pa, si;
void init(int n) {
pa.clear(); pa.resize(n);
si.clear(); si.resize(n, 1);
for (int i = 0; i < n; i++) pa[i] = i;
}
int find(int x) {return (x == pa[x]) ? x : (pa[x] = find(pa[x]));}
void unite(int x, int y) {
x = find(x); y = find(y);
if (x == y) return;
if (si[x] < si[y]) swap(x, y);
si[x] += si[y];
pa[y] = x;
}
} u;
int dfs(int x) {
bio[x] = 1;
siz[x] = u.si[x];
int res = 0;
for (auto y : adj[x]) {
if (bio[y]) continue;
res = add(res, dfs(y));
siz[x] += siz[y];
}
return add(res, mul(siz[x], n-siz[x]));
}
int solve(vector <pii> v) {
map <pii, int> m;
u.init(n);
for (int i = 0; i < n; i++) {
m[v[i]] = i;
siz[i] = bio[i] = 0;
adj[i].clear();
}
for (int i = 0; i < n; i++) if (m.find({v[i].F+1, v[i].S}) != m.end()) u.unite(i, m[{v[i].F+1, v[i].S}]);
for (int i = 0; i < n; i++) {
for (int j = 0; j < 4; j++) {
int nx = v[i].F+dx[j], ny = v[i].S+dy[j];
if (m.find({nx, ny}) != m.end()) adj[u.find(i)].pb(u.find(m[{nx, ny}]));
}
}
return dfs(u.find(0));
}
int DistanceSum(int N, int *X, int *Y) {
n = N;
vector <pii> v;
for (int i = 0; i < n; i++) {
cin >> X[i] >> Y[i];
v.pb({X[i], Y[i]});
}
int res = solve(v);
for (int i = 0; i < n; i++) swap(v[i].F, v[i].S);
return add(res, solve(v));
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2652 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
2 ms |
2800 KB |
Output is correct |
4 |
Correct |
2 ms |
2652 KB |
Output is correct |
5 |
Correct |
2 ms |
2652 KB |
Output is correct |
6 |
Correct |
2 ms |
2652 KB |
Output is correct |
7 |
Correct |
2 ms |
2652 KB |
Output is correct |
8 |
Correct |
2 ms |
2652 KB |
Output is correct |
9 |
Correct |
2 ms |
2652 KB |
Output is correct |
10 |
Correct |
2 ms |
2904 KB |
Output is correct |
11 |
Correct |
3 ms |
2652 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
2908 KB |
Output is correct |
2 |
Correct |
4 ms |
2908 KB |
Output is correct |
3 |
Correct |
4 ms |
2908 KB |
Output is correct |
4 |
Correct |
5 ms |
2908 KB |
Output is correct |
5 |
Correct |
6 ms |
2908 KB |
Output is correct |
6 |
Correct |
7 ms |
3072 KB |
Output is correct |
7 |
Correct |
7 ms |
3320 KB |
Output is correct |
8 |
Correct |
5 ms |
2908 KB |
Output is correct |
9 |
Correct |
6 ms |
2908 KB |
Output is correct |
10 |
Correct |
6 ms |
3048 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
70 ms |
6292 KB |
Output is correct |
2 |
Correct |
62 ms |
6300 KB |
Output is correct |
3 |
Correct |
178 ms |
11152 KB |
Output is correct |
4 |
Correct |
193 ms |
11096 KB |
Output is correct |
5 |
Correct |
473 ms |
20648 KB |
Output is correct |
6 |
Correct |
430 ms |
19896 KB |
Output is correct |
7 |
Correct |
411 ms |
19972 KB |
Output is correct |
8 |
Correct |
459 ms |
20512 KB |
Output is correct |
9 |
Correct |
440 ms |
18868 KB |
Output is correct |
10 |
Correct |
389 ms |
20656 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
76 ms |
5996 KB |
Output is correct |
2 |
Correct |
69 ms |
6028 KB |
Output is correct |
3 |
Correct |
172 ms |
10680 KB |
Output is correct |
4 |
Correct |
182 ms |
10940 KB |
Output is correct |
5 |
Correct |
427 ms |
18732 KB |
Output is correct |
6 |
Correct |
467 ms |
19604 KB |
Output is correct |
7 |
Correct |
419 ms |
18948 KB |
Output is correct |
8 |
Correct |
454 ms |
19460 KB |
Output is correct |
9 |
Correct |
471 ms |
19388 KB |
Output is correct |
10 |
Correct |
454 ms |
19576 KB |
Output is correct |