Range queries

4.6

7 votes
Advanced Data Structures, Data Structures, Fenwick (Binary Indexed) Trees, C++
Problem

You are given a permutation of N numbers that are denoted by array A.

You are also given Q queries of the form:

  • L R: For all the subsequences from the subarray A[L], A[L+1], ..., A[R] such that the length of subsequence is equal to the maximum element present in the subsequence, find the bitwise XOR of all the elements present in these subsequences.  

Find the answer for Q queries.

Note: Assume 1 based indexing.

Input format

  • The first line contains a single integer T that denotes the number of test cases. 
  • For each test case:
    • The first line contains an integer N.
    • The second line contains an integer Q.
    • The third line contains N space-separated integers denoting the array A.
    • Next Q lines contains the queries.

Output format

For each test case, print Q space-separated integers denoting the answer for queries in a new line.

Constraints

1T101N,Q1051LRNA is a permutation of numbers from 1 to N

Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation
  • For query 1 L = 1, R = 5 so the subarray is [2, 3, 1, 5, 4]
    • Following subsequences satisfy the condition, {2, 1}, {2, 3, 1}, {1}, {2, 3, 1, 5, 4}, {2, 3, 1, 4}.
    • So, the required answer is 2^1^2^3^1^1^2^3^1^5^4^2^3^1^4 = 7
  • For query 2, L = 2, R = 4 so the subarray is [3, 1, 5]
    • Following subsequence satisfy the condition, {1}.
    • So, the required answer is 1.
  • Thus, required answer is [7, 1].
Editor Image

?