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):

from math import factorial

def combinations_count(n, r):
    return factorial(n) // (factorial(n - r) * factorial(r))

def main():
    num = int(input())
    data = list(map(int, input().split()))

    ketasuu = 20
    righ = 0

    keta_flg = [0 for i in range(ketasuu)]
    ans = 0


    for left in range(num):

        left_bin = bin(data[left])[2:]
        left_bin = left_bin.zfill(ketasuu)

        for i in range(ketasuu):
            if left_bin[i] == '1':
                keta_flg[i] = 1

        righ = max(righ, left + 1)

        while righ < num:
            righ_bin = bin(data[righ])[2:]
            righ_bin = righ_bin.zfill(ketasuu)

            flg = 0
            for i in range(ketasuu):
                if righ_bin[i] == '1' and keta_flg[i]:
                    flg = 1
                    break

            if flg:
                break

            for i in range(ketasuu):
                if righ_bin[i] == '1':
                    keta_flg[i] = 1

            righ += 1

        ans += righ - left


        for i in range(ketasuu):
            if left_bin[i] == '1':
                keta_flg[i] = 0

    print(ans)






if __name__ == '__main__':
    main()

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
l, r, fl, fr, gl, gr, ans= 0, 0, 0, A[0], 0, A[0], 0
while l < N:
    while r+1 < N and fr-fl+A[r+1] == gr^gl^A[r+1]:
        r += 1
        fr += A[r]
        gr ^= A[r]
    ans += r-l+1
    fl += A[l]
    gl ^= A[l]
    l += 1
print(ans)

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

b = [0]*n
c = [0]*n

b[0] = a[0]
c[0] = a[0]

for i in range(1,n):
    b[i] = a[i] + b[i-1]
    c[i] = a[i] ^ c[i-1]

b.insert(0,0)
c.insert(0,0)

res = 0
right = 1

for left in range(1,n+1):
    while right < n+1 and b[right]-b[left-1] == c[right] ^ c[left-1]:
        right += 1
    res += right - left
    if left == right:
        right += 1
        
print(res)

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

ruiseki_add = [0] * (n + 1)
for i in range(n):
    ruiseki_add[i + 1] = ruiseki_add[i] + a[i]

ruiseki_xor = [0] * (n + 1)
for i in range(n):
    ruiseki_xor[i + 1] = ruiseki_xor[i] ^ a[i]
    
def solve(s, mid):
    xor_sum = ruiseki_add[mid + 1] - ruiseki_add[s] 
    add_sum = ruiseki_xor[mid + 1] ^ ruiseki_xor[s]
    if xor_sum == add_sum:
        return True
    else:
        return False

ans = 0
for i in range(n):
    ok = i
    ng = n
    while abs(ok - ng) > 1:
        mid = (ok + ng) // 2
        if solve(i, mid):
            ok = mid
        else:
            ng = mid
    ans += ng - i
print(ans)

Solution (Python):
import sys


fin = sys.stdin.readline
N = int(fin())
A_list = tuple(int(elem) for elem in fin().split())
num_valid_intervals = 0

r = 0
cur_sum = 0
cur_xor = 0
for l in range(N):
    item_left = A_list[l]
    while r < N and cur_sum + A_list[r] == cur_xor ^ A_list[r]:
        cur_sum += A_list[r]
        cur_xor ^= A_list[r]
        r += 1
    num_valid_intervals += r - l
    cur_sum -= item_left
    cur_xor ^= item_left
print(num_valid_intervals)

Solution (Python):



import sys
input = sys.stdin.readline
def d():
    n = int(input())
    a = list(map(int,input().split()))
    ans = 0
    l = 0
    r = 0
    s = 0
    while l < n and r < n:
        while s&a[r] == 0:
            s += a[r]
            r += 1
            ans += r-l
            if r == n:
                break
        if l==r:
            s += a[r]
            r += 1
        s -= a[l]
        l += 1
    print(ans)

if __name__ == '__main__':
    d()

Solution (Python):
N, *a = map(int, open(0).read().split())

ans = 0
r = 0
x_xor = 0
x_sum = 0

for l in range(N):
    while (r == l) or (r < N and x_xor ^ a[r] == x_sum + a[r]):
        x_xor ^= a[r]
        x_sum += a[r]
        r += 1

    ans += r - l
    x_xor ^= a[l]
    x_sum -= a[l]
    
print(ans)

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

N=int(input())
A=list(map(int,input().split()))
a=0
b=0
left=0
right=0
res=0
while(left<N):
    while(right<N and a+A[right]==b^A[right]):
        a+=A[right]
        b^=A[right]
        right+=1
    res+=(right-left)
    a-=A[left]
    b^=A[left]
    left+=1
print(res)

Solution (Python):
n=int(input())
arr=list(map(int,input().split()))
s=0
r=0
ans=0
for l in range(n):
  while r<n:
    if s&arr[r]!=0:
      break
    else:
      s+=arr[r]
      r+=1
  ans+=r-l
  s-=arr[l]  
print(ans)

Solution (Python):
n=int(input())
arr=list(map(int,input().split()))
ans=0
pos=0
cnt=0
while 1:
  if pos==n:
    break
  if arr[pos]==0:
    cnt+=1
    pos+=1
    continue
  sum=0
  xor=0
  len=0
  for i in range(pos,n):
    if arr[i]==0:
      len+=1
      continue
    sum+=arr[i]
    xor=xor^arr[i]
    if sum!=xor:
      break
    len+=1
  ans+=len*(cnt+1)+(cnt*(cnt+1))//2
  cnt=0
  pos+=1
if cnt!=0:
  ans+=(cnt*(cnt+1))//2
print(ans)

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

right = 0
total = 0
ans = 0
for left in range(N):  
  while right < N and (total^A[right]) == (total+A[right]):
    total += A[right]
    right += 1
  ans += right - left  
  if left == right:
    right += 1
  else:
    total -= A[left]
print(ans)

Solution (Python):
n=int(input())
a_list=list(map(int,input().split()))
A_list=[format(a_list[i],'b').zfill(20) for i in range(n)]
count=[0]*20
r=0
ans=0
for l in range(n):
    while not 2 in count and r<=n-1:
        for j in range(20):
            count[j]=count[j]+int(A_list[r][j])
        r=r+1
    if not 2 in count:
        ans=ans+n-l
    else:
        ans=ans+r-l-1
    for j in range(20):
        count[j]=count[j]-int(A_list[l][j])
print(ans)

Solution (Python):
N = int(input())
*A, = map(int, input().split())
right = 0; ans = 0; S = 0; S_xor = 0;
for left in range(N):
    while right < N and S_xor ^ A[right] == S + A[right]:
        S_xor ^= A[right]
        S += A[right]
        right += 1
    ans += right - left
    if right == left:
        right += 1
    else:
        S_xor ^= A[left]
        S -= A[left]
print(ans)

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

li,ri = 0,0
ans = 0
xor = A[li]
add = A[li]
while ri < N and li <= ri:
    if xor == add: 
        ans += ri - li +1   
        ri += 1
        if ri < N:  
            xor ^= A[ri]
            add += A[ri]
    else:
        if li < N:
            xor ^= A[li]
            add -= A[li]
        li += 1

print(ans)

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

ans = 0
right = 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):
N = int(input())
A = list(map(int,input().split()))

li,ri = 0,0
ans = 0
xor = A[li]
add = A[li]
while ri < N and li <= ri:
    if xor == add:
        ans += ri - li +1
        ri += 1
        if ri < N:
            xor ^= A[ri]
            add += A[ri]
    else:
        if li < N:
            xor ^= A[li]
            add -= A[li]
        li += 1

print(ans)

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


