# Codechef PREFIXOR: Prefix XOR

This is a problem from the Codechef SnackDown Elimination Round 2017. During the contest, I read the statements, and immediately started coding a plain segment tree, thinking it would work, only to find that I couldn’t merge the left and right segments at around 10 LOC or so. Anyway, a few days ago, I decided to give the problem another try.

My first observation is that, for each `l`

, if we can find the maximum `r`

so that the pair `(l, r)`

is permissible (i.e. `a[l] <= a[l] ^ a[l + 1] <= ... <= a[l] ^ a[l + 1] ^ ... a[r]`

), then `(l, k)`

is also permissible iff `l <= k <= r`

. As such, let the maximum `r`

for each `l`

be `maxDst[l]`

, then the answer for the segment `(l, r)`

is `sum { min(maxDst[k], r) - k + 1 | l <= k <= r }`

. This query can be done `O(log n)`

on a segment tree with a merge-split treap in each node by splitting each treap by `maxDst[k] == r`

, and adding up `sum { maxDst[k] - k + 1 }`

and `sum { r - k + 1 }`

for the left and right side respectively. However, since no updates are needed, we can just use a merge-sort tree, with prefix sums for `maxDst[k] - k + 1`

and suffix sums for `- k + 1`

in each node. (The latter is summed with `r * [NUMBER OF ELEMENTS]`

during the query.)

The problem is then transformed into solving the `maxDst`

array. We can see that when `k == 1`

, we have to calculate the relations of `a[1], a[1] ^ a[2], ... , a[1] ^ a[2] ^ ... a[n]`

, and when `k == 2`

, the sequence turns into `a[2], a[2] ^ a[3], ...`

. Note that we can just drop the first element from the former and xor all the elements with `a[1]`

to get the latter. In other words, we have to track the relations while supporting modifications that xor all the elements with a certain integer.

Looking into how xor works, we can observe that `x <= y`

becomes `x ^ k > y ^ k`

iff the most significant bit that differs between `x`

and `y`

is xored with `1`

. Thus, we can categorize the relations into 32 sets, and turn this into a modal where we have a sequence of 0s and 1s, standing for `<=`

and `>`

respectively. For each xor operation, we flip some certain sets of elements according to the binary representation of the number so that we can track the relations. Then, if we can query the sum of a segment, we can know whether the sequence is increasing by checking whether the sum is zero, thus solving `maxDst`

via binary search.

In order to do the operations mentioned above, the first thing that came into mind are segment trees. However, since the modifications are non-continuous, we have to use multiple segment trees, one for each set, querying and modifying them separately. Unfortunately, this method is too slow and gets TLEs. On further inspection, it can be observed that the only modifications are the flipping, which are only done on the root. In other words, none of the nodes, except the roots, are modified. As such, we can turn to prefix sums and maintain a boolean array on whether a set is flipped. If the set is flipped, the query returns `N - ans`

, where `ans`

is the original return value, and `N`

is the total number of elements in the segment, which can be determined by another prefix sum array.

To recap, the procedure is as follows:

- Create an array of
`a[1], a[1] ^ a[2], ... a[1] ^ a[2] ^ ... a[n]`

and record the relations between every pair`(a[i], a[i + 1])`

. (`O(n)`

) - Place each relation into two prefix sums (one for tracking the
`>`

count, and the other for tracking the number of elements) according to their binary representation (one of the 32 sets). (`O(n)`

) - Scan from left to right. For each
`l`

, binary search the minimum`r`

so that the segment`(l, r)`

has a sum > 0 on the prefix sum (querying the 32 sets separately).`maxDst[l]`

can then be updated to`r`

. Then, do the modification by xorring with`a[l]`

, i.e. flip the sets whose corresponding bit in`a[l]`

is 1. (`O(n * log n * log C)`

) - Build a merge-sort tree. In each node, maintain not only
`maxDst[]`

but also the prefix sums of`maxDst[k] - k + 1`

and suffix sums of`-k + 1`

. (`O(n * log n)`

) - For each query
`(l, r)`

, search the segment on the merge-sort tree. For each node hit, binary search`maxDst`

for`r`

, and sum up the prefix sums left of`r`

and suffix sums right of`r`

together with`r * [NUMBER OF ELEMENTS ON THE RIGHT]`

. (`O(q * log n)`

, since for each query, the number of binary searches are bounded by a constant.)

Total time complexity: `O(n * log n * log C + q * log n)`

Code is as follows:

## Post a comment

Send an email to comment@nerde.pw with

`/2017/07/04/codechef-prefixor`

as the title. More info