#include <map>
#include <utility>
#include <queue>
#include <algorithm>
using namespace std;
vector < int > Next[100005];
int con[100005];
map < pair < int , int > , int > where;
int MOD=1000000000;
queue < pair < int , int > > BFS;
int DistanceSum(int N, int *X, int *Y)
{
int ans=0,x=0,y=0,i,j;
if(N<=2000)
{
for(i=0;i<N;i++) where[make_pair(X[i],Y[i])]=i;
for(i=0;i<N;i++)
{
if(where.find(make_pair(X[i]+1,Y[i]))!=where.end()) Next[i].push_back(where[make_pair(X[i]+1,Y[i])]);
if(where.find(make_pair(X[i]-1,Y[i]))!=where.end()) Next[i].push_back(where[make_pair(X[i]-1,Y[i])]);
if(where.find(make_pair(X[i],Y[i]+1))!=where.end()) Next[i].push_back(where[make_pair(X[i],Y[i]+1)]);
if(where.find(make_pair(X[i],Y[i]-1))!=where.end()) Next[i].push_back(where[make_pair(X[i],Y[i]-1)]);
}
for(i=0;i<N;i++)
{
for(j=0;j<N;j++) con[j]=-1;
BFS.push(make_pair(i,0));
while(!BFS.empty())
{
x=BFS.front().first;
y=BFS.front().second;
BFS.pop();
if(con[x]!=-1) continue;
con[x]=y;
if(i<x)
{
ans+=y;
if(ans>MOD) ans-=MOD;
}
for(auto k:Next[x]) BFS.push(make_pair(k,y+1));
}
}
}
else
{
sort(X,X+N);
sort(Y,Y+N);
for(i=0;i<N;i++)
{
ans+=(int) ((long long) X[i]*(long long) i%MOD);
ans%=MOD;
ans-=x;
ans+=MOD;
ans%=MOD;
ans+=(int) ((long long) Y[i]*(long long) i%MOD);
ans%=MOD;
ans-=y;
ans+=MOD;
ans%=MOD;
x=(int)(((long long) x+(long long) X[i])%MOD);
y=(int)(((long long) y+(long long) Y[i])%MOD);
}
}
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
2668 KB |
Output is correct |
2 |
Correct |
2 ms |
2668 KB |
Output is correct |
3 |
Correct |
2 ms |
2668 KB |
Output is correct |
4 |
Correct |
2 ms |
2668 KB |
Output is correct |
5 |
Correct |
2 ms |
2668 KB |
Output is correct |
6 |
Correct |
4 ms |
2668 KB |
Output is correct |
7 |
Correct |
4 ms |
2668 KB |
Output is correct |
8 |
Correct |
4 ms |
2668 KB |
Output is correct |
9 |
Correct |
4 ms |
2668 KB |
Output is correct |
10 |
Correct |
5 ms |
2812 KB |
Output is correct |
11 |
Correct |
4 ms |
2668 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
50 ms |
2832 KB |
Output is correct |
2 |
Correct |
65 ms |
2796 KB |
Output is correct |
3 |
Correct |
117 ms |
2880 KB |
Output is correct |
4 |
Correct |
133 ms |
2816 KB |
Output is correct |
5 |
Correct |
197 ms |
2924 KB |
Output is correct |
6 |
Correct |
253 ms |
3052 KB |
Output is correct |
7 |
Correct |
190 ms |
2924 KB |
Output is correct |
8 |
Correct |
249 ms |
2924 KB |
Output is correct |
9 |
Correct |
274 ms |
2924 KB |
Output is correct |
10 |
Correct |
245 ms |
2924 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
2924 KB |
Output is correct |
2 |
Correct |
8 ms |
3052 KB |
Output is correct |
3 |
Correct |
18 ms |
3564 KB |
Output is correct |
4 |
Correct |
18 ms |
3436 KB |
Output is correct |
5 |
Correct |
36 ms |
4332 KB |
Output is correct |
6 |
Correct |
36 ms |
4204 KB |
Output is correct |
7 |
Correct |
37 ms |
4332 KB |
Output is correct |
8 |
Correct |
36 ms |
4204 KB |
Output is correct |
9 |
Correct |
35 ms |
4296 KB |
Output is correct |
10 |
Correct |
35 ms |
4332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
9 ms |
2924 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |