This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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));
}
int n;
map<int, vector<int>> hori, vert;
point points[N], lol[30*N];
map<point, int> mp;
vector<int> adj[30*N];
int siz[30*N], curid;
int dist(point a, point b){
return abs(a.x-b.x)+abs(a.y-b.y);
}
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];
}
}
long long dfs2(int v, int p){
long long ans = 0;
for(auto u : adj[v]){
if(u == p) continue;
ans += dfs2(u, v)+1ll*siz[u]*dist(lol[u], lol[v]);
}
return ans;
}
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 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));
}
}
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));
}
}
for(int i = 0; i<n; i++)
siz[add(points[i])]++;
int cent = centroid(0);
memset(siz, 0, sizeof siz);
for(int i = 0; i<n; i++)
siz[add(points[i])]++;
dfs(cent, -1);
cout << dfs2(cent, -1) << endl;
}
Compilation message (stderr)
cvenk.cpp: In function 'int main()':
cvenk.cpp:96:9: warning: unused variable '_c' [-Wunused-variable]
96 | int _c = 0;
| ^~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |