XOR queries

4

1 votes
Approved, Data Structures, Hard, Segment Trees, Treap
Problem

You are given a 1indexed array A, of length n, and q queries to be performed on it. The queries are of two types

  • 1 i x : Change the value of Ai to x
  • 2 L R I J : Find l, r,i and j according to the code below. Consider the subarray B=[Al,Al+1Ar] . Sort B, and find the Bitwise xor of Bi,Bi+1Bj in the sorted array B.

You maintain the answer to the last query of type 2 in variable ans . Note that ans is initially 0. Now, whenever you get a query of type 2, calculate l, r,i and j as :

l = 1 + ((ans ^ L) % n);
r = 1 + ((ans ^ R) % n);
if(l > r) swap(l, r);
i = 1 + ((I ^ ans) % (r - l + 1));
j = 1 + ((J ^ ans) % (r - l + 1));
if(i > j) swap(i, j);
Here ^ denotes the bitwise xor operator, and % denotes the modulo operator

Note that array A is NOT changed in a query of second type.

Input

  • The first line contains n and q
  • The next line contains n space separated integers representing the array A
  • Each of the next q lines contain a query either of type 1 or type 2 .

Output
For each of the query of type 2, print the required value on a new line.

Constraints

  • 1n,q,L,R,I,J5×104

  • 1Ai,x105

  • At any instant, all elements of A are distinct

Time Limit: 4
Memory Limit: 256
Source Limit:
Explanation

In the first query, l,r,i,j come out to be 1,2,1,2 respectively. Therefore, array B=[8,10]. and the required answer is B1B2=810=2

Then, in second query, we update A4 to 1. Now, A=[10,8,5,1]

In query 3, value of ans is 2 . Using this value, l,r,i,j come out to be 3,4,1,1 respectively. Array B=[1,5], and the required answer is B1=1

Editor Image

?