#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};
int dis[2010][2010], mp[2010][2010];
int bfs(int i, int X, int Y){
queue<pair<int,int>> Q; Q.push({X,Y});
vector<pair<int,int>> v; dis[X][Y]=0; int tot = 0;
while(!empty(Q)){
auto [x1,y1] = Q.front(); Q.pop();
tot+=dis[x1][y1]*(mp[x1][y1]>=i); tot%=MOD;
v.push_back({x1,y1});
for(int k = 0; k < 4; k++){
int x2 = x1+x[k], y2 = y1+y[k];
if(x2<0 or y2<0 or !mp[x2][y2] or dis[x2][y2]!=-1) continue;
dis[x2][y2] = dis[x1][y1]+1, Q.push({x2,y2});
}
}
for(auto [x1,y1] : v) dis[x1][y1]=-1;
return tot;
}
int DistanceSum(int N, int *X, int *Y) {
if(N<=2000){
vector<int> v; memset(dis,-1,sizeof(dis));
for(int j : {0,1}){
v.clear();
for(int i = 0; i < N; i++) v.push_back(X[i]);
sort(begin(v),end(v)); v.erase(unique(begin(v),end(v)),end(v));
for(int i = 0; i < N; i++)
X[i] = (int)(lower_bound(begin(v),end(v),X[i])-begin(v)),
swap(X[i],Y[i]);
}
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:29:17: warning: unused variable 'j' [-Wunused-variable]
29 | for(int j : {0,1}){
| ^
city.cpp:45:13: warning: unused variable 'j' [-Wunused-variable]
45 | for(int j : {0,1}){
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
16084 KB |
Output is correct |
2 |
Correct |
7 ms |
16084 KB |
Output is correct |
3 |
Correct |
6 ms |
16084 KB |
Output is correct |
4 |
Correct |
7 ms |
16148 KB |
Output is correct |
5 |
Correct |
6 ms |
16124 KB |
Output is correct |
6 |
Correct |
9 ms |
16160 KB |
Output is correct |
7 |
Correct |
9 ms |
16216 KB |
Output is correct |
8 |
Correct |
8 ms |
16212 KB |
Output is correct |
9 |
Correct |
7 ms |
16084 KB |
Output is correct |
10 |
Correct |
8 ms |
16212 KB |
Output is correct |
11 |
Correct |
8 ms |
16212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
34 ms |
16340 KB |
Output is correct |
2 |
Correct |
31 ms |
16344 KB |
Output is correct |
3 |
Correct |
77 ms |
16508 KB |
Output is correct |
4 |
Correct |
67 ms |
16480 KB |
Output is correct |
5 |
Correct |
148 ms |
16576 KB |
Output is correct |
6 |
Correct |
102 ms |
16448 KB |
Output is correct |
7 |
Correct |
145 ms |
16736 KB |
Output is correct |
8 |
Correct |
102 ms |
16724 KB |
Output is correct |
9 |
Correct |
99 ms |
16480 KB |
Output is correct |
10 |
Correct |
99 ms |
16468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
480 KB |
Output is correct |
2 |
Correct |
5 ms |
540 KB |
Output is correct |
3 |
Correct |
12 ms |
724 KB |
Output is correct |
4 |
Correct |
12 ms |
680 KB |
Output is correct |
5 |
Correct |
24 ms |
1164 KB |
Output is correct |
6 |
Correct |
24 ms |
1136 KB |
Output is correct |
7 |
Correct |
25 ms |
1148 KB |
Output is correct |
8 |
Correct |
26 ms |
1148 KB |
Output is correct |
9 |
Correct |
24 ms |
1108 KB |
Output is correct |
10 |
Correct |
24 ms |
1108 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
5 ms |
520 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |