Submission #28201

# Submission time Handle Problem Language Result Execution time Memory
28201 2017-07-15T15:14:44 Z noobprogrammer Ideal city (IOI12_city) C++14
100 / 100
166 ms 19628 KB
#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;
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;

// }
# Verdict Execution time Memory 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 0 ms 6168 KB Output is correct
7 Correct 3 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
# Verdict Execution time Memory Grader output
1 Correct 3 ms 6300 KB Output is correct
2 Correct 0 ms 6300 KB Output is correct
3 Correct 3 ms 6300 KB Output is correct
4 Correct 0 ms 6300 KB Output is correct
5 Correct 3 ms 6432 KB Output is correct
6 Correct 3 ms 6300 KB Output is correct
7 Correct 3 ms 6432 KB Output is correct
8 Correct 3 ms 6300 KB Output is correct
9 Correct 3 ms 6300 KB Output is correct
10 Correct 3 ms 6300 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 19 ms 7896 KB Output is correct
2 Correct 23 ms 8160 KB Output is correct
3 Correct 56 ms 10256 KB Output is correct
4 Correct 53 ms 10792 KB Output is correct
5 Correct 109 ms 14608 KB Output is correct
6 Correct 116 ms 15756 KB Output is correct
7 Correct 109 ms 15288 KB Output is correct
8 Correct 119 ms 14608 KB Output is correct
9 Correct 113 ms 15388 KB Output is correct
10 Correct 136 ms 19628 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 26 ms 8424 KB Output is correct
2 Correct 19 ms 8168 KB Output is correct
3 Correct 83 ms 11840 KB Output is correct
4 Correct 73 ms 11352 KB Output is correct
5 Correct 166 ms 17380 KB Output is correct
6 Correct 136 ms 16100 KB Output is correct
7 Correct 166 ms 17528 KB Output is correct
8 Correct 143 ms 16140 KB Output is correct
9 Correct 136 ms 15664 KB Output is correct
10 Correct 136 ms 15664 KB Output is correct