xor = [0]*(N+1)
cum = [0]*(N+1)
for i in range(N):
    xor[i] = xor[i-1]^A[i]
    cum[i] = cum[i-1]+A[i]

rlist = []
r = 0
for l in range(N):
    r = max(l+1,r)
    while r < N and xor[r]^xor[l-1] == cum[r]-cum[l-1]:
        r += 1
    if r == N:
        break
    r -= 1
    rlist.append(r)
ans = 0
rlist += [N-1]*(N-len(rlist))
for l in range(N):
    r = rlist[l]
    ans += r-l+1
print(ans)

Solution (Python):
N = int(input())
a = list(map(int,input().split()))
l,r = 0,1
ans = 0


nowx = 0
nows = 0
while True:
    if l == N:
        break
    if r == N+1:
        break
    if nowx^a[r-1] == nows+a[r-1]: 
        nowx ^= a[r-1]
        nows += a[r-1]
        ans += r-l
        r += 1
        
    elif r == l:
        r += 1
        l += 1
    else:
        nowx ^= a[l]
        nows -= a[l]
        l += 1
        
print(ans)

Solution (Python):

N=input()
A=map(int,raw_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
    if l==r:r+=1
    else: s-=A[l]
print(ans)

Solution (Python):

N = int(input())
A = list(map(int, input().split()))
ans = 0
s = 0
j = 0

for i in range(N):
    while j < N and not (s & A[j]):
        s ^= A[j]
        j += 1
    ans += j-i
    s ^= A[i]
print(ans)

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

ans = 0
r = 0
xor_A = 0
for l in range(N):
    while (r < N and (xor_A ^ A[r]) == (xor_A + A[r])):
        xor_A += A[r]
        r += 1
    ans += r - l
    if l == r:
        r += 1
    else:
        xor_A -= A[l]
print(ans)

Solution (Python):



N = int(input())
A = [int(x) for x in input().split()]

R = -1
S = 0

answer = 0
for L in range(N):
  if R < L:
    R += 1
    S ^= A[L]
  while R<N-1 and not(S & A[R+1]):
    S ^= A[R+1]
    R += 1
  answer += R-L+1
  S ^= A[L]
  
print(answer)

Solution (Python):
n=int(input())
a_list=list(map(int,input().split()))
pre1=[]
pre2=[]
for i in range(20):
    pre1.append(-1)
    pre2.append(-1)
ans=0
for i in range(n):
    Min=n
    s=format(a_list[i],'b').zfill(20)
    for j in range(20):
        if s[j]=='0':
            Num=i-pre2[j]
        else:
            Num=i-pre1[j]
            pre2[j]=pre1[j]
            pre1[j]=i
        Min=min(Min,Num)
    ans=ans+Min
print(ans)

Solution (Python):
import sys, os

f = lambda:list(map(int,input().split()))
if 'local' in os.environ :
    sys.stdin = open('./input.txt', 'r')



def solve():
    n = f()[0]
    a = f()

    add_sum = 0
    xor_sum = 0
    ans = 0

    l = 0
    r = 0
    while r<=n-1:
        add_sum += a[r]
        xor_sum ^= a[r]

        r+= 1

        if add_sum != xor_sum:
            while add_sum != xor_sum:
                add_sum -= a[l]
                xor_sum ^= a[l]
                ans += r - l-1
                l += 1

        if r == n:
            ans += (n-l+1)*(n-l)//2
    
    print(ans)


solve()

Solution (Python):
import sys, os

f = lambda:list(map(int,input().split()))
if 'local' in os.environ :
    sys.stdin = open('./input.txt', 'r')


def c(r, l):
    cnt = r - l +1

    if cnt<2:
        return 0
    return cnt * (cnt-1)//2

def solve():
    n = f()[0]
    a = f()

    add_sum = 0
    xor_sum = 0
    ans = n

    l = 0
    r = 0
    while r<=n-1:
        add_sum += a[r]
        xor_sum ^= a[r]

        r+= 1

        if r == n and add_sum == xor_sum:
            ans += c(n-1, l)
            break

        if add_sum != xor_sum:
            nl = l
            while add_sum != xor_sum:
                add_sum -= a[nl]
                xor_sum ^= a[nl]
                nl += 1
            
            ans += c( nl-1, l)

            ans += (nl - l)*(r - nl-1)

            l = nl

        if r == n:
            ans += c(n-1, l)




    print(ans)

solve()

Solution (Python):
import collections
N = int(input())
A = [int(_) for _ in input().split()]
dp = collections.defaultdict(int)
dp[0] = 1
ans = 0
for a in A:
    dp_new = collections.defaultdict(int)
    dp_new[0] = 1
    for b in dp:
        if a & b == 0:
            dp_new[a + b] += dp[b]
            ans += dp[b]
    dp = dp_new
print(ans)

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

f = [i for i in range(N)]
left, right = 0, 0
ans = 0
nowxor = 0
nowsum = 0
for left in range(N):
    while right < N and nowxor ^ A[right] == nowsum + A[right]:
        nowsum += A[right]
        nowxor = nowxor ^ A[right]
        right += 1
    ans += right - left
    if left == right:
        right += 1
    else:
        nowsum -= A[left]
        nowxor = nowxor ^ A[left]
print(ans)

Solution (Python):

import sys
from pprint import pprint

def solve(N: int, A: "List[int]"):
    bits = [[] for _ in range(N)]
    for i, a in enumerate(A):
        bit = 0
        while a > 0:
            if a % 2 == 1:
                bits[i].append(bit)
            a //= 2
            bit += 1
    
    cur = set()
    l = 0
    r = 0
    ret = 0
    while l < N:
        while r < N:
            ok = True
            for b in bits[r]:
                if b in cur:
                    ok = False
            if ok:
                for b in bits[r]:
                    cur.add(b)
                r += 1
            else:
                break
        ret += r - l
        
        for b in bits[l]:
            cur.remove(b)
        l += 1
    print(ret)
    return


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) ]  
    solve(N, A)

