Scan the shorter list
transcript
Each group keeps waiting queries. Queries wait inside each group. Two groups merge. Which list do you scan? Always the shorter one.
Scan the shorter list. Move its queries to the longer list. Then the longer list grows. Answer any query that now meets.
A moved query joins a list at least as long. Think of the lists as buckets. So its list at least doubles. That happens at most log q times.
4,096 groups merge in a chain. Scanning the shorter costs 4,095. That is nearly free. Scanning the longer costs 8,386,560.
You see all queries first. So you choose the road order. Strongest roads come first. Online code cannot do this.
A query is answered at a merge. The merge puts both ends together. Then the limit is the answer.
Merge small into large. Scan the shorter list. Small into large, every time. Answer when both ends meet. Full breakdown in the lecture.