Problem:
 Score : 500 points    Problem Statement There is an integer sequence A of length N .  Find the number of the pairs of integers l and r ( 1 \leq l \leq r \leq N ) that satisfy the following condition:   A_l\ xor\ A_{l+1}\ xor\ ...\ xor\ A_r = A_l\ +\ A_{l+1}\ +\ ...\ +\ A_r   Here, xor denotes the bitwise exclusive OR.   Definition of XOR  The XOR of integers c_1, c_2, ..., c_m is defined as follows:   Let the XOR be X . In the binary representation of X , the digit in the 2^k 's place ( 0 \leq k ; k is an integer) is 1 if there are an odd number of integers among c_1, c_2, ...c_m whose binary representation has 1 in the 2^k 's place, and 0 if that number is even.   For example, let us compute the XOR of 3 and 5 . The binary representation of 3 is 011 , and the binary representation of 5 is 101 , thus the XOR has the binary representation 110 , that is, the XOR is 6 .

Solution (Python):
def main(sample_file = ''):

    """ convenient functions
    
    
    
    
    
    """

    """convenient decorator
    
    
        
        
        
        
        
        
        
        
        
        
    """
    
    
    import sys
    sys.setrecursionlimit(10**7)
    from itertools import accumulate, combinations, permutations, product 
    
    from math import factorial, ceil, floor, sqrt
    def factorize(n):
        """return the factors of the Arg and count of each factor
        
        Args:
            n (long): number to be resolved into factors
        
        Returns:
            list of tuples: factorize(220) returns [(2, 2), (5, 1), (11, 1)]
        """
        fct = []  
        b, e = 2, 0  
        while b * b <= n:
            while n % b == 0:
                n = n // b
                e = e + 1
            if e > 0:
                fct.append((b, e))
            b, e = b + 1, 0
        if n > 1:
            fct.append((n, 1))
        return fct
    def combinations_count(n, r):   
        """Return the number of selecting r pieces of items from n kinds of items.
        
        Args:
            n (long): number
            r (long): number
        
        Raises:
            Exception: not defined when n or r is negative
        
        Returns:
            long: number
        """
        
        if n < 0 or r < 0:
            raise Exception('combinations_count(n, r) not defined when n or r is negative')
        if n - r < r: r = n - r
        if r < 0: return 0
        if r == 0: return 1
        if r == 1: return n
        numerator = [n - r + k + 1 for k in range(r)]
        denominator = [k + 1 for k in range(r)]
        for p in range(2,r+1):
            pivot = denominator[p - 1]
            if pivot > 1:
                offset = (n - r) % p
                for k in range(p-1,r,p):
                    numerator[k - offset] /= pivot
                    denominator[k] /= pivot
        result = 1
        for k in range(r):
            if numerator[k] > 1:
                result *= int(numerator[k])
        return result
    def combinations_with_replacement_count(n, r):
        """Return the number of selecting r pieces of items from n kinds of items allowing individual elements to be repeated more than once.
        
        Args:
            n (long): number
            r (long): number
        
        Raises:
            Exception: not defined when n or r is negative
        
        Returns:
            long: number
        """
        if n < 0 or r < 0:
            raise Exception('combinations_with_replacement_count(n, r) not defined when n or r is negative')
        elif n == 0:
            return 1
        else:
            return combinations_count(n + r - 1, r)
    from bisect import bisect_left, bisect_right
    from collections import deque, Counter, defaultdict 
    from heapq import heapify, heappop, heappush, heappushpop, heapreplace,nlargest,nsmallest 
    from copy import deepcopy, copy 
    import operator
    from operator import itemgetter 
    
    
    from functools import reduce, lru_cache
    def chmin(x, y):
        """change minimum
        if x > y, x = y and return (x, True).
        convenient when solving problems of dp[i]
        
        Args:
            x (long): current minimum value
            y (long): potential minimum value
        
        Returns:
            (x, bool): (x, True) when updated, else (x, False)
        """
        if x > y:
            x = y
            return (x, True)
        else:
            return (x, False)
    def chmax(x, y):
        """change maximum
        if x < y, x = y and return (x, True).
        convenient when solving problems of dp[i]
        
        Args:
            x (long): current maximum value
            y (long): potential maximum value
        
        Returns:
            (x, bool): (x, True) when updated, else (x, False)
        """
        if x < y:
            x = y
            return (x, True)
        else:
            return (x, False)

    from fractions import gcd 
    def gcds(numbers):
        return reduce(gcd, numbers)
    def lcm(x, y):
        return (x * y) // gcd(x, y)
    def lcms(numbers):
        return reduce(lcm, numbers, 1)
    def make_divisors(n, reversed=False):
        """create list of divisors
        
        Args:
            number (int): number from which list of divisors is created
            reversed (bool, optional): ascending order if False. Defaults to False.
        
        Returns:
            list: list of divisors
        """
        divisors = set()
        for i in range(1, int(n**0.5)+1):
            if n % i == 0:
                divisors.add(i)
                divisors.add(n//i)
        return sorted(list(divisors),reverse=reversed)
    
    
    
    INF = 10 ** 18
    MOD = 10 ** 9 + 7
    AtoZ = list('abcdefghijklmnopqrsruvwxyz')
    modpow = lambda a, n, p = MOD: pow(a, n, p) 
    def modinv(a, p = MOD):
        
        
        return modpow(a, p-2, p)
    def modinv_list(n, p = MOD):
        if n <= 1:
            return [0,1][:n+1]
        else:
            inv_t = [0,1]
            for i in range(2, n+1):
                inv_t += [inv_t[p % i] * (p - int(p / i)) % p]
            return inv_t
    def modfactorial_list(n, p = MOD):
        if n == 0:
            return [1]
        else:
            l = [0] * (n+1)
            tmp = 1
            for i in range(1, n+1):
                tmp = tmp * i % p
                l[i] = tmp
            return l
    def modcomb(n, k, fac_list = [], p = MOD):
        
        
        from math import factorial
        if n < 0 or k < 0 or n < k: return 0
        if n == 0 or k == 0: return 1
        if len(fac_list) <= n:
            a = factorial(n) % p
            b = factorial(k) % p
            c = factorial(n-k) % p
        else:
            a = fac_list[n]
            b = fac_list[k]
            c = fac_list[n-k]
        return (a * modpow(b, p-2, p) * modpow(c, p-2, p)) % p
    def modadd(a, b, p = MOD):
        return (a + b) % MOD
    def modsub(a, b, p = MOD):
        return (a - b) % p
    def modmul(a, b, p = MOD):
        return ((a % p) * (b % p)) % p
    def moddiv(a, b, p = MOD):
        return modmul(a, modpow(b, p-2, p))

    class UnionFindTree:
        """union find tree class
        TODO: fix this description...
        how to use (example):
        >>  uf = UnionFindTree(N) 
        >>  if uf.find_root(a) == uf.find_root(b):
        >>      do something
        >>  else:
        >>      do something
        >>      uf.unite(a, b)
        """
        def __init__(self, N):
            self.root = [-1] * (N+1)
            self.rank   = [0]  * (N+1)
            self.connected_num = [1] * (N+1)
    
        def find_root(self,x):
            root = self.root
            while root[x] != -1:
                x = root[x]
            return x
    
        def unite(self,x,y):
            root = self.root
            rank = self.rank
            connected_num = self.connected_num
            find_root = self.find_root
    
            rx = find_root(x)
            ry = find_root(y)
            if rx != ry:
                if rank[rx] < rank[ry]:
                    root[rx] = ry
                    rx,ry = ry,rx
                else:
                    if rank[rx] == rank[ry]:
                        rank[rx] += 1
                    root[ry] = rx       
                connected_num[rx] += connected_num[ry]

    
    
    
    
    
    
    
    

    def BellmanFord(N, M, ABC, vertex_start, vertex_end, value_if_inf = -1, find_shortest = False):
        """to calculate furthest or shortest length between vertex_start and vertex_end using BellmanFord algorithm
        
        Args:
            N (int): number of vertices
            M (int): number of edges
            ABC (list): [(ai, bi, ci) for _ in range(N)] where i-th edge is directed from vertex ai to vertex bi and the length is ci 
            vertex_start (int): start vertex. usually use 0.
            vertex_end (int): end vertex. usually use N-1.
            value_if_inf (int or string as you like, optional): value you want when the furthest (or shortest) distance is infinite (or -infinite). Defaults to -1.
            find_shortest (bool, optional): choose False to find furthest path. Defaults to False.
        
        Returns:
            int or string: normally int (but can be str if you set value_if_inf to str)
            
        Example:
                N, M, P = R()
                ABC = [R() for _ in range(M)]
                ABC = [(a-1, b-1, c-P) for a, b, c in ABC]
                print(BellmanFord(N, M, ABC, 0, N-1, value_if_inf = 'inf'))

        """

        def make_reachable_list(N, M, ABC, vertex_start, vertex_end):
            reachable_to_direct = defaultdict(list)
            reachable_from_direct = defaultdict(list)
            reachable_from_start = [False] * N
            reachable_to_end = [False] * N
            reachable_from_start[vertex_start] = True
            reachable_to_end[vertex_end] = True
            reachable_from_both_sides = [False] * N
            dfs_from_start = []
            dfs_to_end = []
            for a, b, c in ABC:
                reachable_to_direct[a].append(b)
                reachable_from_direct[b].append(a)
                if a == vertex_start:
                    dfs_from_start.append(b)
                    reachable_from_start[b] = True
                if b == vertex_end:
                    dfs_to_end.append(a)
                    reachable_to_end[a] = True
            while dfs_from_start:
                v = dfs_from_start.pop()
                for i in reachable_to_direct[v]:
                    if not reachable_from_start[i]:
                        reachable_from_start[i] = True
                        dfs_from_start.append(i)
            while dfs_to_end:
                v = dfs_to_end.pop()
                for i in reachable_from_direct[v]:
                    if not reachable_to_end[i]:
                        reachable_to_end[i] = True
                        dfs_to_end.append(i)
            for i in range(N):
                reachable_from_both_sides[i] = reachable_from_start[i] and reachable_to_end[i]
            return reachable_from_both_sides
        
        reachable_from_both_sides = make_reachable_list(N, M, ABC, vertex_start, vertex_end)
        
        if find_shortest:
            dist = [INF for i in range(N)]
        else:
            dist = [-INF for i in range(N)]
        
        dist[vertex_start] = 0
        for i in range(N):
            updated = False
            for a, b, c in ABC:
                if not reachable_from_both_sides[a]:
                    continue
                elif find_shortest:
                    update_condition = dist[a] + c < dist[b]
                else:
                    update_condition = dist[a] + c > dist[b]
                if dist[a] != INF and update_condition:
                    dist[b] = dist[a] + c
                    updated = True
                    if i == N-1:
                        return value_if_inf
            if not updated:
                break
        return dist[vertex_end]

    
    def warshall_floyd(number_vertex, XYD, shift_one_for_vertex=False):
        
        
        
        
        
        d = [[float("inf")]*number_vertex for i in range(number_vertex)]
        for x, y, dist in XYD:
            if shift_one_for_vertex:
                x = x - 1
                y = y - 1
            d[x][y] = dist
            d[y][x] = dist
        for i in range(number_vertex):
            d[i][i] = 0 
        for k in range(number_vertex):
            for i in range(number_vertex):
                for j in range(number_vertex):
                    d[i][j] = min(d[i][j],d[i][k] + d[k][j])
        return d


    """ initialize variables and set inputs
    
        
        
            
            
    
        
        
        
        
        
        
        
    
        
        
        
        
    
        
        
    """

    
    r = lambda: sys.stdin.readline().strip()
    r_int = lambda: int(r())
    r_float = lambda: float(r())
    R = lambda: list(map(int, r().split()))
    R_str = lambda: r().split()
    R_map = lambda: map(int, r().split())
    R_float = lambda: list(map(float, r().split()))
    R_tuple = lambda: tuple(map(int, r().split()))

    """ how to treat input
    
    
    
    
    
    
    
    """

    
    if sample_file:
        sys.stdin = open(sample_file)
        
    
    
    
    N = r_int()
    A = R()
    start = 0 
    end = 0
    tmp_xor = 0
    tmp_plus = 0
    ans = 0
    while True:
        while end < N and tmp_xor ^ A[end] == tmp_plus + A[end]:
            tmp_xor = tmp_xor ^ A[end]
            tmp_plus = tmp_xor
            end += 1
        if end == N:
            
            while start <= end:
                ans += end - start
                start += 1
            print(ans)
            return
        
        ans += end - start
        tmp_xor = tmp_xor ^ A[start]
        tmp_plus = tmp_xor
        start += 1
        

    
    
    

    """memo: how to use defaultdict of list
    
    Dic = defaultdict(list)
    
    Dic[x].append(y)
    
    for k, v in Dic.items():
    """
    
    """memo: how to solve binary problems
    
    >>> a = 5
    >>> bin_str_a = format(a, '
    >>> print(bin_str_a)
    0b0101 
    """
    
    """memo: how to solve the problem
    creating simple test/answer
    greed
    simple dp
    graph
    """

if __name__ == '__main__':
    main()

Solution (Python):
N=int(input())
A=list(map(int, input().split()))
ans=0
right=0
SUM=0
xor=-1
for left in range(N):
    while right<N and ((SUM+A[right]==(xor^A[right])) or (xor==-1)):
        if xor==-1:
            xor=A[left]
        else:
            xor=xor^A[right]
        SUM+=A[right]
        right+=1
    ans+=right-left
    
  
    SUM-=A[left]
    xor=A[left]^xor

print(ans)

Solution (Python):
N=int(input())
A=list(map(int, input().split()))
L=[0]*(2**20)
for a in A:
    L[a]+=1

"""
for a  in A:
    b=bin(a)[2:]
    add=20-len(b)
    print("0"*add + b)
"""
ans=0
right=0
SUM=0
xor=-1
for left in range(N):
    while right<N and ((SUM+A[right]==(xor^A[right])) or (xor==-1)):
        if xor==-1:
            xor=A[left]
        else:
            xor=xor^A[right]
        SUM+=A[right]
        right+=1
    ans+=right-left
    
  
    SUM-=A[left]
    xor=A[left]^xor

print(ans)

Solution (Python):
def main():
    N = int(input())
    A = [int(i) for i in input().split()]
    ans = 0
    right = 0
    cur_bin = 0
    for left in range(N):
        while right < N and (cur_bin & A[right] == 0):
            cur_bin += A[right]
            right += 1
        ans += right-left
        if left == right:
            right += 1
        else:
            cur_bin ^= A[left]
    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
N = int(input())
A = list(map(int,input().split()))
C = [0 for _ in range(N+1)]
for i in range(1,N+1):
    C[i] = C[i-1]^A[i-1]
A = [bin(A[i])[2:] for i in range(N)]
A = [A[i] if len(A[i])==20 else "0"*(20-len(A[i]))+A[i] for i in range(N)]
cnt = 0
cur = 0
for i in range(N):
    cnt += cur-i
    b = C[cur]^C[i]
    b = bin(b)[2:]
    if len(b)<20:
        b = "0"*(20-len(b))+b
    B = [int(b[j]) for j in range(20)]
    while cur<N:
        flag = 0
        for j in range(20):
            B[j] += int(A[cur][j])
            if B[j]>=2:
                flag = 1
                break
        if flag==1:break
        cnt += 1
        cur += 1
print(cnt)

Solution (Python):
def main():
    N = int(input())
    A = [int(i) for i in input().split()]
    ans = 0
    right = 0
    cur_bin = 0
    for left in range(N):
        while right < N and (cur_bin ^ A[right] == cur_bin + A[right]):
            cur_bin += A[right]
            right += 1
        ans += right-left
        if left == right:
            right += 1
        else:
            cur_bin ^= A[left]
    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
N = int(input())
A = list(map(int, input().split()))

right = 0
xor = 0
S = 0
ans = 0
for left, a in enumerate(A):
    while right < N and (xor ^ A[right]) == (S + A[right]):
        xor ^= A[right]
        S += A[right]
        right += 1
    S -= a
    xor ^= a
    ans += right - left

print(ans)

Solution (Python):
import sys
input = sys.stdin.readline

n = int(input())
a = [int(x) for x in input().split()]

right = 0
ans = 0
S = 0

for left in range(n):
    while right < n and S + a[right] == S ^ a[right]:
        S += a[right]
        right += 1

    ans += right - left
    if left == right:
        right += 1
    else:
        S -= a[left]

print(ans)

Solution (Python):
import sys
def main():
    input = sys.stdin.readline
    N=int(input())
    *A,=map(int, input().split())
    dc=[[0]*20 for _ in range(N+1)]
    for i in range(N):
        a = A[i]
        d = dc[i+1]
        p = dc[i]
        for j in range(20):
            d[j]= p[j] + (1 if a&(1<<j) else 0)
    ans=0
    l,r=0,1
    for l in range(N):
        dcl=dc[l]
        while r<=N:
            dcr=dc[r]
            for i in range(20):
                if dcr[i]-dcl[i]>1: break
            else:
                r+=1
                continue
            break
        ans+=r-l-1
    print(ans)

if __name__ == '__main__':
    main()

Solution (Python):
def split(word): 
    return [char for char in word]  
n=int(input())
a=list(map(int,input().split()))
blist=[]
for i in range(20):
  blist.append(0)
ans=0
finalbad=0
for i in range(n):
  s='{0:08b}'.format(a[i])
  maxclash=0
  for j in range(20-len(s),20):
    if s[j-(20-len(s))]=='1':
      if blist[j]>maxclash:
        maxclash=blist[j]
      blist[j]=i+1
  if maxclash>finalbad:
    finalbad=maxclash
  ans+=i+1-finalbad
print(ans)

Solution (Python):
N = int(input())
A = list(map(int,input().split()))

ans = 0
r = 0
tmp = 0
for l in range(N):
    while r < N and (tmp&A[r]) == 0:
        tmp += A[r]
        r += 1
    tmp -= A[l]
    ans += r - l
print(ans)

Solution (Python):
from operator import xor, add
from itertools import accumulate

N = int(input())
A = [0] + list(map(int, input().split()))

Asum = list(accumulate(A, func=add))
Axor = list(accumulate(A, func=xor))

ans = 0
r = 0
for l in range(N):
    while r < N and Asum[r+1] - Asum[l] == Axor[r+1] ^ Axor[l]:
        r += 1
    ans += r - l
    if r == l:
        r += 1

print(ans)

Solution (Python):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)

n = int(readline())
a = list(map(int, readline().split()))
ans = n
index = 0
v = 0
for i, (bf, af) in enumerate(zip(a, a[1:])):
    v += bf
    for _ in range(index, i + 1):
        if v ^ af == v + af:
            break
        v -= a[index]
        index += 1
    ans += i + 1 - index
print(ans)

Solution (Python):
n = int(input())
a = list(map(int, input().split()))

def check(num1,num2):
    return (num1+num2) == (num1^num2)

ans=0
r=0
now=0
for l in range(n):
    while r < n and check(now,a[r]):
        now+=a[r]
        r+=1

    ans+=r-l
    if l==r:
        r+=1
    else:
        now-=a[l]
print(ans)

Solution (Python):
n = int(input())
a = list(map(int, input().split()))

def check(num1,num2):
    return (num1+num2) == (num1^num2)

ans=0
l,r=0,0
now=0
while l < len(a):
    while r < len(a) and check(now,a[r]):
        now+=a[r]
        r+=1

    ans+=r-l
    if l==r:
        r+=1
    else:
        now-=a[l]
    l+=1
print(ans)

Solution (Python):

import sys
MOD = 1000000007

def solve(N: int, A: "List[int]"):
    l, r = 0, 1
    sux, su, ans = A[0], A[0], 0
    while l < len(A):
        while r < len(A) and (sux ^ A[r]) == (su + A[r]):
            sux ^= A[r]
            su += A[r]
            r += 1
        ans += r - l
        sux ^= A[l]
        su -= A[l]
        l += 1
    return ans


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  
    A = [int(next(tokens)) for _ in range(N)]  
    print(solve(N, A))

