Submission #28201

#TimeUsernameProblemLanguageResultExecution timeMemory
28201noobprogrammerIdeal city (IOI12_city)C++14
100 / 100
166 ms19628 KiB
#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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...