min-xor
Time limit0.4sMemory limit8 MB
Maintain a dynamic set under insertions and deletions, and after each min-xor query report the smallest XOR of any two elements currently in the set.
- Level
Hard8 of 10
- Topics
- Trie, Bit manipulation, Tree, Dynamic programming
- Solved
- No attempts yet
Problem
Consider two integers a and b. Their bitwise xor (exclusive or) is computed as follows:
- Write each of them in binary: a=ak-1ak-2...a0 and b = bk-1bk-2...b0;
- Their bitwise xor, denoted by a ^ b, has a bit equal to 1 in position p if and only if exactly one of ap and bp has a value of 1.
For example, 9 ^ 3 = 10 because 9 = 1001(2), 3 = 0011(2) and 10 = 1010(2).
You are given a sequence of N operations on a set of integers. The set is initially empty. Each operation is one of the following:
- insert(x): put integer x into the set;
- delete(x): remove integer x from the set;
- min-xor(): print the smallest bitwise xor between any two integers currently in the set.
You must print the output of every min-xor() operation.
Input
The first line contains the number of operations N. Each of the following N lines describes one operation using the following syntax:
- 1 x for insert(x)
- 2 x for delete(x)
- 3 for min-xor()
Output
The output must contain as many lines as there are min-xor() operations. Each line must contain a single number, the answer to the corresponding min-xor() operation.
Constraints
- 1 ≤ N ≤ 100,000;
- 1 ≤ x ≤ 1,000,000,000 for all insert(x) and delete(x) operations;
- For any insert(x) operation it is guaranteed that x does not exist in the set;
- For any delete(x) operation it is guaranteed that x exists in the set;
- For any min-xor() operation it is guaranteed that the set has at least 2 elements;
Hint
For the first min-xor(), the set is {24, 17}. The minimum bitwise xor is 24 ^ 17 = 9.
For the second min-xor(), the set is {24, 17, 23, 30}. The minimum bitwise xor is 24 ^ 30 = 6 (also 17 ^ 23 = 6).
For the third min-xor(), the set is {24, 23, 30}. The minimum bitwise xor is 24 ^ 30 = 6.
For the last min-xor(), the set is {24, 23}. The minimum bitwise xor is 24 ^ 23 = 15.