This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
using namespace std;
#define pii pair<int,int>
#define F first
#define S second
#define pb push_back
const int MAXN = 100005;
const int MOD = 1000000000;
long long ans = 0;
int n;
pii a[MAXN];
set<int> adj[MAXN];
int sz[MAXN],dist[MAXN];
int comp;
map<pair<int,int>,int> C;
void dfs(int u,int p,int d){
dist[u] = d;
for(auto v:adj[u]){
if(v != p) dfs(v,u,d+1);
}
}
void solve(){
C.clear();
for(int i = 0; i < MAXN; i ++){
adj[i].clear();
sz[i] = 0;
}
comp = 0;
sort(a,a+n);
C[a[0]] = 0;
int last = 0;
for(int i = 1; i < n; i++){
if(a[i].F == a[i-1].F and a[i].S == a[i-1].S+1){
C[a[i]] = comp;
}
else{
sz[comp] = i-last;
comp++;
last = i;
C[a[i]] = comp;
}
}
sz[comp] = n-last;
for(int i = 0; i < n; i++){
if(C.find({a[i].F-1,a[i].S}) != C.end()){
adj[C[{a[i].F-1,a[i].S}]].insert(C[{a[i].F,a[i].S}]);
adj[C[{a[i].F,a[i].S}]].insert(C[{a[i].F-1,a[i].S}]);
}
}
for(int i = 0; i <= comp; i++){
dfs(i,i,0);
for(int j = 0; j <= comp; j++){
ans += (dist[j]*sz[j]*sz[i]);
}
}
}
int DistanceSum(int N, int *X, int *Y) {
n = N;
for(int i = 0; i < n; i ++){
a[i] = {X[i],Y[i]};
}
solve();
for(int i = 0; i < n; i ++){
a[i] = {Y[i],X[i]};
}
solve();
ans /= 2;
return ans%MOD;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |