#include <bits/stdc++.h>
#define IO_OP ios::sync_with_stdio(0), cin.tie(0)
#define F first
#define S second
#define V vector
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define SZ(v) int((v).size())
#define ALL(v) (v).begin(), (v).end()
using namespace std;
typedef long long ll;
typedef pair<int, int> pi;
typedef V<int> vi;
const int INF = 1e9 + 7, M = 1000000000;
int dx[] = {0, 0, 1, -1}, dy[] = {1, -1, 0, 0};
void add(int& a, int b) {
a += b;
if(a >= M) a -= M;
}
int brute(int n, int* x, int* y) {
V<vi> G(n);
map<pi, int> mp;
for(int i = 0; i < n; i++)
mp[{x[i], y[i]}] = i;
for(int i = 0; i < n; i++) {
for(int _ = 0; _ < 4; _++) {
int xx = x[i] + dx[_], yy = y[i] + dy[_];
if(mp.count({xx, yy})) {
G[i].PB(mp[{xx, yy}]);
}
}
}
int ans = 0;
auto bfs = [&] (int s) {
queue<int> q;
vi d(n, -1);
q.push(s), d[s] = 0;
while(q.size()) {
int u = q.front(); q.pop();
if(u < s) add(ans, d[u]);
for(int v:G[u]) if(d[v] == -1) {
d[v] = d[u] + 1;
q.push(v);
}
}
};
for(int i = 0; i < n; i++) {
bfs(i);
}
return ans;
}
int convex(int n, int* x, int *y) {
map<int, int> mp;
int mnx = INF, mxx = -INF;
for(int i = 0; i < n; i++) {
mnx = min(mnx, x[i]);
mxx = max(mxx, x[i]);
mp[x[i]]++;
}
int ans = 0, cnt = 0, sum = 0;
for(int i = mnx; i <= mxx; i++) {
add(ans, 1LL * i * mp[i] % M * cnt % M);
add(ans, M - 1LL * sum * mp[i] % M);
add(cnt, mp[i]);
add(sum, 1LL * mp[i] * i % M);
}
return ans;
}
int DistanceSum(int n, int *x, int *y) {
if(n <= 2000) {
return brute(n, x, y);
} else {
return (convex(n, x, y) + convex(n, y, x)) % M;
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
304 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
2 ms |
212 KB |
Output is correct |
7 |
Correct |
2 ms |
212 KB |
Output is correct |
8 |
Correct |
2 ms |
212 KB |
Output is correct |
9 |
Correct |
2 ms |
300 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
2 ms |
308 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
22 ms |
432 KB |
Output is correct |
2 |
Correct |
26 ms |
420 KB |
Output is correct |
3 |
Correct |
59 ms |
500 KB |
Output is correct |
4 |
Correct |
56 ms |
496 KB |
Output is correct |
5 |
Correct |
99 ms |
572 KB |
Output is correct |
6 |
Correct |
102 ms |
516 KB |
Output is correct |
7 |
Correct |
109 ms |
572 KB |
Output is correct |
8 |
Correct |
97 ms |
564 KB |
Output is correct |
9 |
Correct |
95 ms |
576 KB |
Output is correct |
10 |
Correct |
96 ms |
468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
596 KB |
Output is correct |
2 |
Correct |
6 ms |
696 KB |
Output is correct |
3 |
Correct |
15 ms |
1176 KB |
Output is correct |
4 |
Correct |
11 ms |
1108 KB |
Output is correct |
5 |
Correct |
24 ms |
1896 KB |
Output is correct |
6 |
Correct |
24 ms |
1936 KB |
Output is correct |
7 |
Correct |
26 ms |
2104 KB |
Output is correct |
8 |
Correct |
24 ms |
1876 KB |
Output is correct |
9 |
Correct |
25 ms |
2004 KB |
Output is correct |
10 |
Correct |
37 ms |
3280 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
5 ms |
596 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |