![]() |
VOOZH | about |
Given a list s that initially contains a single value 0. Below are the q queries of the following types:
The task is to print all the element in the list in increasing order after performing the given Q queries.
Examples:
Input: queries[][] = [{0, 6}, {0, 3}, {0, 2}, {1, 4}, {1, 5} ]
Output: 1 2 3 7
Explanation: [0] (initial value) , [0 6] (add 6 to list), [0 6 3] (add 3 to list), [0 6 3 2] (add 2 to list), [4 2 7 6] (XOR each element by 4), [1 7 2 3] (XOR each element by 5), Thus sorted order after performing queries is [1 2 3 7]Input: queries[][]= [{0, 2}, {1, 3}, {0, 5} ]
Output: 1 3 5
Explanation: [0] (initial value), [0 2] (add 2 to list). [3 1] (XOR each element by 3), [3 1 5] (add 5 to list), Thus sorted order after performing queries is [1 3 5]
Table of Content
The idea is to sequentially process each query exactly as described.
1 2 3 7
The idea is to process the queries from right to left because type-1 queries (XOR updates) affect all previously inserted elements. Instead of updating every element repeatedly, we maintain a cumulative XOR value. While traversing backward, whenever we encounter an insertion query, we apply the current cumulative XOR to it and store the result.
1 2 3 7