이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define DIM 100010
#define INF 2000000000
#define MOD 1000000000
using namespace std;
map <pair<int,int>,int> a;
set <int> L[DIM],L2[DIM];
deque <int> c;
pair <int,int> v[DIM];
int what_comp[DIM],Size[DIM],Size2[DIM],sum[DIM],sum2[DIM],viz[DIM];
int nr_nodes;
int di[] = {-1,1,0,0};
int dj[] = {0,0,-1,1};
void dfs (int nod, int tata){
sum[nod] = Size[nod];
for (auto vecin : L[nod])
if (vecin != tata){
dfs (vecin,nod);
sum[nod] += sum[vecin];
}}
void dfs2 (int nod, int tata){
sum2[nod] = Size2[nod];
for (auto vecin : L2[nod])
if (vecin != tata){
dfs2 (vecin,nod);
sum2[nod] += sum2[vecin];
}}
int DistanceSum (int n, int *X, int *Y){
int i;
long long sol = 0;
for (i=1;i<=n;i++){
v[i] = make_pair (X[i-1],Y[i-1]);
a[v[i]] = i;
}
/// grupez celulele pe orizontala si pe verticala si o sa am 2 arbori
/// orizontala
for (i=1;i<=n;i++){
if (!viz[i]){
viz[i] = 1;
what_comp[i] = ++nr_nodes;
int x = v[i].first, y = v[i].second, cnt = 1;
while (a[make_pair(x,y+1)]){
y++, cnt++;
int idx = a[make_pair(x,y)];
viz[idx] = 1;
what_comp[idx] = nr_nodes;
}
y = v[i].second;
while (a[make_pair(x,y-1)]){
y--, cnt++;
int idx = a[make_pair(x,y)];
viz[idx] = 1;
what_comp[idx] = nr_nodes;
}
Size[nr_nodes] = cnt;
}
}
/// construiesc primul arbore
for (i=1;i<=n;i++){
int ic = v[i].first, jc = v[i].second;
int x = what_comp[i];
for (int dir=0;dir<=3;dir++){
int iv = ic + di[dir];
int jv = jc + dj[dir];
int idx = a[make_pair(iv,jv)];
if (!idx)
continue;
int y = what_comp[idx];
if (x != y){
L[x].insert(y);
L[y].insert(x);
}}}
dfs (1,0);
for (i=1;i<=nr_nodes;i++)
sol += 1LL * sum[i] * (n - sum[i]);
memset(viz, 0, sizeof(viz));
memset(what_comp, 0, sizeof(what_comp));
nr_nodes = 0;
/// verticala
for (i=1;i<=n;i++){
if (!viz[i]){
viz[i] = 1;
what_comp[i] = ++nr_nodes;
int x = v[i].first, y = v[i].second, cnt = 1;
while (a[make_pair(x-1,y)]){
x--, cnt++;
int idx = a[make_pair(x,y)];
viz[idx] = 1;
what_comp[idx] = nr_nodes;
}
x = v[i].first;
while (a[make_pair(x+1,y)]){
x++, cnt++;
int idx = a[make_pair(x,y)];
viz[idx] = 1;
what_comp[idx] = nr_nodes;
}
Size2[nr_nodes] = cnt;
}
}
for (i=1;i<=n;i++){
int ic = v[i].first, jc = v[i].second;
int x = what_comp[i];
for (int dir=0;dir<=3;dir++){
int iv = ic + di[dir];
int jv = jc + dj[dir];
int idx = a[make_pair(iv,jv)];
if (!idx)
continue;
int y = what_comp[idx];
if (x != y){
L2[x].insert(y);
L2[y].insert(x);
}}}
dfs2 (1,0);
for (i=1;i<=nr_nodes;i++)
sol += 1LL * sum2[i] * (n - sum2[i]);
return sol % 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... |