if __name__ == '__main__':
    main()

Solution (Python):
import sys
sys.setrecursionlimit(1 << 25)
read = sys.stdin.readline
ra = range
enu = enumerate


def read_ints():
    return list(map(int, read().split()))


def read_a_int():
    return int(read())


def read_tuple(H):
    '''
    H is number of rows
    '''
    ret = []
    for _ in range(H):
        ret.append(tuple(map(int, read().split())))
    return ret


def read_col(H):
    '''
    H is number of rows
    A列、B列が与えられるようなとき
    ex1)A,B=read_col(H)    ex2) A,=read_col(H) 
    '''
    ret = []
    for _ in range(H):
        ret.append(list(map(int, read().split())))
    return tuple(map(list, zip(*ret)))


def read_matrix(H):
    '''
    H is number of rows
    '''
    ret = []
    for _ in range(H):
        ret.append(list(map(int, read().split())))
    return ret
    


MOD = 10**9 + 7
INF = 2**31  

from collections import defaultdict, Counter, deque
from operator import itemgetter
from itertools import product, permutations, combinations
from bisect import bisect_left, bisect_right  









N = read_a_int()
A = read_ints()



















def two_pointers(ls: list):
    '''すべてのlに対して、条件is_okをみたすls[l:r]の中で
    r - lが最大になるような(l,r)の集合を返す'''
    n_ls = len(ls)
    ret = []

    def append(r, pre_states):
        '''状態にls[r]を考慮して更新する'''
        
        sum_state, xor_state = pre_states
        sum_state += ls[r]
        xor_state ^= ls[r]
        return (sum_state, xor_state)

    def pop(l, pre_states):
        '''状態からls[l]を抜く更新をする'''
        
        sum_state, xor_state = pre_states
        sum_state -= ls[l]
        xor_state ^= ls[l]
        return (sum_state, xor_state)

    def is_ok(r, pre_states):
        '''満たしていてほしい条件'''
        
        states = append(r, pre_states)
        sum_state, xor_state = states
        return sum_state == xor_state

    r = 0
    states = (0, 0)
    for l in range(n_ls):
        while r < n_ls and is_ok(r, states):
            
            states = append(r, states)
            r += 1
        ret.append((l, r))
        
        states = pop(l, states)
    return ret


idxs = two_pointers(A)
ans = 0
for l, r in idxs:
    ans += r - l
print(ans)

Solution (Python):
import sys
sys.setrecursionlimit(1 << 25)
read = sys.stdin.readline
ra = range
enu = enumerate


def read_ints():
    return list(map(int, read().split()))


def read_a_int():
    return int(read())


def read_tuple(H):
    '''
    H is number of rows
    '''
    ret = []
    for _ in range(H):
        ret.append(tuple(map(int, read().split())))
    return ret


def read_col(H):
    '''
    H is number of rows
    A列、B列が与えられるようなとき
    ex1)A,B=read_col(H)    ex2) A,=read_col(H) 
    '''
    ret = []
    for _ in range(H):
        ret.append(list(map(int, read().split())))
    return tuple(map(list, zip(*ret)))


def read_matrix(H):
    '''
    H is number of rows
    '''
    ret = []
    for _ in range(H):
        ret.append(list(map(int, read().split())))
    return ret
    


MOD = 10**9 + 7
INF = 2**31  

from collections import defaultdict, Counter, deque
from operator import itemgetter
from itertools import product, permutations, combinations
from bisect import bisect_left, bisect_right  









N = read_a_int()
A = read_ints()
a_sum = 0
a_xor = 0
ans = 0
r = -1
for l in range(N):
    if r <= l:  
        r = l
        a_sum = A[r]
        a_xor = A[r]
    while r < N and a_sum == a_xor:
        r += 1
        if r == N:  
            break
        a_sum += A[r]
        a_xor ^= A[r]
        
    ans += r - l
    
    a_sum -= A[l]
    a_xor ^= A[l]
print(ans)

Solution (Python):
n,*a=map(int,open(0).read().split());c=0;r=1;m=a[0]
for i in range(n):
 while r<n and not(a[r]&m):m|=a[r];r+=1
 c+=r-i;m&=~a[i]
print(c)

Solution (Python):
N = int(input())
A = list(map(int, input().split()))

bit_index = [-1] * 20
cnt = 0
pos = -1
for i in range(N):
    a = A[i]
    for j in range(20):
        if (a >> j) & 1:
            pos = max(pos, bit_index[j])
            bit_index[j] = i
    cnt += i - pos
print(cnt)

Solution (Python):
n = int(input())
A = list(map(int,input().split()))

s = 0
x = 0
j = 0

ans = 0
for i in range(n):
    while j < n:
        if s + A[j] == x ^ A[j]:
            s += A[j]
            x ^= A[j]
            j += 1
        else:
            break
    ans += j - i 
    s -= A[i]
    x ^= A[i]
print(ans)

Solution (Python):
n = int(input())
a = list(map(int,input().split()))
ssum = [0]*(n+1)+[2**21-1]
sxor = [0]*(n+1)+[2**21-1]
for i in range(n):
  ssum[i+1] = a[i] + ssum[i]
  sxor[i+1] = a[i] ^ sxor[i]
def f(i,j):
  return ssum[j]-ssum[i-1]
def g(i,j):
  return sxor[j]^sxor[i-1]
cnt = [0]*(n+1)
r = 1
for l in range(1,n):
  while f(l,r) == g(l,r):
    if r == n+1:
      break
    r += 1
  cnt[l] = r-1
cnt[n] = n
ans = 0
for i in range(1,n+1):
  ans += cnt[i] - i + 1
print(ans)

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
l=r=now=0
ans=[0]*n 
while l<n:
    if r<n and now^a[r]==now+a[r]:
        now+=a[r]
        
        r+=1
    else:
        now-=a[l]
        l+=1
        ans[l-1]=r-l+1
print(sum([ans[i] for i in range(n)]))

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
l=r=now=0
ans=[0]*n 
while l<n:
    if r<n and now^a[r]==now+a[r]:
        now+=a[r]
        
        r+=1
    else:
        now-=a[l]
        l+=1
        ans[l-1]=r-1
print(sum([ans[i]-i+1 for i in range(n)]))

Solution (Python):
N = int(input())
numbers = [int(x) for x in input().split()]


def solve():
  xor = numbers[0]
  total = numbers[0]
  count = 0
  right = 1
  
  for left in range(N):
    while right < N and xor ^ numbers[right] == total + numbers[right]:
      xor ^= numbers[right]
      total += numbers[right]
      right += 1
   
    count += right - left
  
    xor ^= numbers[left]
    total -= numbers[left]
  
  return count
  
    
print(solve())

Solution (Python):
import sys
sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline

n = int(input())
a = list( map(int, input().split()))
ans=0
right=0
sum_pls = 0
sum_xor = 0

for left in range(n):

    while right<n and (sum_pls+a[right])==(sum_xor^a[right]):
        sum_pls += a[right]
        sum_xor ^= a[right]
        right+=1

    ans += right - left
    sum_pls -= a[left]
    sum_xor ^= a[left]

print(ans)

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
l=0
r=0
now=0
ans=[0]*n
while l < n:
    if r < n and now^a[r] == now + a[r]:
        now += a[r]
        ans[l]=r
        r+=1
    else:
        now -= a[l]
        l+=1
        ans[l-1]=r-1
print(sum([ans[i]-i+1 for i in range(n)]))

Solution (Python):
n = int(input())
a = [int(x) for x in input().split()]

l, r = 0, 0
ans = 0
key = 0
while r < n:
  if key^a[r] == key+a[r]:
    key = key+a[r]
    r += 1
    continue
  key = key^a[l]
  ans += r-l
  l += 1
  if l > r:
    key = key^a[r]
    r += 1

while l < n:
  if key^a[l] == key-a[l]:
    ans += n-l
    l += 1
    continue
  break
print(ans)

Solution (Python):



N = int(input())
A = list(map(int, input().split()))


def main():
    x = 0
    right = 0
    while right < N and x & A[right] == 0:
        x += A[right]
        right += 1
    
    ans = right
    for left in range(N - 1):
        x -= A[left]
        while right < N and x & A[right] == 0:
            x += A[right]
            right += 1
        ans += right - left - 1
    
    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
N = int(input())
A = list(map(int, input().split()))

l = r = 0
now = 0
ans = 0
temp = 0
while r < N:
  if now ^ A[r] == now + A[r]:
    ans += temp + 1
    temp += 1
    now += A[r]
    r += 1
  else:
    temp = max(temp - 1, 0)
    now -= A[l]
    l += 1

print(ans)

Solution (Python):
N = int(input())
A = [int(_) for _ in input().split()]
ans = 0
s = A[0]
x = A[0]
r = 1
for l in range(N-1):
    while r < N and s+A[r] == x^A[r]:
        s += A[r]
        x ^= A[r]
        ans += r-l
        r += 1
    if l == r:
        r += 1
    else:
        s -= A[l]
        x ^= A[l]
print(ans+N)

Solution (Python):
import sys,queue,math,copy,itertools
from fractions import gcd
sys.setrecursionlimit(10**7)
INF = 10**18
MOD = 10**9 + 7
LI = lambda : [int(x) for x in sys.stdin.readline().split()]
_LI = lambda : [int(x)-1 for x in sys.stdin.readline().split()]

N = int(input())
A = LI()+[-1]


ans = 0
l = 0
r = 0
xor = 0
s = 0

while r < N:

    if xor & A[r] == 0:
        xor ^= A[r]
        r += 1
        ans += (r - l)
    else:
        s = xor & A[r]
        while s:
            s -= s & A[l]
            xor ^= A[l]
            l += 1

print (ans)

Solution (Python):
import sys,queue,math,copy,itertools
from fractions import gcd
sys.setrecursionlimit(10**7)
INF = 10**18
MOD = 10**9 + 7
LI = lambda : [int(x) for x in sys.stdin.readline().split()]
_LI = lambda : [int(x)-1 for x in sys.stdin.readline().split()]

N = int(input())
A = LI()+[-1]


ans = 0
l = 0
r = 0
xor = 0
s = 0

while r < N:

    if xor & A[r] == 0:
        xor ^= A[r]
        r += 1
        ans += (r - l)
    else:
        s = xor & A[r]
        while s:
            s -= s & A[l]
            xor ^= A[l]
            l += 1
            if l == r : break
print (ans)

Solution (Python):
import sys
def main():
    input = sys.stdin.readline
    N = int(input())
    A = map(int, input().split())

    b = [0] * 20
    last = [-1] * 20
    ans = 0
    l = 0
    for r, a in enumerate(A):
        for i in range(20):
            if a & 1:
                if b[i] == 1: l = max(l, last[i] + 1)
                b[i] = 1
                last[i] = r
            a >>= 1
        ans += r-l+1
    print(ans)

if __name__ == '__main__':
    main()

Solution (Python):
N=int(input())
A=list(map(int,input().split()))
S=[0 for i in range(0,N+1)]
X=[0 for i in range(0,N+1)]
for i in range(0,N):
    S[i+1]=S[i]+A[i]
    X[i+1]=X[i]^A[i]

s=0
t=0
count=0
while N>=s and N>=t:
    sum=S[t]-S[s]
    xor=X[t]^X[s]
    if sum==xor:
        t+=1
    else:
        count+=N-t+1
        s+=1
print(N*(N+1)//2-count)

Solution (Python):

n = int(input())
al = list(map(int, input().split()))

s = 0
res = 0
l = r = 0
while l<n:
    while r<n and s^al[r]==s+al[r]:
        s += al[r]
        r += 1

    res += r-l

    if r==l:
        r += 1
    else:
        s -= al[l]

    l += 1

print(res)

Solution (Python):
n = int(input())
*a, = map(int, input().split())

res = 0
r = 0
s = 0
for l in range(n):
    while r < n and (s ^ a[r]) == (s + a[r]):
        s += a[r]
        r += 1

    res += r - l
    if l == r:
        r += 1
    else:
        s -= a[l]
print(res)

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
ans=0
s=0
r=0
for l in range(n):
  while r<n and s^a[r]==s+a[r]:
    s+=a[r]
    r+=1
  ans+=r-l
  s-=a[l]
print(ans)