**Xor-sequence**

### Problem Statement :

An array, A, is defined as follows: A0 = 0 Ax = Ax-1 + x for x >0, where is the symbol for XOR You will be given a left and right index l r. You must determine the XOR sum of the segment of A as A[l] + A[l+1] + ... + A[[r-l] + A[r]. For example, A = [0,1,3,0,4,1,7,0,8]. The segment from l=1 to r=4 sums to 1 +3 +0 + 4 = 6. Print the answer to each question. Function Description Complete the xorSequence function in the editor below. It should return the integer value calculated. xorSequence has the following parameter(s): l: the lower index of the range to sum r: the higher index of the range to sum Input Format The first line contains an integer q, the number of questions. Each of the next q lines contains two space-separated integers, l[i] and r[i], the inclusive left and right indexes of the segment to query. Constraints 1 <= q <= 10^5 1 <= l[i] <= r[i] <= 10^15 Output Format On a new line for each test case, print the XOR-Sum of A's elements in the inclusive range between indices l[i] and r[i].

### Solution :

` ````
Solution in C :
In C++ :
#include <cstdio>
#include <cmath>
#include <iostream>
#include <set>
#include <algorithm>
#include <vector>
#include <map>
#include <cassert>
#include <string>
#include <cstring>
#include <queue>
using namespace std;
#define rep(i,a,b) for(int i = a; i < b; i++)
#define S(x) scanf("%d",&x)
#define S2(x,y) scanf("%d%d",&x,&y)
#define P(x) printf("%d\n",x)
#define all(v) v.begin(),v.end()
#define FF first
#define SS second
typedef long long int LL;
typedef pair<int, int > pii;
typedef vector<int > vi;
LL f(LL x) {
if(x % 4 == 0) {
return x;
} else if (x % 4 == 1) {
return x ^ (x - 1);
} else if (x % 4 == 2) {
return x ^ (x - 1) ^ (x - 2);
} else {
return 0;
}
}
LL g(LL x) {
if(x & 1) {
return f(x) ^ g(x - 1);
}
return 2 * f(x / 2);
}
int main() {
int Q;
S(Q);
while(Q--) {
LL l,r;
scanf("%lld%lld",&l,&r);
printf("%lld\n",g(r) ^ g(l-1));
}
return 0;
}
In Java :
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int Q = in.nextInt();
for(int a0 = 0; a0 < Q; a0++){
long L = in.nextLong();
long R = in.nextLong();
System.out.println(ant(R)^ant(L-1));
}
}
public static long ant (long n){
if(n%2==0){
return 2*ans(n/2);
}
return ans(n)+2*ans(n/2);
}
public static long ans (long n){
if(n==0) return 0;
if(n==1) return 1;
if(n%2==1){
if(n%4==1){
return 1;
}
return 0;
}
else{
return n^(ans(n-1));
}
}
}
In C :
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>
long long memoize[4];
unsigned long long arrayfinder(unsigned long long);
int main(){
int Q;
scanf("%d",&Q);
for(int a0 = 0; a0 < Q; a0++){
unsigned long long L;
unsigned long long R;
unsigned long long total=0;
scanf("%llu %llu",&L,&R);
if((R-L)<17){
for(int i= L;i<=R;i++){
total^=arrayfinder(i);
}
}
else{
while(L%8!=7){
total^=arrayfinder(L);
L++;
}
while(R%8!=7){
total^=arrayfinder(R);
R--;
}
}
printf("%llu\r\n",total);
}
return 0;
}
unsigned long long arrayfinder(unsigned long long x){
if(x%4==0)return x;
if(x%4==1)return 1;
if(x%4==2)return x+1;
if(x%4==3)return 0;
return -1;
}
In Python3 :
#!/bin/python3
import sys
def x(a):
res = [a,1,a+1,0]
return res[a%4]
def y(a):
if a%2==0:
return x(a//2)<<1
else:
r = 0
if (a+1)%4==2:
r =1
return((x(a//2)<<1) + r)
#print(y(5))
Q = int(input().strip())
for a0 in range(Q):
L,R = input().strip().split(' ')
L,R = [int(L),int(R)]
print(y(R)^y(L-1))
```

## View More Similar Problems

## Castle on the Grid

You are given a square grid with some cells open (.) and some blocked (X). Your playing piece can move along any row or column until it reaches the edge of the grid or a blocked cell. Given a grid, a start and a goal, determine the minmum number of moves to get to the goal. Function Description Complete the minimumMoves function in the editor. minimumMoves has the following parameter(s):

View Solution →## Down to Zero II

You are given Q queries. Each query consists of a single number N. You can perform any of the 2 operations N on in each move: 1: If we take 2 integers a and b where , N = a * b , then we can change N = max( a, b ) 2: Decrease the value of N by 1. Determine the minimum number of moves required to reduce the value of N to 0. Input Format The first line contains the integer Q.

View Solution →## Truck Tour

Suppose there is a circle. There are N petrol pumps on that circle. Petrol pumps are numbered 0 to (N-1) (both inclusive). You have two pieces of information corresponding to each of the petrol pump: (1) the amount of petrol that particular petrol pump will give, and (2) the distance from that petrol pump to the next petrol pump. Initially, you have a tank of infinite capacity carrying no petr

View Solution →## Queries with Fixed Length

Consider an -integer sequence, . We perform a query on by using an integer, , to calculate the result of the following expression: In other words, if we let , then you need to calculate . Given and queries, return a list of answers to each query. Example The first query uses all of the subarrays of length : . The maxima of the subarrays are . The minimum of these is . The secon

View Solution →## QHEAP1

This question is designed to help you get a better understanding of basic heap operations. You will be given queries of types: " 1 v " - Add an element to the heap. " 2 v " - Delete the element from the heap. "3" - Print the minimum of all the elements in the heap. NOTE: It is guaranteed that the element to be deleted will be there in the heap. Also, at any instant, only distinct element

View Solution →## Jesse and Cookies

Jesse loves cookies. He wants the sweetness of all his cookies to be greater than value K. To do this, Jesse repeatedly mixes two cookies with the least sweetness. He creates a special combined cookie with: sweetness Least sweet cookie 2nd least sweet cookie). He repeats this procedure until all the cookies in his collection have a sweetness > = K. You are given Jesse's cookies. Print t

View Solution →