Utility function to find LCA
def LCA ( qlow : int , qhigh : int , low : int , high : int , pos : int ) -> list : NEW_LINE INDENT if qlow <= low and qhigh >= high : NEW_LINE INDENT return seg [ pos ] NEW_LINE DEDENT if qlow > high or qhigh < low : NEW_LINE INDENT return [ INT_MAX , 0 ] NEW_LINE DEDENT mid = low + ( high - low ) // 2 NEW_LINE return minPair ( LCA ( qlow , qhigh , low , mid , 2 * pos ) , LCA ( qlow , qhigh , mid + 1 , high , 2 * pos + 1 ) ) NEW_LINE DEDENT