Sorting Illusion
Time limit2sMemory limit512 MB
Find the smallest b making a[i] xor b nondecreasing, updating the answer after each of q point changes to the array.
- Level
Hard8 of 10
- Topics
- Bit manipulation, Divide and conquer, Segment tree, Sorting
- Solved
- No attempts yet
Problem
A well-known programmer tries his hand at being a magician. His signature trick works as follows.
Given an array of non-negative integers , he quickly finds a magic number . A non-negative integer is called magic for the array if applying the bitwise exclusive OR operation with this number to every element of the array turns the array into a sorted array. In other words, [ (a_1 \oplus b) \leq (a_2 \oplus b) \leq \ldots \leq (a_n \oplus b), ] where is the bitwise exclusive OR operation.
To make the trick more impressive, after presenting the magic number for the given array, the magician performs the following action times. He invites the audience to change one of the elements of the array and then tries to present a magic number again. The programmer has honed his magician's skill so well that each time he presents the audience with the smallest possible magic number. Sometimes the trick fails, because no magic number can be found for the resulting array.
Write a program that, for the given initial array and after each change of an element, computes the minimum magic number for the resulting array, or determines that no such number exists.
Input
The first line of the input contains an integer , the number of numbers in the array ().
The second line contains integers , the elements of the array ().
The third line contains an integer , the number of changes to an element of the array ().
The following lines each contain two integers and , where is the index of the array element to replace () and is the new value of that element ().
Output
The output must contain integers , one per line.
The value is either the minimum possible magic number for the initial array or if no such number exists.
For from 1 to , the value is either the minimum possible magic number for the array after the first changes or if no such number exists.
Note
Exclusive OR is a logical operation denoted by , defined by the following truth table:
Let us define the bitwise exclusive OR of two non-negative integers and . Write each of the integers and in binary, padding the shorter number with leading zeros if necessary so that they have equal length. The bitwise exclusive OR of two integers and , also denoted , is the non-negative integer whose binary representation has, in each digit, the exclusive OR of the corresponding digits of and . For example, .
Among the programming languages offered at the olympiad, Pascal uses the <<xor>> operator to denote exclusive OR, and the remaining programming languages use the <<^>> operator.