Submission #314797

# Submission time Handle Problem Language Result Execution time Memory
314797 2020-10-21T08:47:38 Z talant117408 Ideal city (IOI12_city) C++17
0 / 100
1000 ms 41680 KB
#include <bits/stdc++.h>
 
using namespace std;
 
typedef long long ll;
typedef pair <int, int> 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};
int x[100007], y[100007];
ll ans, dist[2007][2007];
vector <vector <int>> graph(100007);
const int mod = 1e9;
vector <pii> points;

void bfs(int p){
    for(int i = 0; i < n; i++){
        dist[p][i] = 9e18;
    }
    dist[p][p] = 0;
    queue <int> K;
    K.push(p);
    
    while(sz(K)){
        auto u = K.front(); K.pop();
        
        for(auto to : graph[u]){
            if(dist[p][to] > dist[p][u]){
                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++){
        int XX = points[i].first, YY = points[i].second;
        for(j = 0; j < 4; j++){
            int nx = moveX[j]+XX;
            int 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{
        int mx1 = 0, mn1 = INT_MAX, mx2 = 0, mn2 = INT_MAX;
        for(i = 0; i < n; i++){
            mx1 = max(mx1, x[i]);
            mx2 = max(mx2, y[i]);
            mn1 = min(mn1, x[i]);
            mn2 = min(mn2, y[i]);
        }
        int nn = mx1-mn1+1, mm = mx2-mn2+1;
        for(i = 1; i <= nn; i++){
            for(j = 1; j <= mm; j++){
                ans = add(ans, mult(mm, calc(nn-i)));
                ans = add(ans, calc(mm-j));
                ans = add(ans, mult(nn-i, add(calc(mm-j), calc(j-1))));
            }
        }
    }
    
    return ans;
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2688 KB Output is correct
2 Correct 2 ms 2816 KB Output is correct
3 Correct 2 ms 2816 KB Output is correct
4 Correct 4 ms 3200 KB Output is correct
5 Correct 56 ms 3392 KB Output is correct
6 Correct 16 ms 3840 KB Output is correct
7 Execution timed out 1101 ms 41680 KB Time limit exceeded
8 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1099 ms 38916 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 21 ms 3836 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 26 ms 3836 KB Output isn't correct
2 Halted 0 ms 0 KB -