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 :



title-img


                            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

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 →

Find the Running Median

The median of a set of integers is the midpoint value of the data set for which an equal number of integers are less than and greater than the value. To find the median, you must first sort your set of integers in non-decreasing order, then: If your set contains an odd number of elements, the median is the middle element of the sorted sample. In the sorted set { 1, 2, 3 } , 2 is the median.

View Solution →

Minimum Average Waiting Time

Tieu owns a pizza restaurant and he manages it in his own way. While in a normal restaurant, a customer is served by following the first-come, first-served rule, Tieu simply minimizes the average waiting time of his customers. So he gets to decide who is served first, regardless of how sooner or later a person comes. Different kinds of pizzas take different amounts of time to cook. Also, once h

View Solution →

Merging Communities

People connect with each other in a social network. A connection between Person I and Person J is represented as . When two persons belonging to different communities connect, the net effect is the merger of both communities which I and J belongs to. At the beginning, there are N people representing N communities. Suppose person 1 and 2 connected and later 2 and 3 connected, then ,1 , 2 and 3 w

View Solution →

Components in a graph

There are 2 * N nodes in an undirected graph, and a number of edges connecting some nodes. In each edge, the first value will be between 1 and N, inclusive. The second node will be between N + 1 and , 2 * N inclusive. Given a list of edges, determine the size of the smallest and largest connected components that have or more nodes. A node can have any number of connections. The highest node valu

View Solution →

Kundu and Tree

Kundu is true tree lover. Tree is a connected graph having N vertices and N-1 edges. Today when he got a tree, he colored each edge with one of either red(r) or black(b) color. He is interested in knowing how many triplets(a,b,c) of vertices are there , such that, there is atleast one edge having red color on all the three paths i.e. from vertex a to b, vertex b to c and vertex c to a . Note that

View Solution →