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;
typedef long long ll;
typedef vector<ll> vii;
typedef pair<ll,ll> pii;
#define F first
#define S second
#define pb push_back
#define all(v) v.begin(),v.end()
const ll dx[4]={1,-1,0,0};
const ll dy[4]={0,0,1,-1};
const ll M=1e5+6;
const ll mod=1e9;
ll n;
map<pii,ll>mp;
bool vis[M];
vii v[M];
ll bfs(ll x){
for(int i=0;i<n;i++)
vis[i]=0;
queue<pii>q;
q.push({0,x});
ll res=0;
while(!q.empty()){
pii p=q.front();
q.pop();
if(vis[p.S])
continue;
res+=p.F;
vis[p.S]=1;
for(auto it:v[p.S]){
if(!vis[it])
q.push({p.F+1,it});
}
}
return res;
}
int DistanceSum(int N, int *X, int *Y) {
n=N;
for(int i=0;i<n;i++){
mp[{X[i],Y[i]}]=i+1;
}
for(auto &it:mp){
for(ll i=0;i<4;i++){
if(mp.find({it.F.F+dx[i],it.F.S+dy[i]})!=mp.end())
v[it.S].pb(mp[{it.F.F+dx[i],it.F.S+dy[i]}]);
}
}
ll ans=0;
for(int i=1;i<=n;i++){
ans+=bfs(i);
ans%=mod;
}
return ans;
}
# | 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... |