XOR Queries

5

2 votes
Lazy Propagation in Interval/Segment Trees, Data Structures, Bit manipulation, Advanced Data Structures
Problem

You are given an array A, consisting of N integers A1,A2,...,AN.  

You will be asked queries of following 3 types:

  •  1 L R X: update all the elements in the range L to R to X, i.e. do assign operation Ai=X ,  LiR
  •  2 L R X: change all the elements in the range L to R to AiX, i.e. do Bitwise XOR operation Ai=AiXLiR (where  denotes the Bitwise XOR operator).
  •  3 L R: output the sum of  range L to R, i.e. Ri=LAi=AL+AL+1+....+AR

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 A1,A2,...,AN.
  • The third line of input contains an integer Q.
  • Next Q lines first contains an integer Ti which denotes the type of the query. If Ti=1 or 2 then next followed three space-separated integers L R X otherwise if Ti=3 then next followed two space-separated integers L 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

1N,Q21050Ai,X<2101Ti31LRN

 

Time Limit: 2.5
Memory Limit: 256
Source Limit:
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,32,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.
Editor Image

?