#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
map<pair<int,int>,int>k;
vector<int> v[100010];
const int mod=1e9;
int DistanceSum(int N, int *X, int *Y) {
for(int i=0;i<N;i++){
if(k.count({X[i]-1,Y[i]})){
v[i].push_back(k[{X[i]-1,Y[i]}]);
v[k[{X[i]-1,Y[i]}]].push_back(i);
}
if(k.count({X[i]+1,Y[i]})){
v[i].push_back(k[{X[i]+1,Y[i]}]);
v[k[{X[i]+1,Y[i]}]].push_back(i);
}
if(k.count({X[i],Y[i]-1})){
v[i].push_back(k[{X[i],Y[i]-1}]);
v[k[{X[i],Y[i]-1}]].push_back(i);
}
if(k.count({X[i],Y[i]+1})){
v[i].push_back(k[{X[i],Y[i]+1}]);
v[k[{X[i],Y[i]+1}]].push_back(i);
}
k[{X[i],Y[i]}]=i;
}
ll tot=0;
for(int i=0;i<N;i++){
vector<bool> vis(N,0);
queue<pair<int,int> >q;
q.push({i,0});
while(!q.empty()){
int pos=q.front().first;
int d=q.front().second;
q.pop();
if(vis[pos])continue;
vis[pos]=1;
tot+=d;
for(int y:v[pos])q.push({y,d+1});
}
}
return (tot/2)%mod;
}
/*
#define inbuf_len 1 << 16
#define outbuf_len 1 << 16
int DistanceSum(int N, int *X, int *Y);
int main() {
int tmp;
char *inbuf, *outbuf;
inbuf = (char*) malloc(inbuf_len * sizeof(char));
outbuf = (char*) malloc(outbuf_len * sizeof(char));
tmp = setvbuf(stdin, inbuf, _IOFBF, inbuf_len);
assert(tmp == 0);
tmp = setvbuf(stdout, outbuf, _IOFBF, outbuf_len);
assert(tmp == 0);
int N, i;
tmp = scanf("%d", &N);
assert(tmp == 1);
int *sq_x, *sq_y;
sq_x = (int*) malloc(N * sizeof(int));
sq_y = (int*) malloc(N * sizeof(int));
for (i = 0; i < N; i++) {
tmp = scanf("%d %d", &sq_x[i], &sq_y[i]);
assert(tmp == 2);
}
int ds = DistanceSum(N, sq_x, sq_y);
printf("%d\n", ds);
return 0;
}*/
/*
11
2 5
2 6
3 3
3 6
4 3
4 4
4 5
4 6
5 3
5 4
5 6
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
2688 KB |
Output is correct |
2 |
Correct |
2 ms |
2688 KB |
Output is correct |
3 |
Correct |
2 ms |
2688 KB |
Output is correct |
4 |
Correct |
2 ms |
2688 KB |
Output is correct |
5 |
Correct |
4 ms |
2688 KB |
Output is correct |
6 |
Correct |
3 ms |
2688 KB |
Output is correct |
7 |
Correct |
5 ms |
2688 KB |
Output is correct |
8 |
Correct |
3 ms |
2688 KB |
Output is correct |
9 |
Correct |
3 ms |
2688 KB |
Output is correct |
10 |
Correct |
3 ms |
2688 KB |
Output is correct |
11 |
Correct |
3 ms |
2688 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
28 ms |
2816 KB |
Output is correct |
2 |
Correct |
32 ms |
2816 KB |
Output is correct |
3 |
Correct |
65 ms |
2816 KB |
Output is correct |
4 |
Correct |
74 ms |
2816 KB |
Output is correct |
5 |
Correct |
123 ms |
2944 KB |
Output is correct |
6 |
Correct |
131 ms |
2944 KB |
Output is correct |
7 |
Correct |
114 ms |
3064 KB |
Output is correct |
8 |
Correct |
134 ms |
2944 KB |
Output is correct |
9 |
Correct |
145 ms |
2944 KB |
Output is correct |
10 |
Correct |
142 ms |
2944 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1081 ms |
4856 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1077 ms |
4856 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |