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>
using namespace std;
#define sz(a) ((int)(a).size())
typedef vector<int> vint;
typedef vector<vint> vvint;
#ifndef wambule
// #include ".h"
#else
#endif
const int mod = 1e9;
const int N = 100005;
long long dr;
vector<int> u[2][N];
int g, w[2], gn, z[2][N];
bool bh[2][N];
int G(int x) {
// cout << "[+] " << x << endl;
bh[g][x] = 1;
int r = z[g][x];
for(int z : u[g][x]) {
if(!bh[g][z]) r += G(z);
}
dr += 1ll * r * (gn - r);
// cout << "[-] " << x << endl;
return r;
}
int DistanceSum(int n, int *x, int *y) {
gn = n;
for(int i = 1; i <= n; ++i) {
u[0][i].clear();
u[1][i].clear();
z[0][i] = 0;
z[1][i] = 0;
bh[0][i] = 0;
bh[1][i] = 0;
}
vector<pair<int, int>> v;
for(int i = 0; i < n; ++i) {
v.push_back({x[i], y[i]});
}
g = w[0] = 0;
map<pair<int, int>, int> ma;
sort(v.begin(), v.end());
for(int i = 0; i < sz(v); ++i) {
if(!i || (i && v[i].second > v[i - 1].second + 1) || (i && v[i].first > v[i - 1].first)) ++w[g];
if(int a = ma[{v[i].first - 1, v[i].second}]) {
u[g][a].push_back(w[g]);
u[g][w[g]].push_back(a);
}
ma[{v[i].first, v[i].second}] = w[g];
++z[g][w[g]];
}
for(int i = 0; i < sz(v); ++i) {
swap(v[i].first, v[i].second);
}
g = 1; w[1] = 0;
ma.clear();
sort(v.begin(), v.end());
for(int i = 0; i < sz(v); ++i) {
if(!i || (i && v[i].second > v[i - 1].second + 1) || (i && v[i].first > v[i - 1].first)) ++w[g];
if(int a = ma[{v[i].first - 1, v[i].second}]) {
u[g][a].push_back(w[g]);
u[g][w[g]].push_back(a);
}
ma[{v[i].first, v[i].second}] = w[g];
++z[g][w[g]];
}
// cout << "| - [ 0 ] - |" << endl;
dr = g = 0;
G(1);
// cout << "| - [ 1 ] - |" << endl;
G(g = 1);
return dr % mod;
}
#ifdef wambule
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int n = 11;
int x[] = {2, 2, 3, 3, 4, 4, 4, 4, 5, 5, 5};
int y[] = {5, 6, 3, 6, 3, 4, 5, 6, 3, 4, 6};
cout << DistanceSum(n, x, y) << endl;
return 0;
}
#endif
# | 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... |