A ground station keeps a pool of radio frequencies (non-negative integers; the same frequency may be
in the pool several times). When a new transmitter with frequency x asks for a slot, the station
ranks the pool by the clash value x XOR y of every stored copy y. Design FrequencyPool:
FrequencyPool()starts with an empty pool.add(x)puts one more copy ofxinto the pool (returnsNone).remove(x)takes one copy ofxout and returnsTrue, or returnsFalseifxis not in the pool.kth_clash(x, k)lists the clash valuesx ^ yof all copies in the pool, sorted ascending (duplicates kept), and returns thek-th one (kstarts at 1). Return-1if the pool holds fewer thankcopies. The pool does not change.
Examples
ops: ["FrequencyPool", "add", "add", "add", "add", "kth_clash", "kth_clash", "kth_clash", "remove", "remove", "kth_clash", "kth_clash"]
args: [[], [5], [1], [5], [12], [4, 1], [4, 3], [4, 4], [5], [7], [4, 3], [4, 1]]
Output: [None, None, None, None, None, 1, 5, 8, True, False, 8, 1]
Explanation: against 4 the clash values are 5^4 = 1, 1^4 = 5, 5^4 = 1, 12^4 = 8,
sorted [1, 1, 5, 8]. After removing one 5 they are [1, 5, 8].
Constraints
0 <= x < 2**20,k >= 1- Up to
2 * 10**4calls in total - Target: every call in about
O(20)steps; sorting the clash values for eachkth_clashis too slow
Goals
- Keep a multiset of integers in a binary trie with a count on every node
- Find the k-th smallest XOR against a query by choosing a child from counts at each bit
- Support removal and report whether the value was present