#P5105. Xiyue's Multiset XOR Query
Xiyue's Multiset XOR Query
Xiyue's Multiset XOR Query
In this problem, you are given an initially empty multiset S that allows duplicate elements. You need to support the following two types of operations:
- Insertion Operation: Given two integers L and R, insert all the integers in the inclusive range [L, R] into S (i.e. perform of insertion of L, L+1, ..., R).
- Query Operation: Compute Sort(S), where first S is sorted in ascending order (using quicksort, though any correct sort is acceptable) and written as a1, a2, ... , an. Then, the answer is defined as \[ Sort(S) = \bigoplus_{i=2}^{n}\Bigl(a_i^2 - a_{i-1}^2\Bigr), \] where \(\bigoplus\) denotes the bitwise XOR operation. If the multiset contains less than two elements, the answer is 0.
Note: Although S is a multiset (allowing duplicates), the computed answer only depends on the distinct values present, because repeated adjacent equal values contribute a 0 difference.
You are required to implement a program that processes several operations and produces the correct answer for each query.
inputFormat
The first line contains a single integer Q (the number of operations).
The next Q lines each represent an operation. Each operation is of one of the following two types:
1 L R
: An insertion operation that inserts all integers from L to R (inclusive) into the multiset S.2
: A query operation. When this command is given, you should compute and output the value of Sort(S) as described above.
It is guaranteed that all numbers are integers. You can assume that the operations are such that a solution using simple methods will pass the given test cases.
outputFormat
For each query operation (i.e. operations of type 2), output a single line containing the computed result.
sample
6
1 1 3
2
1 2 4
2
2
6
1
1
</p>