#include <bits/stdc++.h>
#define pb push_back
#define int long long
#define pii pair<int,int>
#define ff first
#define ss second
using namespace std;
#define debug(args...) kout("[ + " string(#args) + " ]", args)
void kout() { cerr << endl; }
template <class T, class ...U> void kout(T a, U ...b) { cerr << a << ' ',kout(b...); }
template <class T> void pary(T L, T R) { while (L != R) cerr << *L << " \n"[++L==R]; }
const int MOD = 1000000000;
const int INF = 1e18;
const int MAXN = 100000;
vector <int> path[MAXN];
vector <int> deg;
vector <bool> vis;
map <pii,int> appr;
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
int BFS(int s, int N) {
vector <int> dis(N, INF);
dis[s] = 0;
queue <int> nxt;
nxt.push(s);
while (nxt.size()) {
int now = nxt.front();
nxt.pop();
for (int i : path[now]) {
if (dis[i] == INF) {
dis[i] = dis[now] + 1;
nxt.push(i);
}
}
}
int sum = 0;
for (int i = s+1; i < N; i++)
sum += dis[i];
return sum;
}
void init(int N) {
for (int i = 0; i < N; i++)
path[i].clear();
appr.clear();
deg.resize(N);
vis.resize(N);
}
signed DistanceSum(signed N, signed *X, signed *Y) {
init(N);
for (int i = 0; i < N; i++)
appr[pii(X[i], Y[i])] = i;
int mnX = INF, mxX = -INF, mnY = INF, mxY = -INF;
for (int i = 0; i < N; i++) {
mnX = min(mnX, (int)X[i]);
mnY = min(mnY, (int)Y[i]);
mxX = max(mxX, (int)X[i]);
mxY = max(mxY, (int)Y[i]);
for (int j = 0; j < 4; j++) {
if (appr.count(pii(X[i]+dx[j], Y[i]+dy[j]))) {
int id = appr[pii(X[i]+dx[j], Y[i]+dy[j])];
path[id].pb(i);
path[i].pb(id);
}
}
}
int ans = 0;
if ((mxX - mnX + 1) * (mxY - mnY + 1) != N) {
for (int i = 0; i < N; i++)
ans = (ans + BFS(i, N)) % MOD;
}
return ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2668 KB |
Output is correct |
2 |
Correct |
3 ms |
2668 KB |
Output is correct |
3 |
Correct |
2 ms |
2668 KB |
Output is correct |
4 |
Correct |
3 ms |
2668 KB |
Output is correct |
5 |
Correct |
2 ms |
2668 KB |
Output is correct |
6 |
Correct |
3 ms |
2668 KB |
Output is correct |
7 |
Correct |
3 ms |
2668 KB |
Output is correct |
8 |
Correct |
3 ms |
2668 KB |
Output is correct |
9 |
Correct |
3 ms |
2668 KB |
Output is correct |
10 |
Correct |
3 ms |
2668 KB |
Output is correct |
11 |
Correct |
5 ms |
2668 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
36 ms |
2796 KB |
Output is correct |
2 |
Correct |
36 ms |
2796 KB |
Output is correct |
3 |
Correct |
81 ms |
2924 KB |
Output is correct |
4 |
Correct |
81 ms |
2924 KB |
Output is correct |
5 |
Correct |
136 ms |
3052 KB |
Output is correct |
6 |
Correct |
149 ms |
3180 KB |
Output is correct |
7 |
Correct |
140 ms |
3052 KB |
Output is correct |
8 |
Correct |
149 ms |
3052 KB |
Output is correct |
9 |
Correct |
151 ms |
3180 KB |
Output is correct |
10 |
Correct |
147 ms |
3052 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1085 ms |
6124 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1095 ms |
5740 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |