#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair <int, int> pii;
#define precision(n) fixed << setprecision(n)
#define pb push_back
#define ub upper_bound
#define lb lower_bound
#define mp make_pair
#define eps (double)1e-9
#define PI 2*acos(0.0)
#define endl "\n"
#define sz(v) int((v).size())
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define do_not_disturb ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
int n, moveX[] = {-1, 0, 0, 1}, moveY[] = {0, -1, 1, 0};
ll x[100007], y[100007], ans, dist[2007][2007];
const ll mod = 1e9;
map <pii, int> id;
void dijk(int xx, int yy){
int p = id[mp(xx, yy)];
for(int i = 0; i < n; i++){
dist[p][i] = 9e18;
}
dist[p][p] = 0;
queue <pair <int, pii>> K;
K.push(mp(0, mp(xx, yy)));
while(sz(K)){
auto u = K.front(); K.pop();
int pp = id[u.second];
for(int i = 0; i < 4; i++){
int nx = moveX[i]+u.second.first;
int ny = moveY[i]+u.second.second;
if(!id.count(mp(nx, ny))) continue;
int ppp = id[mp(nx, ny)];
if(dist[p][ppp] > dist[p][pp]+1){
dist[p][ppp] = dist[p][pp]+1;
K.push(mp(-dist[p][ppp], mp(nx, ny)));
}
}
}
}
ll add(ll a, ll b){
a = ((a%mod)+mod)%mod;
b = ((b%mod)+mod)%mod;
return (a+b)%mod;
}
ll mult(ll a, ll b){
a = ((a%mod)+mod)%mod;
b = ((b%mod)+mod)%mod;
return (a*b)%mod;
}
ll calc(ll a){
return a*(a+1)/2;
}
int DistanceSum(int N, int *X, int *Y) {
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
int i, j;
n = N;
for(i = 0; i < n; i++){
x[i] = X[i];
y[i] = Y[i];
id[mp(x[i], y[i])] = i;
}
if(n <= 2000){
for(i = 0; i < n; i++){
dijk(x[i], y[i]);
}
for(i = 0; i < n; i++){
for(j = i+1; j < n; j++){
ans = add(ans, dist[i][j]);
}
}
}
else{
ll mx1 = 0, mn1 = 9e18, mx2 = 0, mn2 = 9e18;
for(i = 0; i < n; i++){
mx1 = max(mx1, x[i]);
mx2 = max(mx2, y[i]);
mn1 = min(mn1, x[i]);
mn2 = min(mn2, y[i]);
}
int nn = mx1-mn1+1, mm = mx2-mn2+1;
for(i = 1; i <= nn; i++){
for(j = 1; j <= mm; j++){
ans = add(ans, mult(mm, calc(nn-i)));
ans = add(ans, calc(mm-j));
ans = add(ans, mult(nn-i, add(calc(mm-j), calc(j-1))));
}
}
}
return ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
512 KB |
Output is correct |
4 |
Correct |
4 ms |
896 KB |
Output is correct |
5 |
Correct |
4 ms |
768 KB |
Output is correct |
6 |
Correct |
15 ms |
1536 KB |
Output is correct |
7 |
Correct |
16 ms |
1536 KB |
Output is correct |
8 |
Correct |
15 ms |
1536 KB |
Output is correct |
9 |
Correct |
16 ms |
1448 KB |
Output is correct |
10 |
Correct |
16 ms |
1536 KB |
Output is correct |
11 |
Correct |
17 ms |
1536 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
587 ms |
12476 KB |
Output is correct |
2 |
Correct |
574 ms |
12280 KB |
Output is correct |
3 |
Execution timed out |
1081 ms |
18168 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
15 ms |
2176 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
18 ms |
2192 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |