#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll MOD = (ll)1e9;
int x[] = {1,-1,0,0};
int y[] = {0,0,1,-1};
unordered_map<int,unordered_map<int,int>> dis,mp;
int bfs(int i, int X, int Y){
queue<pair<int,int>> Q; Q.push({X,Y});
dis[X][Y]=0; dis.clear(); int tot = 0;
while(!empty(Q)){
auto [x1,y1] = Q.front(); Q.pop();
tot+=dis[x1][y1]*(mp[x1][y1]>=i); tot%=MOD;
for(int k = 0; k < 4; k++){
int x2 = x1+x[k], y2 = y1+y[k];
if(mp[x2].find(y2)==mp[x2].end()) continue;
if(dis[x2].find(y2)==dis[x2].end()){
dis[x2][y2] = dis[x1][y1]+1;
Q.push({x2,y2});
}
}
}
return tot;
}
int DistanceSum(int N, int *X, int *Y) {
if(N<=2000){
ll ans = 0;
for(int i = 0; i < N; i++)
mp[X[i]][Y[i]]=i+1;
for(int i = 0; i < N; i++)
ans+=bfs(i+1, X[i],Y[i]),ans%=MOD;
return ans;
}
ll ans = 0, tot = 0;
sort(X,X+N), sort(Y,Y+N);
for(int j : {0,1}){
tot = 0; sort(X,X+N);
for(ll i = 0; i < N; i++){
ans+=(1ll*X[i]*i-tot+MOD)%MOD;
tot+=X[i], tot%=MOD; ans%=MOD;swap(X[i],Y[i]);
}
}
return (int)ans;
}
Compilation message
city.cpp: In function 'int DistanceSum(int, int*, int*)':
city.cpp:37:13: warning: unused variable 'j' [-Wunused-variable]
37 | for(int j : {0,1}){
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
3 ms |
212 KB |
Output is correct |
5 |
Correct |
3 ms |
212 KB |
Output is correct |
6 |
Correct |
9 ms |
304 KB |
Output is correct |
7 |
Correct |
9 ms |
256 KB |
Output is correct |
8 |
Correct |
9 ms |
332 KB |
Output is correct |
9 |
Correct |
9 ms |
212 KB |
Output is correct |
10 |
Correct |
10 ms |
212 KB |
Output is correct |
11 |
Correct |
9 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
245 ms |
400 KB |
Output is correct |
2 |
Correct |
230 ms |
340 KB |
Output is correct |
3 |
Correct |
550 ms |
468 KB |
Output is correct |
4 |
Correct |
527 ms |
468 KB |
Output is correct |
5 |
Correct |
968 ms |
540 KB |
Output is correct |
6 |
Correct |
967 ms |
540 KB |
Output is correct |
7 |
Execution timed out |
1004 ms |
548 KB |
Time limit exceeded |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
596 KB |
Output is correct |
2 |
Correct |
5 ms |
560 KB |
Output is correct |
3 |
Correct |
12 ms |
1072 KB |
Output is correct |
4 |
Correct |
11 ms |
1104 KB |
Output is correct |
5 |
Correct |
22 ms |
1912 KB |
Output is correct |
6 |
Correct |
22 ms |
1748 KB |
Output is correct |
7 |
Correct |
25 ms |
2012 KB |
Output is correct |
8 |
Correct |
22 ms |
1748 KB |
Output is correct |
9 |
Correct |
23 ms |
1748 KB |
Output is correct |
10 |
Correct |
23 ms |
1896 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
6 ms |
592 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |