Shortest Path in Binary Trees
All submissions for this problem are available.
Read problems statements in Mandarin Chinese and Russian.
Consider an infinite full binary tree (each node has two children except the leaf nodes) defined as follows. For a node labelled v its left child will be labelled 2*v and its right child will be labelled 2*v+1. The root is labelled as 1.
You are given N queries of the form i j. For each query, you have to print the length of the shortest path between node labelled i and node labelled j.
First line contains N, the number of queries. Each query consists of two space separated integers i and j in one line.
For each query, print the required answer in one line.
- 1 ≤ N ≤ 105
- 1 ≤ i,j ≤ 109
Input: 3 1 2 2 3 4 3 Output: 1 2 3
For first query, 1 is directly connected to 2 by an edge. Hence distance 1.
|Tags||april14 darkshadows easy tree|
|Time Limit:||1 sec|
|Source Limit:||50000 Bytes|
|Languages:||ADA, ASM, BASH, BF, C, C99 strict, CAML, CLOJ, CLPS, CPP 4.3.2, CPP 4.9.2, CPP14, CS2, D, ERL, FORT, FS, GO, HASK, ICK, ICON, JAVA, JS, LISP clisp, LISP sbcl, LUA, NEM, NICE, NODEJS, PAS fpc, PAS gpc, PERL, PERL6, PHP, PIKE, PRLG, PYTH, PYTH 3.4, RUBY, SCALA, SCM guile, SCM qobi, ST, TCL, TEXT, WSPC|
Fetching successful submissions
If you are still having problems, see a sample solution here.