제출 #314806

#제출 시각아이디문제언어결과실행 시간메모리
314806talant117408이상적인 도시 (IOI12_city)C++17
55 / 100
169 ms34424 KiB
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair <ll, ll> pii; #define precision(n) fixed << setprecision(n) #define pb push_back #define ub upper_bound #define lb lower_bound #define mp make_pair #define eps (double)1e-9 #define PI 2*acos(0.0) #define endl "\n" #define sz(v) int((v).size()) #define all(v) v.begin(),v.end() #define rall(v) v.rbegin(),v.rend() #define do_not_disturb ios::sync_with_stdio(0);cin.tie(0);cout.tie(0); int n, moveX[] = {-1, 0, 0, 1}, moveY[] = {0, -1, 1, 0}; ll x[100007], y[100007]; ll ans, dist[2007][2007]; vector <vector <int>> graph(100007); const int mod = 1e9; vector <pii> points; map <ll, pii> row; void bfs(int p){ for(int i = 0; i < n; i++){ dist[p][i] = 9e18; } dist[p][p] = 0; queue <ll> K; K.push(p); while(sz(K)){ auto u = K.front(); K.pop(); for(auto to : graph[u]){ if(dist[p][to] == 9e18){ dist[p][to] = dist[p][u] + 1; K.push(to); } } } } ll add(ll a, ll b){ a = ((a%mod)+mod)%mod; b = ((b%mod)+mod)%mod; return (a+b)%mod; } ll mult(ll a, ll b){ a = ((a%mod)+mod)%mod; b = ((b%mod)+mod)%mod; return (a*b)%mod; } ll calc(ll a){ return a*(a+1)/2; } int DistanceSum(int N, int *X, int *Y) { int i, j; n = N; for(i = 0; i < n; i++){ x[i] = X[i]; y[i] = Y[i]; points.pb(mp(x[i], y[i])); } sort(all(points)); for(i = 0; i < n; i++){ x[i] = points[i].first; y[i] = points[i].second; } for(i = 0; i < n; i++){ ll XX = points[i].first, YY = points[i].second; for(j = 0; j < 4; j++){ ll nx = moveX[j]+XX; ll ny = moveY[j]+YY; auto it = lb(all(points), mp(nx, ny))-points.begin(); if(it != sz(points) && points[it] == mp(nx, ny)){ graph[i].pb(it); } } } if(n <= 2000){ for(i = 0; i < n; i++){ bfs(i); } for(i = 0; i < n; i++){ for(j = i+1; j < n; j++){ ans = add(ans, dist[i][j]); } } } else{ vector <ll> prefx, prefy, Xes, Yes; prefx.pb(0); prefy.pb(0); Xes.pb(0); Yes.pb(0); for(i = 0; i < n; i++){ Xes.pb(x[i]); Yes.pb(y[i]); prefx.pb(x[i]); prefy.pb(y[i]); } sort(all(Xes)); sort(all(Yes)); sort(all(prefx)); sort(all(prefy)); for(i = 1; i <= n; i++){ prefx[i] += prefx[i-1]; prefy[i] += prefy[i-1]; } for(i = 1; i <= n; i++){ ans = add(ans, prefx.back()-prefx[i]-Xes[i]*(n-i)); ans = add(ans, prefy.back()-prefy[i]-Yes[i]*(n-i)); } } return ans; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...