Submission #288214

#TimeUsernameProblemLanguageResultExecution timeMemory
288214ToMmyDongIdeal city (IOI12_city)C++11
55 / 100
157 ms5868 KiB
#include <iostream>
#include <queue>
#include <map>
#include <algorithm>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
#define ALL(i) i.begin(), i.end()
#define SZ(i) int(i.size())
#define X first
#define Y second
#ifdef tmd
#define debug(...) fprintf(stderr,"#%d-(%s)=",__LINE__,#__VA_ARGS__);_do(__VA_ARGS__);
template<typename T> void _do(T &&x){cerr<<x<<endl;}
template<typename T, typename ...S> void _do(T &&x, S &&...y) {cerr<<x<<",";_do(y...);}
template<typename IT> ostream& __printRng (ostream& os, IT bg, IT ed) {
    for (IT it=bg;it!=ed;it++) {
        if (it == bg) os << "{" << *it;
        else os << "," << *it;
    }
    return os << "}";
}
template<typename T> ostream& operator << (ostream& os, const vector<T> &vec) {
    return __printRng(os, ALL(vec));
}
template<typename T, typename S> ostream& operator << (ostream& os, const pair<T,S> &pa) {
    return os << "{" << pa.X << "," << pa.Y << "}";
}
#else
#define debug(...)
#endif

const int MAXN = 100083;

struct Bit {
    ll bit[MAXN];
    void add (int x, int val) {
        for (x+=2;x<MAXN;x+=-x&x) {
            bit[x] += val;
        }
    }
    ll pre (int x) {
        ll ret = 0;
        for (x+=2;x>0;x-=-x&x) {
            ret += bit[x];
        }
        return ret;
    }
    ll suf (int x) {
        return pre(MAXN-3) - pre(x-1);
    }
}bx, by, cx, cy;

int DistanceSum2(int n, int *x, int *y);
int DistanceSum(int n, int *x, int *y) {
    if (n <= 2000) return DistanceSum2(n,x,y);
    vector<int> vx, vy;
    for (int i=0; i<n; i++) {
        vx.emplace_back(x[i]);
        vy.emplace_back(y[i]);
    }
    sort(ALL(vx));
    sort(ALL(vy));
    vx.resize(unique(ALL(vx))-vx.begin());
    vy.resize(unique(ALL(vy))-vy.begin());
    
    ll ans = 0;
    for (int i=0; i<n; i++) {
        x[i] = lower_bound(ALL(vx), x[i])-vx.begin();
        y[i] = lower_bound(ALL(vy), y[i])-vy.begin();
        debug(x[i], y[i]);
        ans += x[i] * cx.pre(x[i]-1) - bx.pre(x[i]-1);
        ans += -x[i] * cx.suf(x[i]+1) + bx.suf(x[i]+1);
        ans += y[i] * cy.pre(y[i]-1) - by.pre(y[i]-1);
        ans += -y[i] * cy.suf(y[i]+1) + by.suf(y[i]+1);

        bx.add(x[i], x[i]);
        by.add(y[i], y[i]);
        cx.add(x[i], 1);
        cy.add(y[i], 1);
    }

    return ans % 1000000000; 
}


map<pii, int> id;
vector<int> edge[MAXN];
int DistanceSum2(int n, int *x, int *y) {
    for (int i=0; i<n; i++) {
        id[pii(x[i], y[i])] = i;
    }
 
    for (int i=0; i<n; i++) {
        vector<int> dx = {1,0,-1,0}, dy = {0,1,0,-1};
        for (int j=0; j<4; j++) {
            int nx = x[i] + dx[j];
            int ny = y[i] + dy[j];
            if (id.count(pii(nx, ny))) {
                int z = id[pii(nx,ny)];
                edge[i].emplace_back(z);
                edge[z].emplace_back(i);
            }
        }
    }
 
    int ans = 0;
    for (int i=0; i<n; i++) {
        vector<int> dis(n, -1);
        dis[i] = 0;
        queue<int> bfs;
        bfs.emplace(i);
        while (bfs.size()) {
            int cur = bfs.front();
            bfs.pop();
            for (int z : edge[cur]) {
                if (dis[z] == -1) {
                    dis[z] = dis[cur] + 1;
                    bfs.emplace(z);
                    ans += dis[z];
                }
            }
        }
    }
 
    return ans / 2;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...