//#include "grader.cpp"
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
const int N=2005, mod= 1e9;
int n, dist[N][N];
vector<vector<int>> edges(2005);
void Dijkstra(int cur){
queue<int>q;
q.push(cur);
while(!q.empty()){
int u=q.front(); q.pop();
for(auto x:edges[u]){
if(dist[cur][u]+1 < dist[cur][x]){
dist[cur][x] = dist[cur][u]+1;
q.push(x);
}
}
}
}
const int NAX = 1e5+5;
int sufx[NAX], sufy[NAX];
int DistanceSum(int N, int *x, int *y) {
n=N;
if(n<=2000){
for(int i=0;i<n;i++){
for(int j=i+1;j<n;j++){
dist[i][j]=2e9;
dist[j][i]=2e9;
if(abs(x[i]-x[j]) + abs(y[i]-y[j]) == 1){
edges[i].pb(j);
edges[j].pb(i);
}
}
}
for(int i=0;i<n;i++)
Dijkstra(i);
ll ans=0;
for(int i=0; i<n; i++){
for(int j=i+1; j<n; j++){
ans += dist[i][j];
ans %= mod;
}
}
return ans;
}
else{
sort(x,x+n);
sort(y,y+n);
for(int i=n-2; i>=0; i--){
sufx[i] += sufx[i+1] + x[i+1];
sufy[i] += sufy[i+1] + y[i+1];
}
ll ans=0;
for(int i=0;i<n;i++){
int cur=x[i]*(n-i-1);
ans+=(sufx[i]-cur);
ans%=mod;
}
for(int i=0;i<n;i++){
int cur=y[i]*(n-i-1);
ans+=(sufy[i]-cur);
ans%=mod;
}
return ans;
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
512 KB |
Output is correct |
3 |
Correct |
1 ms |
512 KB |
Output is correct |
4 |
Correct |
1 ms |
768 KB |
Output is correct |
5 |
Correct |
2 ms |
768 KB |
Output is correct |
6 |
Correct |
2 ms |
1280 KB |
Output is correct |
7 |
Correct |
2 ms |
1408 KB |
Output is correct |
8 |
Correct |
2 ms |
1280 KB |
Output is correct |
9 |
Correct |
2 ms |
1408 KB |
Output is correct |
10 |
Correct |
2 ms |
1408 KB |
Output is correct |
11 |
Correct |
3 ms |
1408 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
37 ms |
8320 KB |
Output is correct |
2 |
Correct |
40 ms |
8320 KB |
Output is correct |
3 |
Correct |
86 ms |
12160 KB |
Output is correct |
4 |
Correct |
95 ms |
12160 KB |
Output is correct |
5 |
Correct |
154 ms |
16252 KB |
Output is correct |
6 |
Correct |
169 ms |
16128 KB |
Output is correct |
7 |
Correct |
158 ms |
16120 KB |
Output is correct |
8 |
Correct |
168 ms |
16248 KB |
Output is correct |
9 |
Correct |
165 ms |
16128 KB |
Output is correct |
10 |
Correct |
161 ms |
16120 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
768 KB |
Output is correct |
2 |
Correct |
7 ms |
928 KB |
Output is correct |
3 |
Correct |
17 ms |
1664 KB |
Output is correct |
4 |
Correct |
16 ms |
1536 KB |
Output is correct |
5 |
Correct |
35 ms |
2808 KB |
Output is correct |
6 |
Correct |
32 ms |
2808 KB |
Output is correct |
7 |
Correct |
34 ms |
2936 KB |
Output is correct |
8 |
Correct |
33 ms |
2684 KB |
Output is correct |
9 |
Correct |
33 ms |
2776 KB |
Output is correct |
10 |
Correct |
32 ms |
2816 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
7 ms |
768 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |