This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define f first
#define s second
using namespace std;
const int Nn = 1e5 + 5;
ll tot[Nn], ans;
int n, id[Nn], x[Nn], y[Nn], c[Nn];
int a[4] = {1, -1, 0, 0};
int b[4] = {0, 0, 1, -1};
map < pair < int , int > , int > f;
vector < int > v[Nn];
void dfs(int x, int p) {
tot[x] = c[x];
for (int i = 0; i < v[x].size(); ++i) {
int to = v[x][i];
if (to == p) continue;
dfs(to, x);
tot[x] += tot[to];
}
ans += tot[x] * (n - tot[x]);
}
void solve() {
vector < pair < pair < int , int > , int > > s;
for (int i = 0; i < n; ++i) {
f[{x[i], y[i]}] = i;
s.pb({{x[i], y[i]}, i});
}
sort(s.begin(), s.end());
int nd = 0;
for (int i = 0; i < s.size(); ++i) {
if (!i || (s[i - 1].f.f != s[i].f.f || s[i - 1].f.s + 1 < s[i].f.s)) ++nd;
id[s[i].s] = nd, ++c[nd];
}
vector < pair < int , int > > ss;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < 2; ++j) {
int Ax = x[i] + a[j], Ay = y[i] + b[j];
if (f.find({Ax, Ay}) != f.end()) {
int A = id[i];
int B = id[f[{Ax, Ay}]];
if (A > B) swap(A, B);
ss.pb({A, B});
}
}
}
sort(ss.begin(), ss.end());
for (int i = 0; i < ss.size(); ++i) {
if (!i || ss[i - 1] != ss[i]) {
v[ss[i].f].pb(ss[i].s);
v[ss[i].s].pb(ss[i].f);
}
}
dfs(1, 1);
}
void clear() {
f.clear();
for (int i = 1; i <= n; ++i) {
v[i].clear(), c[i] = tot[i] = 0;
}
for (int i = 0; i < n; ++i) {
swap(x[i], y[i]);
}
}
ll DistanceSum(int nn, int *X, int *Y) {
n = nn;
for (int i = 0; i < n; ++i) {
x[i] = X[i], y[i] = Y[i];
}
solve();
clear();
solve();
return ans;
}
Compilation message (stderr)
city.cpp: In function 'void dfs(int, int)':
city.cpp:21:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
21 | for (int i = 0; i < v[x].size(); ++i) {
| ~~^~~~~~~~~~~~~
city.cpp: In function 'void solve()':
city.cpp:38:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<std::pair<int, int>, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
38 | for (int i = 0; i < s.size(); ++i) {
| ~~^~~~~~~~~~
city.cpp:55:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
55 | for (int i = 0; i < ss.size(); ++i) {
| ~~^~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |