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 int long long
int n,ans = 0;
pair < int , int > par(pair < int, int > a){
if((a.first & -a.first) < (a.second & -a.second) and (a.first & -a.first) != 0){
ans += a.first & -a.first;
a.first -= a.first & -a.first;
}
else if((a.second & -a.second) != 0){
ans += a.second & -a.second;
a.second -= a.second & -a.second;
}
else {
ans += a.first & -a.first;
a.first -= a.first & -a.first;
}
return a;
}
int dist(pair < int , int > a , pair < int , int > b){
int ret = 0;
int cnta = __builtin_popcountll(a.first) + __builtin_popcountll(a.second);
int cntb = __builtin_popcountll(b.first) + __builtin_popcountll(b.second);
if(cnta < cntb)swap(a,b);
int diff = cntb - cnta;
while(diff--){
pair < int , int > newa = par(a);
ret += a.first - newa.first + a.second - newa.second;
a = newa;
}
while(a != b){
pair < int , int > newa = par(a);
pair < int , int > newb = par(b);
if(par(a) == par(b)){
if(a.first == b.first){
ret -= min(a.second,b.second) - newa.second + 1;
}
else if(a.second == b.second){
ret -= min(a.first,b.first) - newa.first + 1;
}
}
ret += a.first - newa.first + a.second - newa.second;
ret += b.first - newb.first + b.second - newb.second;
a = newa;
b = newb;
}
return ret;
}
void solve(){
int n;cin >> n;
pair < int , int > a,b;
cin >> a.first >> a.second;
cin >> b.first >> b.second;
cout << dist(a,b) << endl;
cout<<endl<<fixed<<setprecision(2)<<1000.0 * clock() / CLOCKS_PER_SEC<< " milliseconds ";
}
signed main(){
ios_base::sync_with_stdio(0);cin.tie(0);
int testcase = 1;//cin >> testcase;
while(testcase--)solve();
}
# | 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... |