Official

G - Minimum Xor Pair Query Editorial by en_translator


XOR (exclusive OR) has the following property:

  • \(\mathrm{min}(x\oplus y, y\oplus z) < x\oplus z\) for all non-negative integers \(x,y,z\ (x<y<z)\).

Proof: In binary representations of \(x\) and \(z\), suppose that the \((k+1)\)-th-or-greater least significant bits are the same, and the \(k\)-th least ones differ (\(0\) for \(x\) and \(1\) for \(z\), since \(x<z\)); then \(x\oplus z \geq 2^k\).

Regarding \(y\), its \((k+1)\)-th-or-greater least significant bits coincide with those of \(x\) and \(z\). Here, if \(k\)-th significant bit of \(y\) is \(0\), then \(x\oplus y < 2^k\); if it is \(1\), then \(y\oplus z < 2^k\). Thus, \(\mathrm{min}(x\oplus y, y\oplus z) < x\oplus z\) is shown. (End of proof)

By this property, the answer can only be one of XORs of adjacent terms in the sequence of the integers written on the blackboard, sorted in ascending order.

One can track the XOR of adjacent values by the additions and deletions of values on the blackboard with an ordered set like std::set.

Specifically, when adding a value, add the XORs with the previous and next values to the candidates; on a deletion, remove such values from the candidates, and add the XOR of two values that are newly adjacent.

posted:
last update: