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> //:3
using namespace std;
typedef long long ll;
#define all(a) (a).begin(), (a).end()
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define pi pair<int, int>
#define sz(x) (int)((x).size())
//#define int long long
const int dx[] = {0, 1, 0, -1};
const int dy[] = {1, 0, -1, 0};
const ll inf = 2e9;
const ll mod = 1e9 + 0;
const int N = 1e5 + 11;
const ll INF64 = 3e18 + 1;
const double eps = 1e-14;
const double PI = acos(-1);
//ifstream in(".in");
//ofstream out(".out");
pi v[N];
int comp[N], siz[N], siz1[N], sum[N], sum1[N], viz[N], k;
map<pi, int> a;
set<int> g[N], g1[N];
void dfs(int nod, int p){
sum[nod] = siz[nod];
for(auto it : g[nod])
if(it != p){
dfs(it, nod);
sum[nod] += sum[it];
}
}
void dfs1(int nod, int p){
sum1[nod] = siz1[nod];
for(auto it : g1[nod])
if(it != p){
dfs1(it, nod);
sum1[nod] += sum1[it];
}
}
int DistanceSum (int n, int X[], int Y[]){
for(int i = 1; i <= n; i++){
v[i] = {X[i - 1], Y[i - 1]};
a[v[i]] = i;
}
//horizontal
for(int i = 1; i <= n; i++){
if(viz[i])continue;
comp[i] = ++k;
int cnt = 0;
int x = X[i - 1], y = Y[i - 1];
while(a[{x, y + 1}]){
y++;
int idx = a[{x, y}];
comp[idx] = k;
viz[idx] = 1;
cnt++;
}
y = Y[i - 1];
while(a[{x, y - 1}]){
y--;
int idx = a[{x, y}];
comp[idx] = k;
viz[idx] = 1;
cnt++;
}
siz[k] = cnt;
}
for(int i = 1; i <= n; i++){
int I = X[i - 1], J = Y[i - 1];
int now = comp[i];
for(int l = 0; l < 4; l++){
int x = I + dx[l], y = J + dy[l];
if(!a[{x, y}])continue;
int next = comp[a[{x, y}]];
if(now == next)continue;
g[now].insert(next);
g[next].insert(now);
}
}
dfs(1, 0);
int ans = 0;
for(int i = 1; i <= k; i++){
ans = (ans + sum[i] * (n - sum[i]) + mod) % mod;
}
k = 0;
memset(viz, 0, sizeof(viz));
memset(comp, 0, sizeof(comp));
//vertical
for(int i = 1; i <= n; i++){
if(viz[i])continue;
comp[i] = ++k;
int cnt = 0;
int x = X[i - 1], y = Y[i - 1];
while(a[{x + 1, y}]){
x++;
int idx = a[{x, y}];
comp[idx] = k;
viz[idx] = 1;
cnt++;
}
x = X[i - 1];
while(a[{x - 1, y}]){
x--;
int idx = a[{x, y}];
comp[idx] = k;
viz[idx] = 1;
cnt++;
}
siz1[k] = cnt;
}
for(int i = 1; i <= n; i++){
int I = X[i - 1], J = Y[i - 1];
int now = comp[i];
for(int l = 0; l < 4; l++){
int x = I + dx[l], y = J + dy[l];
if(!a[{x, y}])continue;
int next = comp[a[{x, y}]];
if(now == next)continue;
g1[now].insert(next);
g1[next].insert(now);
}
}
dfs1(1, 0);
for(int i = 1; i <= k; i++){
ans = (ans + sum1[i] * (n - sum1[i]) + mod) % mod;
}
cout << ans << '\n';
}
Compilation message (stderr)
city.cpp: In function 'int DistanceSum(int, int*, int*)':
city.cpp:179:1: warning: no return statement in function returning non-void [-Wreturn-type]
179 | }
| ^
# | 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... |