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>
typedef int ll;
#define FOR(i,x,y) for(ll i=x; i<y; i++)
#define FORNEG(i,x,y) for(ll i=x; i>y; i--)
using namespace std;
vector<ll> lca(vector<ll> a, vector<ll> b){
if (a[0]+a[1] < b[0] + b[1]) swap(a,b);
map<vector<ll>, vector<ll>> prev;
set<vector<ll>> stuff;
stuff.insert(a);
prev[a] = {-1,-1};
FOR(i,0,30){
if ((1<<i) & a[0]){
vector<ll> old = a;
a[0] -= (1<<i);
stuff.insert(a);
prev[a] = old;
}
if ((1<<i) & a[1]){
vector<ll> old = a;
a[1] -= (1<<i);
stuff.insert(a);
prev[a] = old;
}
}
FOR(i,0,30){
if ((1<<i) & b[0]){
vector<ll> old = b;
b[0] -= (1<<i);
if (stuff.count(b)){
if ((b[0] == old[0] && old[0] == prev[b][0]) || (b[1] == old[1] && old[1] == prev[b][1])) return old;
return b;
}
}
if ((1<<i) & b[1]){
vector<ll> old = b;
b[1] -= (1<<i);
if (stuff.count(b)){
if ((b[0] == old[0] && old[0] == prev[b][0]) || (b[1] == old[1] && old[1] == prev[b][1])) return old;
return b;
}
}
}
return {};
}
int main(){
ll n;
cin >> n;
vector<ll> A(2);
vector<ll> B(2);
cin >> A[0] >> A[1] >> B[0] >> B[1];
vector<ll> sus = lca(A,B);
cout << A[0]+A[1]+B[0]+B[1]-sus[0]*2-sus[1]*2;
}
# | 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... |