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;
typedef int ll;
const ll INF = 1e9;
ll MOD = 998244353;
typedef pair<ll,ll> ii;
#define iii pair<ii,int>
#define f(i,a,b) for(ll i = a;i < b;i++)
#define pb push_back
#define vll vector<ll>
#define F first
#define S second
#define all(x) (x).begin(), (x).end()
///I hope I will get uprating and don't make mistakes
///I will never stop programming
///sqrt(-1) Love C++
///Please don't hack me
///@TheofanisOrfanou Theo830
///Think different approaches (bs,dp,greedy,graphs,shortest paths,mst)
///Stay Calm
///Look for special cases
///Beware of overflow and array bounds
///Think the problem backwards
///Training
vector<set<ll> >adj;
ll par[100005];
ll siz[100005];
ll s[100005];
int ans = 0;
int n;
ll find_par(ll a){
if(par[a] == a){
return a;
}
return par[a] = find_par(par[a]);
}
void enose(ll a,ll b){
ll p1 = find_par(a);
ll p2 = find_par(b);
par[p1] = p2;
siz[p2] += siz[p1];
siz[p1] = 0;
}
void dfs(ll idx,ll par){
idx = find_par(idx);
s[idx] = siz[idx];
for(auto x:adj[idx]){
if(x != par){
dfs(x,idx);
s[idx] += s[x];
ans += (1LL * (n - s[x]) * s[x]) % INF;
ans %= INF;
}
}
}
int DistanceSum(int N, int *X, int *Y){
n = N;
vector<iii>arr;
f(i,0,n){
par[i] = i;
siz[i] = 1;
arr.pb(iii(ii(X[i],Y[i]),i));
}
sort(all(arr));
f(i,1,n){
if(arr[i].F.F == arr[i-1].F.F && arr[i-1].F.S + 1 == arr[i].F.S){
enose(arr[i].S,arr[i-1].S);
}
}
f(i,0,n){
swap(arr[i].F.F,arr[i].F.S);
}
sort(all(arr));
adj.assign(n+5,set<ll>());
f(i,1,n){
if(arr[i].F.F == arr[i-1].F.F && arr[i-1].F.S + 1 == arr[i].F.S){
adj[find_par(arr[i].S)].insert(find_par(arr[i-1].S));
adj[find_par(arr[i-1].S)].insert(find_par(arr[i].S));
}
}
dfs(0,-1);
f(i,0,n){
par[i] = i;
siz[i] = 1;
s[i] = 0;
}
f(i,1,n){
if(arr[i].F.F == arr[i-1].F.F && arr[i-1].F.S + 1 == arr[i].F.S){
enose(arr[i].S,arr[i-1].S);
}
}
f(i,0,n){
swap(arr[i].F.F,arr[i].F.S);
}
sort(all(arr));
adj.assign(n+5,set<ll>());
f(i,1,n){
if(arr[i].F.F == arr[i-1].F.F && arr[i-1].F.S + 1 == arr[i].F.S){
adj[find_par(arr[i].S)].insert(find_par(arr[i-1].S));
adj[find_par(arr[i-1].S)].insert(find_par(arr[i].S));
}
}
dfs(0,-1);
return ans;
}
/*
#define inbuf_len 1 << 16
#define outbuf_len 1 << 16
int DistanceSum(int N, int *X, int *Y);
int main() {
int tmp;
char *inbuf, *outbuf;
inbuf = (char*) malloc(inbuf_len * sizeof(char));
outbuf = (char*) malloc(outbuf_len * sizeof(char));
tmp = setvbuf(stdin, inbuf, _IOFBF, inbuf_len);
assert(tmp == 0);
tmp = setvbuf(stdout, outbuf, _IOFBF, outbuf_len);
assert(tmp == 0);
int N, i;
tmp = scanf("%d", &N);
assert(tmp == 1);
int *sq_x, *sq_y;
sq_x = (int*) malloc(N * sizeof(int));
sq_y = (int*) malloc(N * sizeof(int));
for (i = 0; i < N; i++) {
tmp = scanf("%d %d", &sq_x[i], &sq_y[i]);
assert(tmp == 2);
}
int ds = DistanceSum(N, sq_x, sq_y);
printf("%d\n", ds);
return 0;
}
*/
/*
11
2 5
2 6
3 3
3 6
4 3
4 4
4 5
4 6
5 3
5 4
5 6
*/
# | 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... |