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