#include "bits/stdc++.h"
using namespace std;
using ll = long long;
using pii = pair<int, int>;
const int maxn = 1e5 + 5;
const int mod = 1e9;
vector<int> graph[maxn];
vector<ll> sub(maxn), sz(maxn);
void dfs(int u, int p) {
sub[u] = sz[u];
for(int &v : graph[u]) {
if(v == p) continue;
dfs(v, u);
// cout << u << " -> " << v << '\n';
sub[u] += sub[v];
}
}
ll solve(vector<pii> v, int N, int *X, int *Y) {
for(int i=0; i<maxn; i++) {
graph[i].clear();
sub[i] = sz[i] = 0;
}
int mn = 1e9, mx = 0;
for(int i=0; i<N; i++) {
mn = min(mn, v[i].first);
mx = max(mx, v[i].first);
}
int curr = 1, p = 0, start = 0;
vector<array<int, 3> > ivals;
for(int i=mn; i<=mx; i++) {
start = p;
int last = v[p].second-1;
vector<array<int, 3> > nvals;
int ptr = 0;
while(p < N && v[p].first == i) {
if(v[p].second - last > 1) {
for(auto &[L, r, id] : ivals) {
if(min(r, v[p-1].second) - max(L, v[start].second) >= 0) {
graph[id].push_back(curr);
graph[curr].push_back(id);
}
}
nvals.push_back({ v[start].second, v[p-1].second, curr });
start = p;
curr++;
}
sz[curr]++;
last = v[p].second;
p++;
}
if(start < p) {
for(auto &[L, r, id] : ivals) {
if(min(r, v[p-1].second) - max(L, v[start].second) >= 0) {
graph[id].push_back(curr);
graph[curr].push_back(id);
}
}
nvals.push_back({ v[start].second, v[p-1].second, curr });
start = p;
}
curr++;
ivals = nvals;
}
dfs(1, 1);
ll ans = 0;
for(int i=2; i<curr; i++) ans = (ans + sub[i] % mod * ((ll)N - sub[i]) % mod) % mod;
return ans;
}
int DistanceSum(int N, int *X, int *Y) {
vector<pii> v;
for(int i=0; i<N; i++) v.push_back({ X[i], Y[i] });
sort(v.begin(), v.end());
ll ans = solve(v, N, X, Y);
for(int i=0; i<N; i++) swap(v[i].first, v[i].second);
sort(v.begin(), v.end());
return (ans + solve(v, N, X, Y)) % mod;
}
Compilation message
city.cpp: In function 'll solve(std::vector<std::pair<int, int> >, int, int*, int*)':
city.cpp:40:13: warning: unused variable 'ptr' [-Wunused-variable]
40 | int ptr = 0;
| ^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4188 KB |
Output is correct |
2 |
Correct |
2 ms |
4188 KB |
Output is correct |
3 |
Correct |
2 ms |
4184 KB |
Output is correct |
4 |
Correct |
2 ms |
4188 KB |
Output is correct |
5 |
Correct |
2 ms |
4188 KB |
Output is correct |
6 |
Correct |
2 ms |
4440 KB |
Output is correct |
7 |
Correct |
2 ms |
4188 KB |
Output is correct |
8 |
Correct |
2 ms |
4188 KB |
Output is correct |
9 |
Correct |
2 ms |
4188 KB |
Output is correct |
10 |
Correct |
2 ms |
4188 KB |
Output is correct |
11 |
Correct |
2 ms |
4188 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4184 KB |
Output is correct |
2 |
Correct |
2 ms |
4188 KB |
Output is correct |
3 |
Correct |
2 ms |
4188 KB |
Output is correct |
4 |
Correct |
2 ms |
4188 KB |
Output is correct |
5 |
Correct |
3 ms |
4444 KB |
Output is correct |
6 |
Correct |
2 ms |
4444 KB |
Output is correct |
7 |
Correct |
2 ms |
4444 KB |
Output is correct |
8 |
Correct |
2 ms |
4444 KB |
Output is correct |
9 |
Correct |
2 ms |
4188 KB |
Output is correct |
10 |
Correct |
2 ms |
4444 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
4956 KB |
Output is correct |
2 |
Correct |
7 ms |
4956 KB |
Output is correct |
3 |
Correct |
14 ms |
5744 KB |
Output is correct |
4 |
Correct |
15 ms |
5596 KB |
Output is correct |
5 |
Correct |
27 ms |
6904 KB |
Output is correct |
6 |
Correct |
37 ms |
6860 KB |
Output is correct |
7 |
Correct |
28 ms |
6868 KB |
Output is correct |
8 |
Correct |
28 ms |
6872 KB |
Output is correct |
9 |
Correct |
29 ms |
6980 KB |
Output is correct |
10 |
Correct |
29 ms |
9004 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
5176 KB |
Output is correct |
2 |
Correct |
8 ms |
5080 KB |
Output is correct |
3 |
Correct |
20 ms |
6156 KB |
Output is correct |
4 |
Correct |
17 ms |
5968 KB |
Output is correct |
5 |
Correct |
40 ms |
7924 KB |
Output is correct |
6 |
Correct |
31 ms |
7360 KB |
Output is correct |
7 |
Correct |
42 ms |
8144 KB |
Output is correct |
8 |
Correct |
31 ms |
7420 KB |
Output is correct |
9 |
Correct |
30 ms |
7128 KB |
Output is correct |
10 |
Correct |
40 ms |
7116 KB |
Output is correct |