if __name__ == '__main__':
    main()

Solution (Python):


from sys import stdout
from bisect import bisect_left as binl
from copy import copy, deepcopy
from collections import defaultdict


mod = 1


def intin():
    input_tuple = input().split()
    if len(input_tuple) <= 1:
        return int(input_tuple[0])
    return tuple(map(int, input_tuple))


def intina():
    return [int(i) for i in input().split()]


def intinl(count):
    return [intin() for _ in range(count)]


def modadd(x, y):
    global mod
    return (x + y) % mod


def modmlt(x, y):
    global mod
    return (x * y) % mod


def lcm(x, y):
    while y != 0:
        z = x % y
        x = y
        y = z
    return x


def combination(x, y):
    assert(x >= y)

    if y > x // 2:
        y = x - y

    ret = 1

    for i in range(0, y):
        j = x - i
        i = i + 1

        ret = ret * j
        ret = ret // i

    return ret


def get_divisors(x):
    retlist = []
    for i in range(1, int(x**0.5) + 3):
        if x % i == 0:
            retlist.append(i)
            retlist.append(x // i)
    return retlist


def get_factors(x):
    retlist = []
    for i in range(2, int(x**0.5) + 3):
        while x % i == 0:
            retlist.append(i)
            x = x // i
    retlist.append(x)
    return retlist


def make_linklist(xylist):
    linklist = {}
    for a, b in xylist:
        linklist.setdefault(a, [])
        linklist.setdefault(b, [])
        linklist[a].append(b)
        linklist[b].append(a)
    return linklist


def calc_longest_distance(linklist, v=1):
    distance_list = {}
    distance_count = 0
    distance = 0
    vlist_previous = []
    vlist = [v]
    nodecount = len(linklist)

    while distance_count < nodecount:
        vlist_next = []
        for v in vlist:
            distance_list[v] = distance
            distance_count += 1
            vlist_next.extend(linklist[v])
        distance += 1
        vlist_to_del = vlist_previous
        vlist_previous = vlist
        vlist = list(set(vlist_next) - set(vlist_to_del))

    max_distance = -1
    max_v = None
    for v, distance in distance_list.items():
        if distance > max_distance:
            max_distance = distance
            max_v = v

    return (max_distance, max_v)


def calc_tree_diameter(linklist, v=1):
    _, u = calc_longest_distance(linklist, v)
    distance, _ = calc_longest_distance(linklist, u)
    return distance





def binpos(asumlist, xorlist, n, i):
    floor = i
    ceil = n
    ans = floor

    while True:
        if floor > ceil:
            return ans - i + 1
        pos = (floor + ceil) // 2
        asum = asumlist[pos] - asumlist[i-1]
        xor = xorlist[pos] ^ xorlist[i-1]
        if asum == xor:
            ans = max(ans, pos)
            floor = pos + 1
        else:
            ceil = pos - 1


def main():
    n = intin()
    alist = intina()

    asumlist = [0]
    asum = 0
    for i in range(n):
        asum += alist[i]
        asumlist.append(asum)

    xorlist = [0]
    xor = 0
    for i in range(n):
        xor = xor ^ alist[i]
        xorlist.append(xor)

    ans = 0
    for i in range(n):
        i += 1
        ans += binpos(asumlist, xorlist, n, i)
    print(ans)



if __name__ == '__main__':
    main()

Solution (Python):


from sys import stdout
from bisect import bisect_left as binl
from copy import copy, deepcopy
from collections import defaultdict


mod = 1


def intin():
    input_tuple = input().split()
    if len(input_tuple) <= 1:
        return int(input_tuple[0])
    return tuple(map(int, input_tuple))


def intina():
    return [int(i) for i in input().split()]


def intinl(count):
    return [intin() for _ in range(count)]


def modadd(x, y):
    global mod
    return (x + y) % mod


def modmlt(x, y):
    global mod
    return (x * y) % mod


def lcm(x, y):
    while y != 0:
        z = x % y
        x = y
        y = z
    return x


def combination(x, y):
    assert(x >= y)

    if y > x // 2:
        y = x - y

    ret = 1

    for i in range(0, y):
        j = x - i
        i = i + 1

        ret = ret * j
        ret = ret // i

    return ret


def get_divisors(x):
    retlist = []
    for i in range(1, int(x**0.5) + 3):
        if x % i == 0:
            retlist.append(i)
            retlist.append(x // i)
    return retlist


def get_factors(x):
    retlist = []
    for i in range(2, int(x**0.5) + 3):
        while x % i == 0:
            retlist.append(i)
            x = x // i
    retlist.append(x)
    return retlist


def make_linklist(xylist):
    linklist = {}
    for a, b in xylist:
        linklist.setdefault(a, [])
        linklist.setdefault(b, [])
        linklist[a].append(b)
        linklist[b].append(a)
    return linklist


def calc_longest_distance(linklist, v=1):
    distance_list = {}
    distance_count = 0
    distance = 0
    vlist_previous = []
    vlist = [v]
    nodecount = len(linklist)

    while distance_count < nodecount:
        vlist_next = []
        for v in vlist:
            distance_list[v] = distance
            distance_count += 1
            vlist_next.extend(linklist[v])
        distance += 1
        vlist_to_del = vlist_previous
        vlist_previous = vlist
        vlist = list(set(vlist_next) - set(vlist_to_del))

    max_distance = -1
    max_v = None
    for v, distance in distance_list.items():
        if distance > max_distance:
            max_distance = distance
            max_v = v

    return (max_distance, max_v)


def calc_tree_diameter(linklist, v=1):
    _, u = calc_longest_distance(linklist, v)
    distance, _ = calc_longest_distance(linklist, u)
    return distance





def binpos(asumlist, xorlist, n, i):
    floor = i
    ceil = n
    ans = floor

    while True:
        if floor > ceil:
            return ans - i + 1
        pos = (floor + ceil) // 2
        asum = asumlist[pos] - asumlist[i-1]
        xor = xorlist[pos] ^ xorlist[i-1]
        if asum == xor:
            ans = max(ans, pos)
            floor = pos + 1
        else:
            ceil = pos - 1


def main():
    n = intin()
    alist = intina()

    asumlist = [0]
    asum = 0
    for i in range(n):
        asum += alist[i]
        asumlist.append(asum)

    xorlist = [0]
    xor = 0
    for i in range(n):
        xor = xor ^ alist[i]
        xorlist.append(xor)

    ans = 0
    for i in range(n):
        i += 1
        ans += binpos(asumlist, xorlist, n, i)
    print(ans)



if __name__ == '__main__':
    main()

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

ans = 0
tail = 0
prev = nums[0]

for head in range(1, n):
    num = nums[head]
    if prev & num > 0:
        
        
        while prev & num > 0:
            ans += head - tail
            
            prev = prev ^ nums[tail]
            tail += 1
        prev += num
    else:
        
        prev = prev + num

count = n - tail

ans += count*(count+1)//2

print(ans)

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

count= 0
res = 0
right = 0
for left in range(N):
    while right < N and ((res ^ A[right]) == (res + A[right])):
        res += A[right]
        right += 1

    count += right - left

    if left == right:
        right += 1
    else:
        res -= A[left]

print(count)

Solution (Python):
from functools import reduce


n = int(input())
nums = list(map(int, input().split(" ")))

ans = 0
prev = -1
count = 1 
left = -1

for i,num in enumerate(nums):
    if i == 0:
        prev = num
        left = 0
        count = 1
        continue
    
    

    if prev & num > 0:
        
        while prev & num > 0:
            ans += count
            prev = prev ^ nums[left]
            count -= 1
            left += 1
        count += 1
        prev += num
    else:
        count += 1
        prev = prev + num




ans += count*(count+1)//2

print(ans)

Solution (Python):
from collections import defaultdict
import sys,heapq,bisect,math,itertools,string,queue,datetime
sys.setrecursionlimit(10**8)
INF = float('inf')
mod = 10**9+7
eps = 10**-7
def inpl(): return list(map(int, input().split()))
def inpl_str(): return list(input().split())

N = int(input())
AA = inpl()

ans = 0
R = 0
xorsum = AA[0]
plussum = AA[0]
for L in range(N):
    while xorsum == plussum and R < N:
        R += 1
        if R >= N:
            break
        xorsum ^= AA[R]
        plussum += AA[R]
    
    ans += (R-L)
    xorsum ^= AA[L]
    plussum -= AA[L]


print(ans)

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

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

Solution (Python):

import sys
from itertools import accumulate
from operator import add, xor

def solve(N: int, A: "List[int]"):
    acc_add = [0] +list(accumulate(A, add))
    acc_xor = [0] +list(accumulate(A, xor))

    ret = 0
    j = 0
    for i, (ba, bx) in enumerate(zip(acc_add, acc_xor)):
        while j < N:
            if (acc_add[j+1] - ba) != (acc_xor[j+1] ^ bx):
                break
            j += 1
        ret += j - i
    print(ret)



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) ]  
    solve(N, A)

if __name__ == '__main__':
    main()

Solution (Python):
import bisect
from collections import Counter
def MI(): return map(int, input().split())
def II(): return int(input())
def IS(): return input()
def LI(): return list(map(int, input().split()))


n, a = II(), LI()
ans = 0
right = 0
sum = 0

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

    ans += (right - left)

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

print(ans)

Solution (Python):

import sys
INF = float("inf")
from itertools import accumulate
import operator


def solve(N: int, A: "List[int]"):
    acc_sum = [0]+list(accumulate(A))
    acc_xor = [0]+list(accumulate(A, func=operator.xor))

    ans = 0
    for i in range(1, N+1):       
        left, right = i, N+1
        while right-left > 1:
            
            mid = (right+left)//2
            s = acc_sum[mid]-acc_sum[i-1]
            x = acc_xor[mid] ^ acc_xor[i-1]
            if s > x:
                right = mid
            else:
                left = mid
        ans += left-i+1
        
        
        
        
        

        
        
        
        
        
        
        
        

    
    
    print(ans)
    return


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)]  
    solve(N, A)


if __name__ == '__main__':
    main()

Solution (Python):
N = int(input())
A = [int(i) for i in input().split()]

right = 0
answer = 0
now = 0

for left in range(N):
    while right < N and now + A[right] == now ^ A[right]:
        now += A[right]
        right += 1

    answer += right - left

    if right == left:
        right += 1
    else:
        now -= A[left]


