Submission #411787

#TimeUsernameProblemLanguageResultExecution timeMemory
411787couplefireČVENK (COI15_cvenk)C++17
60 / 100
3025 ms314804 KiB
#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; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...