Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- int lca(int u,int v)
- {
- int sol=0;
- while(u!=v)
- {
- if(v<u)
- swap(u,v);
- v>>=1;
- ++sol;
- }
- return sol;
- }
- int main()
- {
- ios_base::sync_with_stdio(false);
- cin.tie(nullptr);
- cout.tie(nullptr);
- int q,u,v;
- cin>>q;
- while(q--)
- cin>>u>>v,cout<<lca(u,v)<<'\n';
- return 0;
- }
Add Comment
Please, Sign In to add comment