#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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
5888 KB |
Output is correct |
2 |
Correct |
0 ms |
5888 KB |
Output is correct |
3 |
Correct |
0 ms |
5888 KB |
Output is correct |
4 |
Correct |
0 ms |
6020 KB |
Output is correct |
5 |
Correct |
0 ms |
5888 KB |
Output is correct |
6 |
Correct |
0 ms |
6020 KB |
Output is correct |
7 |
Correct |
0 ms |
6020 KB |
Output is correct |
8 |
Correct |
0 ms |
6020 KB |
Output is correct |
9 |
Correct |
0 ms |
6020 KB |
Output is correct |
10 |
Correct |
0 ms |
6020 KB |
Output is correct |
11 |
Correct |
0 ms |
6020 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
6020 KB |
Output is correct |
2 |
Correct |
0 ms |
6020 KB |
Output is correct |
3 |
Correct |
0 ms |
6020 KB |
Output is correct |
4 |
Correct |
0 ms |
6020 KB |
Output is correct |
5 |
Correct |
0 ms |
6024 KB |
Output is correct |
6 |
Correct |
0 ms |
6024 KB |
Output is correct |
7 |
Correct |
0 ms |
6024 KB |
Output is correct |
8 |
Correct |
0 ms |
6024 KB |
Output is correct |
9 |
Correct |
0 ms |
6024 KB |
Output is correct |
10 |
Correct |
0 ms |
6024 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
6092 KB |
Output is correct |
2 |
Correct |
4 ms |
6092 KB |
Output is correct |
3 |
Correct |
20 ms |
6412 KB |
Output is correct |
4 |
Correct |
16 ms |
6412 KB |
Output is correct |
5 |
Correct |
40 ms |
6804 KB |
Output is correct |
6 |
Correct |
44 ms |
6804 KB |
Output is correct |
7 |
Correct |
44 ms |
6804 KB |
Output is correct |
8 |
Correct |
44 ms |
6804 KB |
Output is correct |
9 |
Correct |
44 ms |
6828 KB |
Output is correct |
10 |
Correct |
48 ms |
9076 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
6356 KB |
Output is correct |
2 |
Correct |
12 ms |
6224 KB |
Output is correct |
3 |
Correct |
32 ms |
6940 KB |
Output is correct |
4 |
Correct |
24 ms |
6676 KB |
Output is correct |
5 |
Correct |
56 ms |
7992 KB |
Output is correct |
6 |
Correct |
48 ms |
7256 KB |
Output is correct |
7 |
Correct |
56 ms |
8036 KB |
Output is correct |
8 |
Correct |
40 ms |
7200 KB |
Output is correct |
9 |
Correct |
48 ms |
7068 KB |
Output is correct |
10 |
Correct |
48 ms |
7068 KB |
Output is correct |