#include <bits/stdc++.h>
using namespace std;
typedef int ll;
const ll INF = 1e9;
ll MOD = 998244353;
typedef pair<ll,ll> ii;
#define iii pair<ii,int>
#define f(i,a,b) for(ll i = a;i < b;i++)
#define pb push_back
#define vll vector<ll>
#define F first
#define S second
#define all(x) (x).begin(), (x).end()
///I hope I will get uprating and don't make mistakes
///I will never stop programming
///sqrt(-1) Love C++
///Please don't hack me
///@TheofanisOrfanou Theo830
///Think different approaches (bs,dp,greedy,graphs,shortest paths,mst)
///Stay Calm
///Look for special cases
///Beware of overflow and array bounds
///Think the problem backwards
///Training
vector<set<ll> >adj;
ll par[100005];
ll siz[100005];
ll s[100005];
int ans = 0;
int n;
ll find_par(ll a){
if(par[a] == a){
return a;
}
return par[a] = find_par(par[a]);
}
void enose(ll a,ll b){
ll p1 = find_par(a);
ll p2 = find_par(b);
par[p1] = p2;
siz[p2] += siz[p1];
siz[p1] = 0;
}
void dfs(ll idx,ll par){
idx = find_par(idx);
s[idx] = siz[idx];
for(auto x:adj[idx]){
if(x != par){
dfs(x,idx);
s[idx] += s[x];
ans += (1LL * (n - s[x]) * s[x]) % INF;
ans %= INF;
}
}
}
int DistanceSum(int N, int *X, int *Y){
n = N;
vector<iii>arr;
f(i,0,n){
par[i] = i;
siz[i] = 1;
arr.pb(iii(ii(X[i],Y[i]),i));
}
sort(all(arr));
f(i,1,n){
if(arr[i].F.F == arr[i-1].F.F && arr[i-1].F.S + 1 == arr[i].F.S){
enose(arr[i].S,arr[i-1].S);
}
}
f(i,0,n){
swap(arr[i].F.F,arr[i].F.S);
}
sort(all(arr));
adj.assign(n+5,set<ll>());
f(i,1,n){
if(arr[i].F.F == arr[i-1].F.F && arr[i-1].F.S + 1 == arr[i].F.S){
adj[find_par(arr[i].S)].insert(find_par(arr[i-1].S));
adj[find_par(arr[i-1].S)].insert(find_par(arr[i].S));
}
}
dfs(0,-1);
f(i,0,n){
par[i] = i;
siz[i] = 1;
s[i] = 0;
}
f(i,1,n){
if(arr[i].F.F == arr[i-1].F.F && arr[i-1].F.S + 1 == arr[i].F.S){
enose(arr[i].S,arr[i-1].S);
}
}
f(i,0,n){
swap(arr[i].F.F,arr[i].F.S);
}
sort(all(arr));
adj.assign(n+5,set<ll>());
f(i,1,n){
if(arr[i].F.F == arr[i-1].F.F && arr[i-1].F.S + 1 == arr[i].F.S){
adj[find_par(arr[i].S)].insert(find_par(arr[i-1].S));
adj[find_par(arr[i-1].S)].insert(find_par(arr[i].S));
}
}
dfs(0,-1);
return ans;
}
/*
#define inbuf_len 1 << 16
#define outbuf_len 1 << 16
int DistanceSum(int N, int *X, int *Y);
int main() {
int tmp;
char *inbuf, *outbuf;
inbuf = (char*) malloc(inbuf_len * sizeof(char));
outbuf = (char*) malloc(outbuf_len * sizeof(char));
tmp = setvbuf(stdin, inbuf, _IOFBF, inbuf_len);
assert(tmp == 0);
tmp = setvbuf(stdout, outbuf, _IOFBF, outbuf_len);
assert(tmp == 0);
int N, i;
tmp = scanf("%d", &N);
assert(tmp == 1);
int *sq_x, *sq_y;
sq_x = (int*) malloc(N * sizeof(int));
sq_y = (int*) malloc(N * sizeof(int));
for (i = 0; i < N; i++) {
tmp = scanf("%d %d", &sq_x[i], &sq_y[i]);
assert(tmp == 2);
}
int ds = DistanceSum(N, sq_x, sq_y);
printf("%d\n", ds);
return 0;
}
*/
/*
11
2 5
2 6
3 3
3 6
4 3
4 4
4 5
4 6
5 3
5 4
5 6
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
2 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
2 ms |
468 KB |
Output is correct |
6 |
Correct |
2 ms |
468 KB |
Output is correct |
7 |
Correct |
2 ms |
468 KB |
Output is correct |
8 |
Correct |
2 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
2 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
2128 KB |
Output is correct |
2 |
Correct |
11 ms |
2000 KB |
Output is correct |
3 |
Correct |
28 ms |
4448 KB |
Output is correct |
4 |
Correct |
27 ms |
4440 KB |
Output is correct |
5 |
Correct |
58 ms |
8260 KB |
Output is correct |
6 |
Correct |
65 ms |
9184 KB |
Output is correct |
7 |
Correct |
58 ms |
9372 KB |
Output is correct |
8 |
Correct |
61 ms |
9048 KB |
Output is correct |
9 |
Correct |
57 ms |
9412 KB |
Output is correct |
10 |
Correct |
59 ms |
13856 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
2732 KB |
Output is correct |
2 |
Correct |
14 ms |
2384 KB |
Output is correct |
3 |
Correct |
39 ms |
6216 KB |
Output is correct |
4 |
Correct |
42 ms |
5472 KB |
Output is correct |
5 |
Correct |
84 ms |
11972 KB |
Output is correct |
6 |
Correct |
69 ms |
10608 KB |
Output is correct |
7 |
Correct |
85 ms |
12992 KB |
Output is correct |
8 |
Correct |
68 ms |
10688 KB |
Output is correct |
9 |
Correct |
65 ms |
10112 KB |
Output is correct |
10 |
Correct |
69 ms |
10008 KB |
Output is correct |