print(answer)

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

right = 0
res = 0
sum = 0

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

    res += right - left

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

print(res)

Solution (Python):

import sys


def solve(N: int, A: "List[int]"):
    l = 0
    r = 0
    sum = 0
    orResult = 0
    while r < N and l < N:
        if A[r] & orResult == 0:  
            sum += r - l + 1
            orResult |= A[r]
            r += 1
        else:
            orResult ^= A[l]
            l += 1
        if l > r and l != N:
            r = l
            orResult = A[r]
            sum += 1
    print(sum)
    return



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)]  
    solve(N, A)


if __name__ == '__main__':
    main()

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

ans=0
r=0
tmp=0
for i in range(N):
    while tmp&a[i]!=0:
        tmp-=a[r]
        r+=1
    
    ans+=i-r+1
    tmp+=a[i]
print(ans)

Solution (Python):
N=int(input())
a=list(map(int,input().split()))
a=[bin(a[i])[-1:1:-1] for i in range(N)]
ans=0
x=[-1]*20
tmp=-1
for i in range(N):
    for j in range(len(a[i])):
        if a[i][j]=="1":
            tmp=max(tmp,x[j])
            x[j]=i
    ans+=i-tmp

print(ans)

Solution (Python):
N=int(input())
a=list(map(int,input().split()))
a=[bin(a[i])[2:].zfill(20) for i in range(N)]
ans=0
x=[-1]*20
tmp=-1
for i in range(N):
    for j in range(20):
        if a[i][j]=="1":
            tmp=max(tmp,x[j])
            x[j]=i
    ans+=i-tmp

print(ans)

Solution (Python):
N=int(input())
a=list(map(int,input().split()))
a=[bin(a[i])[2:].zfill(20) for i in range(N)]
ans=0
x=[-1]*20
tmp=-1
for i in range(N):
    for j in range(20):
        if a[i][j]=="1":
            x[j],tmp=i,max(tmp,x[j])
    ans+=i-tmp

print(ans)

Solution (Python):
N=int(input())
a=list(map(int,input().split()))
a=[bin(a[i])[2:].zfill(20) for i in range(N)]
ans=0
x=[[-1,-1] for i in range(20)]

for i in range(N):
    for j in range(20):
        if a[i][j]=="1":
            x[j]=[x[j][1],i]
    ans+=i-max([ x[j][0] for j in range(20) ])

print(ans)

Solution (Python):
def aaa(i):
    return bin(int(i))[2:].zfill(20)[::-1]

N=int(input())
a=list(map(aaa,input().split()))

ans=0
x=[[-1,-1] for i in range(20)]

for i in range(N):
    for j in range(20):
        if a[i][j]=="1":
            x[j]=[x[j][1],i]
    ans+=i-max([ x[j][0] for j in range(20) ])

print(ans)

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

S = [0]
X = [0]
for a in A:
    S.append(S[-1]+a)
    X.append(X[-1]^a)

