#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};
int x[100007], y[100007];
ll ans, dist[2007][2007];
vector <vector <int>> graph(100007);
const int mod = 1e9;
map <pii, int> id;
void bfs(int p){
for(int i = 0; i < n; i++){
dist[p][i] = 9e18;
}
dist[p][p] = 0;
queue <pii> K;
K.push(mp(0, p));
while(sz(K)){
auto u = K.front(); K.pop();
for(auto to : graph[u.second]){
if(dist[p][to] > dist[p][u.second]){
dist[p][to] = dist[p][u.second] + 1;
K.push(mp(-dist[p][to], to));
}
}
}
}
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) {
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;
}
for(i = 0; i < n; i++){
for(j = 0; j < 4; j++){
int nx = moveX[j]+x[i];
int ny = moveY[j]+y[i];
if(id.count(mp(nx, ny))){
graph[i].pb(id[mp(nx, ny)]);
}
}
}
if(n <= 2000){
for(i = 0; i < n; i++){
bfs(i);
}
for(i = 0; i < n; i++){
for(j = i+1; j < n; j++){
ans = add(ans, dist[i][j]);
}
}
}
else{
int mx1 = 0, mn1 = INT_MAX, mx2 = 0, mn2 = INT_MAX;
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 |
2 ms |
2688 KB |
Output is correct |
2 |
Correct |
3 ms |
2816 KB |
Output is correct |
3 |
Correct |
4 ms |
2816 KB |
Output is correct |
4 |
Correct |
4 ms |
3200 KB |
Output is correct |
5 |
Correct |
65 ms |
3600 KB |
Output is correct |
6 |
Correct |
20 ms |
3840 KB |
Output is correct |
7 |
Execution timed out |
1108 ms |
227560 KB |
Time limit exceeded |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1092 ms |
81152 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
45 ms |
4984 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
43 ms |
4860 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |