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 <vector>
#include <algorithm>
using namespace std;
#define MAXN 100005
#define MOD 1000000000
typedef long long lld;
static int N,M,V[MAXN],ans;
static struct P{
P(){}
P(int x,int y){ this->x=x; this->y=y; }
int x,y;
bool operator < (const P &ot)const{
return y!=ot.y?y<ot.y:x<ot.x;
}
} A[MAXN];
static struct NODE{
int y,x1,x2;
} T[MAXN];
vector <int> con[MAXN];
inline bool cross(int a,int b,int p,int q){ return !(b < p || a > q); }
inline bool cross(const NODE &a,const NODE &b){
if (abs(a.y-b.y) == 1 && cross(a.x1,a.x2,b.x1,b.x2)) return 1;
return 0;
}
int dfs(int n)
{
int i,k,cnt=T[n].x2-T[n].x1+1;
V[n] = 1;
for (i=con[n].size();i--;){
k = con[n][i];
if (V[k]) continue;
cnt += dfs(k);
}
ans = (ans+lld(cnt)*(N-cnt)%MOD)%MOD;
return cnt;
}
int proc()
{
int i,k;
sort(A+1,A+N+1);
M = 0;
for (i=k=1;i<=N;i++){ // make nodes
if (i == N || A[i+1].y != A[i].y || A[i+1].x > A[i].x+1){
T[++M].y = A[i].y;
T[M].x1 = A[k].x;
T[M].x2 = A[i].x;
k = i+1;
}
}
for (i=1;i<=M;i++) con[i].clear();
for (i=k=1;i<=M;i++){ // make edges
while (k < M && T[k].y < T[i].y+1 || (T[k].y == T[i].y+1 && T[k].x2 < T[i].x1)) k++;
if (k <= M && cross(T[i],T[k])) con[i].push_back(k), con[k].push_back(i);
while (k < M && cross(T[i],T[k+1])){
++k;
con[i].push_back(k), con[k].push_back(i);
}
}
ans = 0;
for (i=1;i<=N;i++) V[i] = 0;
dfs(1);
return ans;
}
int DistanceSum(int n, int *x_arr, int *y_arr)
{
int i,a,b;
N = n;
for (i=0;i<N;i++) A[i+1] = P(x_arr[i],y_arr[i]);
a = proc();
for (i=1;i<=N;i++) swap(A[i].x,A[i].y);
b = proc();
return (a+b)%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... |