# XOR of range [L - R] inclusive for multiple queries

Hi, I have a doubt about how we can find the xor of all the values of an array within range [L - R] inclusive for multiple queries of the form [L - R] efficiently.
Note : 1 <= L < R <= 10^18

```#include
using namespace std ;

int Q ;
long long int L , R , DP , A;
void pre(){
DP = DP = 1 ;
for(int i=1;i<=64;i++){
DP[i] = (DP[i-1]+DP[i-1]) ;
DP[i] = (DP[i-1]+DP[i-1]) ;
}
}

void solve(){
long long sum = 0 ;
for(int i=63;i>=0;i--){
A[i] += sum ;
if(L & (1LL <=0;i--){
A[i] += sum ;
if(R & (1LL <=0;i--){
A[i] -= A[i] ;
A[i] %= 2 ;
if(A[i])
sum += (1LL << i) ;
}
cout << sum <> Q ;
while(Q -- ){
cin >> L >>  R ;
solve() ;
}
return 0 ;
}
```

I thought over this problem and find a simple solution using Dynammic Programming O(log(10^18)). As you know that the XOR Operation is independent for all bits participating in the operaton. Therefore for each bit we can count the number of numbers having set in the range L to R.

This code is not tested well but it should work as the idea is correct :).

2 Likes

btw can i have link to the above problem .

btw thanks, just learned 2 new approaches for the same task i submitted this code on the link provided by you and got WA.This is because i forget to add a memset statement in my solution. add it and it works very fine . Thanks for asking this