제출 #591471

#제출 시각아이디문제언어결과실행 시간메모리
591471cheissmartIdeal city (IOI12_city)C++14
55 / 100
109 ms3280 KiB
#include <bits/stdc++.h>
#define IO_OP ios::sync_with_stdio(0), cin.tie(0)
#define F first
#define S second
#define V vector
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define SZ(v) int((v).size())
#define ALL(v) (v).begin(), (v).end()

using namespace std;

typedef long long ll;
typedef pair<int, int> pi;
typedef V<int> vi;

const int INF = 1e9 + 7, M = 1000000000;

int dx[] = {0, 0, 1, -1}, dy[] = {1, -1, 0, 0};

void add(int& a, int b) {
    a += b;
    if(a >= M) a -= M;
}

int brute(int n, int* x, int* y) {
    V<vi> G(n);
    map<pi, int> mp;
    for(int i = 0; i < n; i++)
        mp[{x[i], y[i]}] = i;
    for(int i = 0; i < n; i++) {
        for(int _ = 0; _ < 4; _++) {
            int xx = x[i] + dx[_], yy = y[i] + dy[_];
            if(mp.count({xx, yy})) {
                G[i].PB(mp[{xx, yy}]);
            }
        }
    }
    int ans = 0;
    auto bfs = [&] (int s) {
        queue<int> q;
        vi d(n, -1);
        q.push(s), d[s] = 0;
        while(q.size()) {
            int u = q.front(); q.pop();
            if(u < s) add(ans, d[u]);
            for(int v:G[u]) if(d[v] == -1) {
                d[v] = d[u] + 1;
                q.push(v);
            }
        } 
    };
    for(int i = 0; i < n; i++) {
        bfs(i);
    }
    return ans;
}
int convex(int n, int* x, int *y) {
    map<int, int> mp;
    int mnx = INF, mxx = -INF;
    for(int i = 0; i < n; i++) {
        mnx = min(mnx, x[i]);
        mxx = max(mxx, x[i]);
        mp[x[i]]++;
    }
    int ans = 0, cnt = 0, sum = 0;
    for(int i = mnx; i <= mxx; i++) {
        add(ans, 1LL * i * mp[i] % M * cnt % M);
        add(ans, M - 1LL * sum * mp[i] % M);
        add(cnt, mp[i]);
        add(sum, 1LL * mp[i] * i % M);
    }
    return ans;
}

int DistanceSum(int n, int *x, int *y) {
    if(n <= 2000) {
        return brute(n, x, y);
    } else {
        return (convex(n, x, y) + convex(n, y, x)) % M;
    }
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...