human
Give me a code to range Query on array whose each element is XOR of index value and previous element and the other steps required given the following:
Range Query on array whose each element is XOR of index value and previous element | function return derived formula value ..
finding xor value of range [ y ... x ].
function to solve query for l and r ..
if l or r is 0..
finding x is divisible by 2 or not ..
Driver Code"
How can the above be solved in Python?
assistant
#Here is code to range Query on array whose each element is XOR of index value and previous element and the other steps required.
 def fun ( x ) :
	y = ( x // 4 ) * 4
	ans = 0
	for i in range ( y , x + 1 ) :
		ans ^= i
	return ans
def query ( x ) :
	if ( x == 0 ) :
		return 0
	k = ( x + 1 ) // 2
	if x % 2 == 0 :
		return ( ( fun ( k - 1 ) * 2 ) ^ ( k & 1 ) )
	else :
		return ( 2 * fun ( k ) )
def allQueries ( q , l , r ) :
	for i in range ( q ) :
		print ( query ( r [ i ] ) ^ query ( l [ i ] - 1 ) )
q = 3
l = [ 2 , 2 , 5 ]
r = [ 4 , 8 , 9 ]
allQueries ( q , l , r )