Range queries
Practice
4.6 (7 votes)
Advanced data structures
Data structures
Fenwick (binary indexed) trees
C++
Problem
57% Success 484 Attempts 30 Points 1s Time Limit 256MB Memory 1024 KB Max Code

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

\(1 \le T \le 10 \\ 1 \le N, Q \le 10^5 \\ 1 \le L \le R \le N \\ \text{A is a permutation of numbers from 1 to N}\)

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:30
2 votes
Tags:
Advanced Data StructuresData StructuresDivide-and-conquer algorithmFenwick TreeFenwick treeMediumTwo pointer
Points:30
27 votes
Tags:
Advanced Data StructuresC++Data StructuresFenwick Tree
Points:30
3 votes
Tags:
Advanced Data StructuresData StructuresFenwick (Binary Indexed) TreesMediumFenwick TreeFenwick tree