#include <bits/stdc++.h>
using namespace std;
int N, C;
vector<int> graph[100005];
int sz[100005];
set<pair<int, int>> st;
int par[100005];
vector<pair<int, int>> coords;
int who[100005];
int getrt(int n){
return par[n] = (par[n] == n ? n : getrt(par[n]));
}
int getidx(pair<int, int> p){
return lower_bound(coords.begin(), coords.end(), p) - coords.begin();
}
void dfs(int n, int p){
for(int e : graph[n]){
if(e != p){
dfs(e, n);
sz[n] += sz[e];
}
}
}
void build(){
for(int i = 0; i<N; i++){
auto p = coords[i];
par[i] = i;
if(st.count({p.first-1, p.second})){
par[getrt(getidx({p.first-1, p.second}))] = i;
}
if(st.count({p.first+1, p.second})){
par[getrt(getidx({p.first-1, p.second}))] = i;
}
st.insert(p);
}
C = 0;
for(int i = 0; i<N; i++){
if(par[i] == i){
who[i] = ++C;
sz[C] = 0;
}
}
for(int i = 0; i<N; i++){
sz[who[getrt(i)]]++;
}
for(int i = 0; i<N; i++){
auto p = coords[i];
int n = who[getrt(i)];
if(st.count({p.first, p.second-1})){
auto a = who[getrt(getidx({p.first, p.second-1}))];
graph[a].push_back(n);
}
if(st.count({p.first, p.second+1})){
auto a = who[getrt(getidx({p.first, p.second+1}))];
graph[a].push_back(n);
}
}
for(int i = 1; i<=C; i++){
sort(graph[i].begin(), graph[i].end());
graph[i].erase(unique(graph[i].begin(), graph[i].end()), graph[i].end());
}
}
long long calc(int *X, int *Y){
for(int i = 0; i<N; i++){
coords.push_back({X[i], Y[i]});
}
sort(coords.begin(), coords.end());
build();
dfs(1, 0);
long long ans = 0;
for(int i = 1; i<=C; i++){
ans += 1LL*sz[i]*(N-sz[i]);
ans %= 1000000000;
graph[i].clear();
}
coords.clear();
st.clear();
fill(who, who+N, -1);
fill(par, par+N, -1);
return ans;
}
int DistanceSum(int NN, int *X, int *Y){
N = NN;
long long ret = calc(X, Y);
ret += calc(Y, X);
return ret%1000000000;
}
/*
int main(){
cin.sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int NNN;
cin >> NNN;
int X[NNN], Y[NNN];
for(int i = 0; i<NNN; i++){
cin >> X[i] >> Y[i];
}
cout << DistanceSum(NNN, X, Y) << endl;
}
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
2688 KB |
Output is correct |
2 |
Correct |
6 ms |
2688 KB |
Output is correct |
3 |
Correct |
6 ms |
2688 KB |
Output is correct |
4 |
Correct |
6 ms |
2688 KB |
Output is correct |
5 |
Correct |
6 ms |
2688 KB |
Output is correct |
6 |
Correct |
6 ms |
2688 KB |
Output is correct |
7 |
Correct |
6 ms |
2688 KB |
Output is correct |
8 |
Correct |
6 ms |
2688 KB |
Output is correct |
9 |
Correct |
7 ms |
2816 KB |
Output is correct |
10 |
Correct |
6 ms |
2688 KB |
Output is correct |
11 |
Correct |
6 ms |
2688 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
2816 KB |
Output is correct |
2 |
Correct |
10 ms |
2816 KB |
Output is correct |
3 |
Correct |
8 ms |
2816 KB |
Output is correct |
4 |
Correct |
8 ms |
2816 KB |
Output is correct |
5 |
Correct |
9 ms |
2816 KB |
Output is correct |
6 |
Correct |
9 ms |
2816 KB |
Output is correct |
7 |
Correct |
9 ms |
2816 KB |
Output is correct |
8 |
Correct |
10 ms |
2816 KB |
Output is correct |
9 |
Correct |
9 ms |
2816 KB |
Output is correct |
10 |
Correct |
9 ms |
2944 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
44 ms |
4732 KB |
Output is correct |
2 |
Correct |
42 ms |
4720 KB |
Output is correct |
3 |
Correct |
108 ms |
7028 KB |
Output is correct |
4 |
Correct |
106 ms |
7412 KB |
Output is correct |
5 |
Correct |
224 ms |
11412 KB |
Output is correct |
6 |
Correct |
230 ms |
12272 KB |
Output is correct |
7 |
Correct |
227 ms |
11888 KB |
Output is correct |
8 |
Correct |
221 ms |
11120 KB |
Output is correct |
9 |
Correct |
223 ms |
11888 KB |
Output is correct |
10 |
Correct |
216 ms |
13936 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
44 ms |
4596 KB |
Output is correct |
2 |
Correct |
43 ms |
4596 KB |
Output is correct |
3 |
Correct |
112 ms |
7284 KB |
Output is correct |
4 |
Correct |
111 ms |
7284 KB |
Output is correct |
5 |
Correct |
225 ms |
11632 KB |
Output is correct |
6 |
Correct |
234 ms |
11760 KB |
Output is correct |
7 |
Correct |
229 ms |
11804 KB |
Output is correct |
8 |
Correct |
231 ms |
11632 KB |
Output is correct |
9 |
Correct |
227 ms |
11376 KB |
Output is correct |
10 |
Correct |
233 ms |
11632 KB |
Output is correct |