l,r = 1,1
ans = 0
p = 1
while r <= N:
    if S[r]-S[l-1] == X[r]^X[l-1]:
        if p==0:
            ans -= (r-l)*(r-l+1)//2
        r += 1
        p = 1
    else:
        if p ==1:
            ans += (r-l)*(r-l+1)//2
        l += 1
        p = 0
ans += (r-l)*(r-l+1)//2
print(ans)

Solution (Python):
num_inputs = int(input())
inputs = [int(st) for st in input().split()]
rnges = [(0,9)]
lf = 0
rg = 0
su = 0
score = 0
while rg < num_inputs:
    if su & inputs[rg] == 0:
        su += inputs[rg]
        if rnges[len(rnges)-1][0] == lf:
            rnges[len(rnges)-1] = (lf, rg+1)
        else:
            rnges.append((lf, rg+1))
        rg += 1
    else:
        
        su -= inputs[lf]
        lf += 1



tmp_lf = 0
tmp_rg = 0

for lf, rg in rnges:
    score += (rg-lf+1)*(rg-lf)//2
    if lf < tmp_rg:
        score -= ((tmp_rg-lf+1)*(tmp_rg-lf)//2)
    tmp_rg = rg


print(score)

Solution (Python):
def main():
    N = int(input())
    A = [int(n) for n in input().split()]
    
    accum = A[0]
    xor = A[0]
    r = 1
    res = 0
    for l in range(N):
        while r < N and accum + A[r] == xor ^ A[r]:
            
            accum += A[r]
            xor ^= A[r]
            r += 1
        res += r - l
        accum -= A[l]
        xor ^= A[l]
        if l == r:
            r+=1
    print(res)

if __name__ == "__main__":
    main()

Solution (Python):
n,*a=map(int,open(0).read().split())
x=s=l=c=0
for r,b in enumerate(a):
  x^=b
  s+=b
  while x<s:
    x^=a[l]
    s-=a[l]
    l+=1
  c+=r-l+1
print(c)

Solution (Python):
n,*a=map(int,open(0).read().split())
x=s=l=c=0
for r,b in enumerate(a):
  x^=b
  s+=b
  while x<s:
    x^=a[l]
    s-=a[l]
    l+=1
  c+=r-l+1
print(c)

Solution (Python):
N=int(input())
A=[int(x) for x in input().split()]
st=0
j=-1
ans=0
for i in range(N):
  while j<N-1:
    if not st&A[j+1]:
      st|=A[j+1]
      j+=1
    else:
      break
  g=j-i+1
  ans+=g
  st-=A[i]
print(ans)

Solution (Python):
n=int(input())
lst=list(map(int, input().split()))
r=0
l=0
ans=0
Sum=lst[0]
xSum=lst[0]
while 1:
  ans+=r-l+1
  if(r==n-1):
    break
  r+=1
  Sum+=lst[r]
  xSum^=lst[r]
  if(Sum!=xSum):
    while(Sum!=xSum):
      Sum-=lst[l]
      xSum^=lst[l]
      l+=1
print(ans)

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

ans = 0
s = 0 
x = 0 

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

    ans += (r-l) 

    if l == r:
        r += 1

    else:
        s -= a[l]
        x ^= a[l]

print(ans)

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

ans = 0
s = 0
j = 0
for i in range(n):
  while not s & a[j] and j < n:
    s += a[j]
    j += 1
  ans += j - i
  s -= a[i]
    
print(ans)

Solution (Python):
import itertools

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

accum_xor = [0]
s = 0
for i in lst:
    s = s ^ i
    accum_xor.append(s)

accum_lst = list(itertools.accumulate(lst))
accum_lst.insert(0, 0)

s = 0
e = 0
count = 0
while s <= n-1:
    flg = False
    if accum_xor[e+1] ^ accum_xor[s] == accum_lst[e+1] - accum_lst[s]:
        e += 1
        if e >= n:
            break
    else:
        flg = True
    if flg:
        count += (e-1) - s + 1
        s += 1
        
if e == n:
    for i in range(s, n):
        count += (n-1)-i+1

print(count)