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 long long long
#define all(x) (x).begin(), (x).end()
#define pii pair<int, int>
#define x first
#define y second
using namespace std;
const int M = 1e9;
const int N = 1e5+5;
vector<set<int> > g;
vector<int> sz;
vector<pii> vec;
int n, ans;
pii dfs(int u, int p) {
pii now(sz[u], 0);
for(auto v : g[u]) if(v != p) {
pii z = dfs(v, u);
ans = (ans + 1ll * now.x * z.y + 1ll * z.x * now.y) % M;
now.x = (now.x + z.x) % M, now.y = (now.y + z.y) % M;
}
return pii(now.x, (now.y + now.x) % M);
}
void solve() {
g = vector<set<int> >(n+1), sz = vector<int>(n+1);
sort(all(vec));
map<pii, int> Mp;
for(int i = 1, p = 0; p != n; ++i) {
do {
int z = Mp[pii(vec[p].x-1, vec[p].y)];
if(z) g[z].emplace(i), g[i].emplace(z);
Mp[vec[p++]] = i, sz[i]++;
} while(p < n && vec[p-1].x == vec[p].x && vec[p-1].y + 1 == vec[p].y);
}
dfs(1, 0);
}
int DistanceSum(int n, int x[], int y[]) {
::n = n;
for(int i = 0; i < n; ++i) vec.emplace_back(x[i], y[i]);
solve();
for(auto &x : vec) swap(x.x, x.y);
solve();
return ans;
}
# | 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... |