PROBLEM LINK:
Author: Yurii Rebryk
Tester: Kevin Atienza
Editorialists: Pushkar Mishra and Suhash Venkatesh
DIFFICULTY:
Medium
PREREQUISITES:
Tries, Properties of Bitwise XOR
PROBLEM:
Given is an array A containing numbers in the range 0 to 10^9. We have to find l_1, r_1, l_2 and r_2 (l_1 \leq r_1 < l_2 \leq r_2) such that (\,\,A[l_1] ⊕ A[l_1 + 1] ⊕ \dots ⊕ A[r_1]\,\,) + (\,\,A[l_2] ⊕ A[l_2 + 1] ⊕ \dots ⊕ A[r_2]\,\,) is maximised. Here x ⊕ y refers to Bitwise XOR of x and y.
EXPLANATION:
Subtask 1
Let’s start off by thinking of the simplest algorithm that we can. We we will then optimise it step by step by making certain observations.
So, the simplest thing to do is to implement what the problem says. We can run four loops, one each for l_1, r_1, l_2 and r_2. This gives us an \mathcal{O}(N^4) algorithm which will exceed the time limit for all the subtasks.
How can we optimise this? Let’s make an observation. If we know for each i = 1 to N, the maximum value we can get for (\,\,A[l_1] ⊕ A[l_1 + 1] ⊕ \dots ⊕ A[r_1]\,\,) and (\,\,A[l_2] ⊕ A[l_2 + 1] ⊕ \dots ⊕ A[r_2]\,\,) such that r_1 \leq i and i < l_2, then we can tell the answer in \mathcal{O}(N) by simply taking the maximum of the sum of the two terms over all i.
What remains now is to calculate efficiently for each i the max (\,\,A[l_1] ⊕ A[l_1 + 1] ⊕ \dots ⊕ A[r_1]\,\,) and (\,\,A[l_2] ⊕ A[l_2 + 1] ⊕ \dots ⊕ A[r_2]\,\,) such that r_1 \leq i and i < l_2. Let’s call the two terms lbest[i] and rbest[i] respectively. For calculating lbest[i], we iterate from j = i to 1 and find the j such that val = (\,\,A[j] ⊕ A[j + 1] ⊕ \dots ⊕ A[i]\,\,) is maximised. Then, lbest[i] = max(\,lbest[i-1],\, val\,)\,\,. Similarly, we can fill the array rbest.
Calculating the arrays lbest and rbest require \mathcal{O}(N^2) time. After that, the answer can be computed in \mathcal{O}(N). For this subtask, an \mathcal{O}(N^2) solution will pass.
Subtask 2
We need to somehow speed up the calculation of arrays lbest and rbest. Up till now, we haven’t utilised any property of XOR operation in our solution. Let’s try to optimise our algorithm using the following property:
Let cumul[i] = (\,\,A[1] ⊕ A[2] ⊕ \dots ⊕ A[i]\,\,).
Then for some j\leq i, cumul[j-1] ⊕ cumul[i]\, = (\,\,A[j] ⊕ A[j + 1] ⊕ \dots ⊕ A[i]\,\,).
The proof behind this is left to the reader as a simple exercise.
We can now say that lbest[i] = max(\,lbest[i-1],\, val\,)\,\, where val = maximum of (cumul[j-1] ⊕ cumul[i]\,\,) for j = 1 to i.
Calculating lbest this way still takes \mathcal{O}(N^2). For i = 1 to N, we need to somehow optimise finding the index j such that (cumul[j-1] ⊕ cumul[i]\,\,) is maximum. If we can do it in \mathcal{O}(\log A[i])\,, then we can compute the array lbest (and analogously, rbest too) in \mathcal{O}(N\log A_{max})\,.
We can use Tries to get the required complexity. We will keep a trie which holds the binary representation of elements in the array cumul. While processing for i from 1 to N, we will first use the trie to get the value which is maximum of (cumul[j-1] ⊕ cumul[i]\,\,) for 1 \leq j \leq i, and then insert cumul[i] into the trie. This will allow us to calculate lbest in the required complexity. Similarly, we can calculate rbest also.
Inserting into a trie is a standard operation. Let us look at how we do the other operation, i.e., for a value x, finding the value y in the trie such that x ⊕ y is maximised. Since, the trie stores binary representations of numbers, we first convert x to its binary representation. Then, we can go down the trie and for each bit in the representation of x, we try to find whether bit of opposite value exists or not. It is easy to see why we seek opposite; because 1 ⊕ 1 and 0 ⊕ 0 = 0 while 0 ⊕ 1 = 1.
COMPLEXITY:
\mathcal{O}(N\log A_{max})