Sponsored
Sponsored
Use these hints if you're stuck. Try solving on your own first.
The queries must be answered efficiently to avoid time limit exceeded verdict.
Use sparse table (dynamic programming application) to travel the tree upwards in a fast way.