#include <bits/stdc++.h>
using namespace std ;
#define ii pair<int,int>
#define fi first
#define se second
#define vi vector<int>
#define vii vector<ii>
typedef long long ll;
const ll mod = 1e9 + 7 ;
int n , ncnt = 0 ;
ll cst[100010] , res = 0 ;
ii grid[100010] ;
vi adj[100010] ;
map<ii , int > mp ;
bool visited[100010] ;
void dfs(int x ,int par){
visited[x] = 1 ;
for(int v : adj[x]){
if(v == par || visited[v]) continue ;
dfs(v ,x ) ;
res = res + (cst[v] * (n-cst[v]))%mod ;
res %= mod ;
cst[x] += cst[v] ;
}
}
void find_ans(int *X , int *Y){
mp.clear() ;
for(int i=0;i<n;i++) grid[i] = {X[i] , Y[i]} ;
sort(grid , grid+n) ;
int j ;
ncnt = 0 ;
memset(cst , 0 ,sizeof cst) ;
for(int i=0;i<n;){
j = i ;
int lst = -2 , tmp ;
while( j < n && grid[i].fi == grid[j].fi ){
if(abs(grid[j].se - lst) > 1) ncnt++ ;
// printf("%d : %d\n" , j , ncnt) ;
mp[grid[j]] = ncnt ; cst[ncnt]++ ;
tmp = mp[ ii(grid[j].fi -1 , grid[j].se) ] ;
if( tmp ) {
adj[tmp ].push_back(ncnt) ;
adj[ncnt].push_back(tmp ) ;
}
lst = grid[j].se ;
j++ ;
}
i = j ;
}
// for(int i=1;i<=ncnt;i++) printf("%d " , cst[i]) ;
// cout << endl ;
memset(visited , 0 , sizeof visited) ;
dfs(1 , 1) ;
for(int i=0;i<=ncnt;i++) adj[i].clear() ;
}
int DistanceSum(int N, int *X, int *Y) {
n = N ; res = 0 ;
find_ans( X , Y ) ;
find_ans( Y , X ) ;
return res%mod ;
}
// /*
// #include <stdio.h>
// #include <stdlib.h>
// #include <assert.h>
// #define inbuf_len 1 << 16
// #define outbuf_len 1 << 16
// int DistanceSum(int N, int *X, int *Y);
// int main() {
// freopen("example.txt" , "r" , stdin ) ;
// int tmp;
// /* Set input and output buffering */
// 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);
// }
// printf("ok\n") ;
// int ds = DistanceSum(N, sq_x, sq_y);
// printf("%d\n", ds);
// return 0;
// }
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
6168 KB |
Output is correct |
2 |
Correct |
3 ms |
6168 KB |
Output is correct |
3 |
Correct |
0 ms |
6168 KB |
Output is correct |
4 |
Correct |
0 ms |
6168 KB |
Output is correct |
5 |
Correct |
3 ms |
6168 KB |
Output is correct |
6 |
Correct |
3 ms |
6168 KB |
Output is correct |
7 |
Correct |
0 ms |
6168 KB |
Output is correct |
8 |
Correct |
0 ms |
6168 KB |
Output is correct |
9 |
Correct |
0 ms |
6168 KB |
Output is correct |
10 |
Correct |
0 ms |
6168 KB |
Output is correct |
11 |
Correct |
0 ms |
6168 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
6300 KB |
Output is correct |
2 |
Correct |
3 ms |
6300 KB |
Output is correct |
3 |
Correct |
0 ms |
6300 KB |
Output is correct |
4 |
Correct |
0 ms |
6300 KB |
Output is correct |
5 |
Correct |
0 ms |
6432 KB |
Output is correct |
6 |
Correct |
6 ms |
6300 KB |
Output is correct |
7 |
Correct |
0 ms |
6432 KB |
Output is correct |
8 |
Correct |
0 ms |
6300 KB |
Output is correct |
9 |
Correct |
3 ms |
6300 KB |
Output is correct |
10 |
Correct |
3 ms |
6300 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
19 ms |
7896 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
33 ms |
8424 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |