#include<bits/stdc++.h>
using namespace std;
struct union_find{
int par[100100],sz[100100];
void RS(){memset(par,0,sizeof par);for(auto&i:sz)i=1;}
union_find(){RS();}
int comp(int x){
return par[x]?par[x]=comp(par[x]):x;
}
void merge(int a,int b){
a=comp(a); b=comp(b);
if(a<b) swap(a,b);
if(a-b) par[a]=b,
sz[b]+=sz[a];
}
} UF;
int mod=1e9;
long long ANS;
vector<int>adj[100100];
set<int>adj2[100100];
map<pair<int,int>,int>S;
long long SZ[100100];
void dfs(int n,int p,int x){
for(auto i:adj2[n])
if(i-p)dfs(i,n,x),
SZ[n]+=SZ[i];
ANS+=SZ[n]*(x-SZ[n]);
}
int DistanceSum(int N, int *X, int *Y) {
int A=*min_element(Y,Y+N);
int B=*min_element(X,X+N);
for(int i=0;i<N;i++)
S[{X[i]-=B,Y[i]-=A}]=i+1;
for(int i=N;i;i--)
X[i]=X[i-1],Y[i]=Y[i-1];
for(int i=1;i<=N;i++){
if(S.count({X[i]-1,Y[i]}))
adj[i].push_back(S[{X[i]-1,Y[i]}]);
if(S.count({X[i]+1,Y[i]}))
adj[i].push_back(S[{X[i]+1,Y[i]}]);
if(S.count({X[i],Y[i]-1}))
adj[i].push_back(S[{X[i],Y[i]-1}]);
if(S.count({X[i],Y[i]+1}))
adj[i].push_back(S[{X[i],Y[i]+1}]);
}
for(int i=1;i<=N;i++)
for(auto x:adj[i])
if(X[x]==X[i])
UF.merge(x,i);
for(int i=1;i<=N;i++)
for(auto x:adj[i]) if(Y[x]==Y[i])
adj2[UF.comp(x)].insert(UF.comp(i));
for(int i=1;i<=N;i++)
SZ[i]=UF.sz[i];
dfs(1,0,N);
UF.RS();
for(int i=1;i<=N;i++)
adj2[i].clear(),swap(X[i],Y[i]);
for(int i=1;i<=N;i++)
for(auto x:adj[i])
if(X[x]==X[i])
UF.merge(x,i);
for(int i=1;i<=N;i++)
for(auto x:adj[i]) if(Y[x]==Y[i])
adj2[UF.comp(x)].insert(UF.comp(i));
for(int i=1;i<=N;i++)
SZ[i]=UF.sz[i];
dfs(1,0,N);
return ANS%mod;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
8796 KB |
Output is correct |
2 |
Correct |
2 ms |
8796 KB |
Output is correct |
3 |
Correct |
2 ms |
8796 KB |
Output is correct |
4 |
Correct |
3 ms |
8892 KB |
Output is correct |
5 |
Correct |
2 ms |
8900 KB |
Output is correct |
6 |
Correct |
4 ms |
8796 KB |
Output is correct |
7 |
Correct |
2 ms |
8792 KB |
Output is correct |
8 |
Correct |
2 ms |
8796 KB |
Output is correct |
9 |
Correct |
4 ms |
8284 KB |
Output is correct |
10 |
Correct |
3 ms |
8284 KB |
Output is correct |
11 |
Correct |
4 ms |
8280 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
9052 KB |
Output is correct |
2 |
Correct |
3 ms |
8796 KB |
Output is correct |
3 |
Correct |
3 ms |
9052 KB |
Output is correct |
4 |
Correct |
3 ms |
9052 KB |
Output is correct |
5 |
Correct |
4 ms |
9048 KB |
Output is correct |
6 |
Correct |
4 ms |
9168 KB |
Output is correct |
7 |
Correct |
4 ms |
9052 KB |
Output is correct |
8 |
Correct |
4 ms |
9048 KB |
Output is correct |
9 |
Correct |
5 ms |
8552 KB |
Output is correct |
10 |
Correct |
8 ms |
8368 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
32 ms |
10588 KB |
Output is correct |
2 |
Correct |
42 ms |
11140 KB |
Output is correct |
3 |
Correct |
93 ms |
14412 KB |
Output is correct |
4 |
Correct |
86 ms |
14628 KB |
Output is correct |
5 |
Correct |
206 ms |
20100 KB |
Output is correct |
6 |
Correct |
198 ms |
20088 KB |
Output is correct |
7 |
Correct |
193 ms |
20288 KB |
Output is correct |
8 |
Correct |
196 ms |
20052 KB |
Output is correct |
9 |
Correct |
196 ms |
20360 KB |
Output is correct |
10 |
Correct |
175 ms |
25012 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
32 ms |
11892 KB |
Output is correct |
2 |
Correct |
36 ms |
11820 KB |
Output is correct |
3 |
Correct |
92 ms |
16388 KB |
Output is correct |
4 |
Correct |
95 ms |
15700 KB |
Output is correct |
5 |
Correct |
216 ms |
23636 KB |
Output is correct |
6 |
Correct |
212 ms |
21552 KB |
Output is correct |
7 |
Correct |
207 ms |
23828 KB |
Output is correct |
8 |
Correct |
205 ms |
21588 KB |
Output is correct |
9 |
Correct |
201 ms |
21076 KB |
Output is correct |
10 |
Correct |
191 ms |
21072 KB |
Output is correct |