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
const int mod = 1e9;
using namespace std;
//g++ -std=c++17 -Wshadow -Wall -Wextra -o "%e" "%f" -g -fsanitize=address -fsanitize=undefined -D_GLIBCXX_DEBUG
int n;
int nodes;
static ll solve(int *X, int *Y) {
nodes = 0;
map<int, set<int> > mat;
map<int,int> w;
map<int, map<int, int> > col;
for(int i = 0; i < n; i++)
col[X[i]][Y[i]];
set<pair<int, int> > edg;
vector<vector<int> > g;
for(auto &v : col) {
int x = v.first;
for(auto &y : v.second) {
if(col[x].find(y.first - 1) != col[x].end())
y.second = col[x][y.first - 1], w[y.second]++;
else
w[nodes] = 1, y.second = nodes++;
if(col.find(x - 1) == col.end() || col[x - 1].find(y.first) == col[x - 1].end())
continue;
//cerr << y.second<< ' ' << x << ' ' << y.first << ' ' << col[x - 1][y.first] << '\n';
edg.insert({col[x - 1][y.first], col[x][y.first]});
}
}
g.resize(nodes);
for(auto x : edg) {
//cerr << x.first << ' ' << x.second << '\n';
g[x.first].push_back(x.second);
g[x.second].push_back(x.first);
}
//cerr << "Ma-ta\n";
ll total = 0;
function<ll(int,int)> dfs = [&](int node, int f) {
int sum = w[node];
for(auto x : g[node])
if(x != f)
sum += dfs(x, node);
//cerr << node << ' ' << f << ' ' << w[node] << '\n';
total += (ll)(n - sum) * sum % mod;
return sum;
};
dfs(0, 0);
return total % mod;
}
int DistanceSum(int N, int *X, int *Y) {
n = N;
return (solve(X, Y) + solve(Y, X)) % mod;
}
# | 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... |