답안 #411787

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
411787 2021-05-26T01:12:31 Z couplefire ČVENK (COI15_cvenk) C++17
60 / 100
3000 ms 314804 KB
#include <bits/stdc++.h>
using namespace std;
#define point pair<int, int>
#define x first
#define y second

const int N = 100005;

int lowbit(int x){return x&(-x);}

point fa(point v){
    if(v.y == 0 || (v.x != 0 && lowbit(v.x) < lowbit(v.y))) return {v.x^lowbit(v.x), v.y};
    return {v.x, v.y^lowbit(v.y)};
}

bool isPar(point u, point v){
    if(u.x > v.x || u.y > v.y) return 0;
    int pos = 30;
    while(pos >= 0 && ((u.x>>pos)&1) == ((v.x>>pos)&1) && ((u.y>>pos)&1) == ((v.y>>pos)&1)) pos--;
    if(pos<0) return 1;
    if((v.x>>pos)&1) return !(u.y&((1<<pos)-1));
    return !(u.x&((1<<pos)-1));
}

point LCA(point u, point v){
    point pu = u, pv = v;
    while(!isPar(pu, v)) pu = fa(pu);
    while(!isPar(pv, u)) pv = fa(pv);
    if(isPar(pu, pv)) return pv;
    return pu;
}

int n;
map<int, vector<int>> hori, vert;
point points[N];
map<point, int> mp;
point lol[30*N];
vector<int> adj[30*N];
int siz[30*N];
int curid = 0;
long long ans;

int add(point a){
    if(mp.count(a)) return mp[a];
    lol[curid] = a;
    return mp[a] = curid++;
}

void ae(int u, int v){
    adj[u].push_back(v);
    adj[v].push_back(u);
}

void dfs(int v, int p){
    for(auto u : adj[v]){
        if(u == p) continue;
        dfs(u, v);
        siz[v] += siz[u];
    }
}

int centroid(int v){
    dfs(v, -1);
    int num = n, p = -1;
    do{
        int nxt = -1;
        for(auto u : adj[v])
            if(u != p && siz[u]*2 > num)
                nxt = u;
        p = v, v = nxt;
    } while(~v);
    return p;
}

int dist(point a, point b){
    return abs(a.x-b.x)+abs(a.y-b.y);
}

int main(){
    // freopen("a.in", "r", stdin);
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cin >> n;
    for(int i = 0; i<n; i++){
        int a, b; cin >> a >> b;
        points[i] = {a, b};
    }
    hori[0].push_back(0);
    vert[0].push_back(0);
    for(int i = 0; i<n; i++){
        point v = points[i];
        while(v != point{0, 0}){
            hori[v.y].push_back(v.x);
            vert[v.x].push_back(v.y);
            v = fa(v);
        }
    }
    int _c = 0;
    for(auto &l : hori){
        sort(l.second.begin(), l.second.end());
        l.second.erase(unique(l.second.begin(), l.second.end()), l.second.end());
        for(int i = 1; i<(int)l.second.size(); i++){
            point u = point{l.second[i-1], l.first}, v = point{l.second[i], l.first};
            if(!isPar(u, v)) continue;
            ae(add(u), add(v));
            _c++;
        }
    }
    for(auto &l : vert){
        sort(l.second.begin(), l.second.end());
        l.second.erase(unique(l.second.begin(), l.second.end()), l.second.end());
        for(int i = 1; i<(int)l.second.size(); i++){
            point u = point{l.first, l.second[i-1]}, v = point{l.first, l.second[i]};
            if(!isPar(u, v)) continue;
            ae(add(u), add(v));
            _c++;
        }
    }
    // cout << curid << " " << _c << endl;
    for(int i = 0; i<n; i++)
        siz[add(points[i])]++;
    point cent = lol[centroid(0)];
    for(int i = 0; i<n; i++){
        point lca = LCA(cent, points[i]);
        ans += 0ll+dist(cent, lca)+dist(points[i], lca);
    }
    cout << ans << endl;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 40 ms 70732 KB Output is correct
2 Correct 41 ms 70752 KB Output is correct
3 Correct 40 ms 70780 KB Output is correct
4 Correct 39 ms 70756 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 40 ms 71052 KB Output is correct
2 Correct 40 ms 71076 KB Output is correct
3 Correct 41 ms 70996 KB Output is correct
4 Correct 44 ms 71028 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 185 ms 77868 KB Output is correct
2 Correct 185 ms 77928 KB Output is correct
3 Correct 110 ms 75444 KB Output is correct
4 Correct 108 ms 75624 KB Output is correct
5 Correct 126 ms 75628 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 3025 ms 314804 KB Time limit exceeded
2 Halted 0 ms 0 KB -