XOR Queries
Practice
5 (2 votes)
Lazy propagation in interval/segment trees
Data structures
Bit manipulation
Advanced data structures
Problem
52% Success 432 Attempts 50 Points 2.5s Time Limit 256MB Memory 1024 KB Max Code
You are given an array \(A\), consisting of \(N\) integers \(A_1, A_2, ..., A_N\).
You will be asked queries of following \(3\) types:
- \(1 \space L \space R \space X :\) update all the elements in the range \(L\) to \(R\) to \(X\), i.e. do assign operation \(A_i = X\) , \(L \le i \le R\)
- \(2 \space L \space R \space X :\) change all the elements in the range \(L\) to \(R\) to \(A_i \newcommand*\xor{\oplus} X\), i.e. do Bitwise XOR operation \(A_i = A_i \newcommand*\xor{\oplus} X\), \(L \le i \le R\) (where \(\newcommand*\xor{\oplus}\) denotes the Bitwise XOR operator).
- \(3 \space L \space R :\) output the sum of range \(L\) to \(R\), i.e. \(\sum_{i = L}^R{A_i} = A_L + A_{L + 1} + .... + A_R \)
Note: Since the size of the input and output is large, please use fast I/O and memory accordingly.
Input format
- The first line of input contains an integer \(N\).
- The second line of input contains \(N\) space-separated integers \(A_1, A_2, ..., A_N\).
- The third line of input contains an integer \(Q\).
- Next \(Q\) lines first contains an integer \(T_i\) which denotes the type of the query. If \(T_i = 1 \space or \space 2\) then next followed three space-separated integers \( L \space R \space X\) otherwise if \(T_i = 3\) then next followed two space-separated integers \(L \space R\).
It is guaranteed that there will be at least one query of type \(3\).
Output format
For each query of type \(3\), print a single integer denoting the answer to that query.
Constraints
\(1 \le N, Q \le 2 * 10^5\)
\(0 \le A_i, X< 2^{10}\)
\(1 \le T_i \le 3\)
\(1 \le L \le R \le N\)
Explanation
Initially, we have the array, \(A = [1, 2, 3, 4, 5]\).
- For the first query, we will do the operation of type \(1\) (assigning operation) in range \([2,3]\), so the array will be \(A = [1, 3, 3, 4, 5]\).
- For the second query, we will do the operation of type \(2\) (xor update operation) in range \([3, 3]\), so the array will be \(A = [1, 3, 3 \newcommand*\xor{\oplus} 2, 4, 5] = [1, 3, 1, 4, 5]\).
- For the third query, we will output the sum in the range \([2, 5]\), that is \(3 + 1 + 4 + 5 = 13\).
- For the fourth query, we will do the operation of type \(1\) (assigning operation) in range \([1, 2]\), so the array will be \(A = [5, 5, 1, 4, 5]\).
- For the fifth query, we will output the sum in the range \([1, 3]\), that is \(5 + 5 + 1 = 11\).
Code Editor
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
Submissions
Please login to view your submissions
Similar Problems
Points:50
1 votes
Tags:
Data StructuresHardSegment treeData Structures
Points:50
3 votes
Tags:
Advanced Data StructuresData StructuresString AlgorithmsLazy Propagation in Interval/Segment TreesHashing algorithm
Points:50
8 votes
Tags:
StringLazy Propagation in Interval/Segment TreesData StructuresAdvanced Data Structures
Editorial
Login to unlock the editorial
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