CF706D Vasiliy’s Multiset
Solution Sketch
We know that $ 1 \oplus 0 = 0 \oplus 1 = 1$ and $ 0 \oplus 0 = 1 \oplus 1 = 0$ ($\oplus$ stands for XOR). Thus, in order to find the maximum XOR value of two numbers, we need to choose a number such that it has as many opposite bits as possible regarding $x$.
How can we do it efficiently? Binary trie is here to save us!
We can view every number in their binary representation form.
- If the query asked us to insert a number, we simply add the number bit by bit: if the current bit is 0, we move to the left subtree. Otherwise, move to the right one. Notice: We will pad zeros in front of some numbers in order to make every number of the same length. This will make querying and removing easy.
- If the query asked for the maximum XOR value regarding a specific $x$, simply traverse the binary trie (look for the solution greedily). If the current bit of $x$ is 1, then see if the trie has a left subtree (0). If yes, then move to that subtree. Otherwise, move to the other one (1).123456root/ \0 / \ 1/ \left rightsubtree subtree
AC Code
|
|