Editorial
First, find one representative fan for each team.
Consider querying the set of people from to . If the return value increases compared to the previous prefix, then person supports a team that has not appeared before. We record person as a representative of that team.
After this process, we have exactly one representative for every team, and all representatives support different teams.
Now consider an unclassified person . Take a subset of the representatives and query together with . Since all representatives support distinct teams, the number of distinct teams among the representatives in is exactly .
- If the return value is , then contains the representative of 's team.
- If the return value is , then does not contain the representative of 's team.
Therefore, we can binary search over the representatives to determine which representative supports the same team as .
Finding the representatives uses queries, and classifying each person uses queries. Since , the total number of queries is at most .