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 collections import defaultdict, Counter
from itertools import product, groupby, count, permutations, combinations
from math import pi, sqrt
from collections import deque
from bisect import bisect, bisect_left, bisect_right
from string import ascii_lowercase
from functools import lru_cache
import sys
sys.setrecursionlimit(10000)
INF = float("inf")
YES, Yes, yes, NO, No, no = "YES", "Yes", "yes", "NO", "No", "no"
dy4, dx4 = [0, 1, 0, -1], [1, 0, -1, 0]


def inside(y, x, H, W):
    return 0 <= y < H and 0 <= x < W

def func(i, N, cs_n, cs_x):
    left, right = i - 1, N + 1
    ans = -1
    while right - left > 1:
        middle = (left + right) // 2
        x = cs_x[middle] ^ cs_x[i]
        t = cs_n[middle] - cs_n[i]
        if x == t:
            left = middle
            ans = middle
        else:
            right = middle

    return ans


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

    cs_n = [0] * (N + 1)
    cs_x = [0] * (N + 1)
    for i in range(0, N):
        cs_n[i + 1] = cs_n[i] + A[i]
        cs_x[i + 1] = cs_x[i] ^ A[i]

    ans = 0
    for l in range(N):
        num = func(l, N, cs_n, cs_x)
        ans += num - l
    print(ans)

if __name__ == '__main__':
    main()

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

left = 0
right = 0
length = 0
sm = 0
ans = 0
while left!=N or right!=N:
    if right == N:
        sm -= A[left]
        length -= 1
        left += 1
    elif left == right or (sm&A[right] == 0):
        sm += A[right]
        length += 1
        right += 1
        ans += length
    else:
        sm -= A[left]
        length -= 1
        left += 1

    
print(ans)

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

right = -1
ans = 0
tmp = 0
f_0 = 0
for left in range(len(A)):
    tmp -= f_0
    while(True):
        right += 1
        if(right == N):
            break
        if(bin(tmp + A[right]) == bin(tmp ^ A[right])):
            tmp += A[right]
        else:
            break
    ans += right - left
    f_0 = A[left]
    right -= 1
    

print(ans)

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

ans = 0
total = 0
right = 1
for left in range(N):
    while right < N+1:
        cur = A[right-1]
        if total & cur == 0:
            right += 1
            total += cur
        else:
            total -= A[left]
            break
        
        ans += right-left-1

        
print(ans)

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

ans = 0
right = 0
arith_sum = 0
for left in range(N):
    while right < N and arith_sum ^ an[right] == arith_sum + an[right]:
        arith_sum += an[right]
        right += 1
    ans += right - left
    arith_sum -= an[left]

print(ans)

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

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

print(ans)

Solution (Python):
def xor(x,y):
  return str(int(x)+y).replace("2", "0")
  
input()
s = list(map(int, input().split()))
t = list(map(lambda x: int(bin(x)[2:]), s))

ans = 0
r = 0
x = 0
y = "0"
for i in range(len(s)):
  ans += r - i
  for j in range(r, len(s)):
    nx = x + s[j]
    ny = xor(y, t[j])
    if bin(nx)[2:] == ny:
      ans += 1
      r += 1
      x = nx
      y = ny
    else:
      break
  x = x - s[i]
  y = xor(y, t[i])

print(ans)

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

i = 0
j = 1
res = a[i]
total = n

while i < n and j < n:
  if res & a[j] == 0:
    res += a[j]
    j += 1
  else:
    res -= a[i]
    i += 1
    total += j - i
    if j == i:
      j += 1
      res = a[i]

if j == n:
  if i < n-1:
    for k in range(i+1, n):
      total += j-k

print(total)

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

right = 0
cursum = 0
xor_res = 0

cnt = 0 

for left in range(N) :
    if left > right :
        right = left
    
    while right < N and left < N and cursum + a[right] == xor_res ^ a[right] :
        cursum += a[right]
        xor_res ^= a[right]
        right += 1
        
    cnt += right - left
    
    
    cursum -= a[left]
    xor_res ^= a[left]
    
    
    
print(cnt)

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


sum = 0


xor = 0
right = 0
cnt = 0
for left in range(n):
    while right < n and xor ^ a[right] == sum + a[right]:
        xor = sum + a[right]
        sum = xor
        right += 1
    
    cnt += right - left
    if right == left:
        xor = a[right]
        sum = a[right]
        right += 1
    else:
        xor = xor ^ a[left] 
        sum -= a[left]
print(cnt)

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

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

Solution (Python):
from collections import deque

def solve(xs):
    ans = 0
    s = 0
    n = 0
    queue = deque()
    for a in xs:
        if s + a != s ^ a:
            while s + a != s ^ a:
                s -= queue.popleft()
                n -= 1
        s += a
        queue.append(a)
        n += 1
        ans += n
    return ans

if __name__ == "__main__":
    n = int(input())
    xs = [int(s) for s in input().split()]

    print(solve(xs))

Solution (Python):
N=int(input())
t=list(map(int,input().split()))
l=1
k=t[0]
a=0
for i in range(N):
    while l<N and k&t[l]==0:
        k+=t[l]
        l+=1
    a+=l-i
    k-=t[i]
print(a)

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

counter = 0

l = 0
r = 0
flag = True
xor = 0
sum = 0

while r < N and l < N:
    if flag:
        xor ^= A[r]
        sum += A[r]
        if xor == sum:
            counter += (r - l + 1)
        else:
            flag = False
    else:
        xor ^= A[l-1]
        sum -= A[l-1]
        if xor == sum:
            counter += r - l + 1
            flag = True

    if flag:
        r += 1
    else:
        l += 1

print(counter)

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

res = 0
sum = 0
r = 0

for l in range(n):
    while r < n and (sum + a[r]) == (sum ^ a[r]):
        sum += a[r]
        r += 1
    res += r - l
    if l == r:
        r += 1
    else:
        sum -= a[l]

print(res)

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

cnt=0
tail=0
sec=0
for head in range(n):
    while(tail<n and sec^a[tail]==sec+a[tail]):
        sec^=a[tail]
        tail+=1
    cnt+=(tail-head)
    sec^=a[head]
print(cnt)

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

right = 0
sum_plus = 0
sum_xor = 0
ans = 0

for left in range(N):
    while right < N and (sum_plus + A[right]) == (sum_xor ^ A[right]):
        sum_plus += A[right]
        sum_xor = sum_xor ^ A[right]
        right += 1
    ans += right - left
    sum_plus -= A[left]
    sum_xor = sum_xor ^ A[left]

print(ans)

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

l, r = 0, 0
xor = 0
while r < N:
  if xor ^ a[r] == xor + a[r]:
    xor = xor ^ a[r]
    ans += r-l+1
    r += 1
  else:
    xor -= a[l]
    l += 1
print(ans)

Solution (Python):
N = int(input())
A_raw = input()
A = [int(i) for i in A_raw.split(' ')]
cnt = 0
first = 0
xsum_old = 0
r_old = 0
for l in range(0, N):
    xsum = xsum_old
    first = A[l]
    cnt += r_old - l
    isBreak = False
    for r in range(r_old, N):
        if xsum & A[r] > 0:
            xsum_old = xsum - first
            r_old = r
            isBreak = True
            break
        xsum += A[r]
        cnt += 1
    if isBreak == False:
        r_old = r
print(cnt)

Solution (Python):


N = int(input())
A = [int(i) for i in input().split()]
ans = 0
formula1 = 0
formula2 = 0
lastRight = 0

for left in range(N):
    rightOut = False
    if left > 0:
        formula1 ^= A[left - 1]
        formula2 -= A[left - 1]
    for right in range(lastRight, N):
        formula1 ^= A[right]
        formula2 += A[right]
        if formula1 != formula2:
            rightOut = True
            formula1 ^= A[right]
            formula2 -= A[right]
            break

    lastRight = max(left, right - rightOut) + 1
    ans += lastRight - left
print(ans)

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

t = a[0]
ans = 0
r = 1

for l in range(n):
  while r<n and t^a[r] == t+a[r]:
    t+=a[r]
    r+=1
  if r==l:
    l=r+1
  ans += r-l
  t -= a[l]

print(ans)

Solution (Python):
n = int(input())
xs = list(map(int, input().split()))
 
ans = 0
sums = xs[0]
xors = xs[0]
i, j = 0, 0

while True:
  if xors == sums:
    ans += j - i + 1
    j += 1
    if j >= n: break
    sums += xs[j]
    xors ^= xs[j]
  else:
    sums -= xs[i]
    xors ^= xs[i]
    i += 1
    
print(ans)

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
my_sum = 0
count = 0
r, l = 0, 0

while r < N:
    boo = my_sum + A[r]
    foo = my_sum ^ A[r]
    if boo == foo:
        my_sum += A[r]
        r += 1
        count += (r - l)
    else:
        my_sum -= A[l]
        l += 1

print(count)

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

cur = a[0]
ans = 0
rp = 1

for lp in range(n):
    while rp < n and cur & a[rp] == 0:
        cur += a[rp]
        rp += 1
    ans += rp - lp
    cur -= a[lp]

print(ans)

Solution (Python):
N = int(input())
A = list(map(int,input().split()))
ans = right = total = 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):
def main():
    n, a = int(input()), list(map(int, input().split()))

    right = 0
    cnt, s = 0, 0

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

        if right == left:
            right += 1
        cnt += right - left
        s -= a[left]

    print(cnt)


if __name__ == '__main__':
    main()

Solution (Python):
N = int(input())
A = [int(i) for i in input().split()]
count = 0
prev_lastidx=-1
initial=0
for i in range(N):
    temp=initial
    for j in range(prev_lastidx+1,N):
        if temp & A[j] == 0:
            temp = temp + A[j]
            prev_lastidx = j
        else:
            prev_lastidx = j-1
            initial = temp - A[i]
            break;
    count = count + (prev_lastidx - i + 1)
print(count)

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

result,right,asum=0,0,0
for left in range(n):
    while n>right and not asum&A[right]:
        asum|=A[right]
        right += 1
    asum -= A[left]
    result += right-left
print(result)

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

result,right,asum=0,0,0
for left in range(n):
    for r in range(right,n):
        if asum&A[right]:
            break
        asum|=A[right]
        right += 1
    asum -= A[left]
    result += right-left
print(result)

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

left = 0
right = 0
sum_list = 0
count = 0

while right < N:
    if sum_list & l[right] == 0:
        sum_list += l[right]
        right += 1
        count += right - left
    elif left == right:
        left += 1
        right += 1
    
    else:
        sum_list -= l[left]
        left += 1

print(count)

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

l, r = 0, 0
sum_a = 0
ans = 0
while r < n:
    
    if sum_a & a[r] == 0:
        sum_a += a[r]
        r += 1
        ans += r - l
    
    elif l == r:
        l += 1
        r += 1
    
    else:
        sum_a -= a[l]
        l += 1
    
print(ans)

Solution (Python):
import copy

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

S=[]
X=[]
for i in range(N):
	if i==0:
		S.append(A[i])
		X.append(A[i])
	else:
		S.append(S[i-1]+A[i])
		X.append(X[i-1]^A[i])


ans=0


for i in range(N):
	ok=copy.deepcopy(i)
	ng=N

	while ng!=ok+1:
		m=(ok+ng)/2
		if i==0:
			if S[m]==X[m]:
				ok=m
			else:
				ng=m
		else:
			if S[m]-S[i-1]==X[m]^X[i-1]:
				ok=m
			else:
				ng=m
	ans+=(ok-i)+1

print ans

Solution (Python):
N=int(input())
A=list(map(int,input().split()))
cur=A[0]
ans=0
rp=1

for lp in range(N):
 while rp<N and cur & A[rp]==0:
  cur+=A[rp]
  rp+=1
 ans+=rp-lp
 cur-=A[lp]

print(ans)

Solution (Python):
def main():
    """
    1 <= N <= 2 * 10^5
    0 <= Ai <= 2^20

    1 <= l <= r <= N

       Al xor Al_1 xor ... xor Ar
    == Al  +  Al_1  +  ... xor Ar
    """
    N = int(input())
    *A, = map(int, input().split())

    editorial(N, A)


def editorial(N, A):
    """
    a + b − (a xor b) = 2 × (a and b)
    （ただしここで andはビットごとの論理積を表す）
    とは？

    xy|xor|(x xor y) xor y
    00|  0|0
    01|  1|0
    10|  1|1
    11|  0|1

    計算量を減らすための工夫
    しゃくとり法
    累積和, 累積XOR
    """
    ans = shakutori(N, A)
    
    print(ans)


def bin_search_TLE(N, A):
    N_BIT = 20
    b = [
        [0] * (N+1)
        for _ in range(N_BIT)
    ]
    
    for j in range(N_BIT):
        for i in range(N):
            x = (A[i] >> j) & 1
            b[j][i+1] = x + b[j][i]

    import bisect
    ans = 0
    for i in range(N):
        tmp = float("inf")
        for j in range(N_BIT):
            
            
            a = 1 + b[j][i]
            x = bisect.bisect_right(b[j][i:], a)
            
            tmp = min(tmp, x - 1)
        
        ans += tmp

    return ans

    for x in b:
        print(*x)


def shakutori(N, A):
    """しゃくとり法
    """
    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]

    return ans


def f(N, A):
    """
    入力例1

    (1,r)=(1,2) => [2,5]
    2: 010
    5: 101
    ------
    7: 111

    XOR: 0が1or偶数個,1が1つ? すべてのbitが立つ?
    """
    pass


def TLE(N, A):
    ans = 0
    for i in range(N):
        for j in range(i+1, N+1):
            a1 = A[i:j]
            s = sum(a1)

            x = a1[0]
            for y in a1[1:]:
                x ^= y
            if x == s:
                ans += 1
            print((i, j), x, s, ans, a1, sep="\t")

    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
from sys import stdin


def main():
    N = int(stdin.readline().rstrip())
    A = [int(x) for x in stdin.readline().rstrip().split()]
    r = 1
    sum_ = A[0]
    count = 0
    for l in range(N):
        while r != N and (sum_ & A[r]) == 0:
            r += 1
            sum_ += A[r - 1]
        count += r - l
        sum_ -= A[l]
    print(count)


if __name__ == '__main__':
    main()

Solution (Python):
def main():
    N = int(input())
    A = list(map(int, input().split()))
    x = 0
    s = 0
    r = 0
    res = 0
    for l in range(N):
        while N > r:
            if s & A[r] != 0: break
            s += A[r]
            r += 1

        s -= A[l]
        res += r - l

    print(res)
if __name__ == '__main__':
    main()

Solution (Python):
def main():
    N = int(input())
    A = list(map(int, input().split()))
    x = 0
    s = 0
    r = 0
    res = 0
    for l in range(N):
        while N > r and x ^ A[r] == s + A[r]:
            x ^= A[r]
            s += A[r]
            r += 1

        x ^= A[l]
        s -= A[l]
        res += r - l

    print(res)
if __name__ == '__main__':
    main()

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

count = 0

xor_temp = 0
sum_temp = 0

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

    xor_temp ^= a[l]
    sum_temp -= a[l]
    count += r - l

print(count)

Solution (Python):


n=int(raw_input())
a=map(int,raw_input().split())
l,r=0,0
res=0
temp=0
while r<n:
 if temp & a[r]==0:
  temp+=a[r]
  r+=1
  res+=r-l
 elif l==r:
  l+=1
  r+=1
 else:
  temp-=a[l]
  l+=1
print res

Solution (Python):
n=int(input())
arr=list(map(int,input().split()))
ans=0
i=0
j=0
sumx=0
while(i<n and j<n):
    if((arr[j] & sumx)==0):
        sumx+=arr[j]
        j+=1
        ans+=j-i
        
    else:
        sumx-=arr[i]
        i+=1
print(ans)

Solution (Python):
from sys import stdin
from itertools import accumulate


def main():
    N = int(stdin.readline().rstrip())
    A = [int(x) for x in stdin.readline().rstrip().split()]
    A1 = [A[0]]
    for i in range(1, N):
        A1.append(A1[-1] ^ A[i])
    A2 = list(accumulate(A))
    l = 0
    r = 1
    tmp1 = A1[0]
    tmp2 = A2[0]
    count = 0
    while True:
        if (l == N - 1) and (r == N):
            count += 1
            break

        if (r != N) and (tmp1 ^ A[r + 1 - 1] == tmp2 + A[r + 1 - 1]):
            if tmp1 == tmp2:
                count += 1
            r += 1
            tmp1 = tmp1 ^ A[r - 1]
            tmp2 = tmp2 + A[r - 1]
            continue
        else:
            if tmp1 == tmp2:
                count += (r - l - 1)
            tmp1 = tmp1 ^ A[l]
            tmp2 = tmp2 - A[l]
            l += 1

    print(count)


if __name__ == '__main__':
    main()

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

cur = A[0]
ans = 0
rp = 1
for lp in range(N):
    while rp < N and cur & A[rp] == 0:
        cur += A[rp]
        rp += 1
    ans += rp - lp
    cur -= A[lp]
    
print(ans)

Solution (Python):
N = int(input())
a = list(map(int, input().split()))
ans = 0
temp = 0
l = 0
r = 0
while r < N:
  if temp ^ a[r] == temp + a[r]:
    temp += a[r]
    ans += r - l + 1
    r += 1
  else:
    temp -= a[l]
    l += 1
print(ans)

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

Ans,xorA,Li,Ri = 0,0,0,0

while Ri < N:
    
    if xorA ^ A[Ri] == xorA + A[Ri]:
        Ans += Ri-Li+1
        xorA ^= A[Ri]
    
    else:
        
        while xorA ^ A[Ri] != xorA + A[Ri]:
            xorA ^= A[Li]
            Li += 1
        xorA ^= A[Ri]
        Ans += Ri-Li+1
    Ri += 1
print(Ans)

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

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

Solution (Python):

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

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

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

l,r,num,ans = 0,0,0,0

while r<N:
        if num + A[r] == num ^ A[r]:
                num ^= A[r]
                r += 1
                ans += r-l
        else:
                num ^= A[l]
                l += 1

print(ans)

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

l,r,num,ans = 0,0,0,0

while r<N:
        if num + A[r] == num ^ A[r]:
                num += A[r]
                r += 1
                ans += r-l
        else:
                num -= A[l]
                l += 1

print(ans)

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

start = 0
x_sum = 0
all = 0

def find(start):
    global all
    all = 1
    if N == 1:
        print(1)
        exit()
    i = start
    x_sum = A[start]
    while 1:
        i += 1
        if x_sum & A[i] == 0:
            x_sum += A[i]
            all += i + 1- start
            if i == N-1:
                break
        else:
            x_sum -= A[start]
            start += 1
            i -= 1

find(0)
print(all)

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

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

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

num_sum = 0
xor_sum = 0
idx = 0
r = 0
count = 0
for l in range(0, N):
    while r < N and num_sum + A_list[r] == xor_sum ^ A_list[r]:
        num_sum += A_list[r]
        xor_sum ^= A_list[r]
        r += 1
    num_sum -= A_list[l]
    xor_sum ^= A_list[l]
    count += (r - l)
print(count)

Solution (Python):
n = int(raw_input())
ls = map(int, raw_input().split())
ct = 0
l = 0
r = 0
x = ls[0]
while l < n:
    while r < n-1:
        if x + ls[r+1] == x ^ ls[r+1]:
            r += 1
            x ^= ls[r]
        else:
            break
    ct += r - l + 1
    x ^= ls[l]
    l += 1
print ct

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
s,xor,cnt,l,r=0,0,0,0,0
for l in range(n):
    while(r<n):
        if s+a[r]==xor^a[r]:
            s+=a[r]
            xor=xor^a[r]
            r+=1
            if r ==n: cnt +=(r-l)*(r-l+1)/2
        else:
            s-=a[l]
            xor = xor & ~a[l]
            cnt +=r-l
            break
print(int(cnt))

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

ans=0
cnt=0
sum=0
for i in range(N):
  while cnt < N:
    if sum&A[cnt]==0:
      sum|=A[cnt]
      cnt+=1
    else:
      break
  ans+=(cnt-i)
  sum&=(~A[i])

print(ans)

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

ans = 0

sum_tmp = A[0]
xor_tmp = A[0]
l = 0
r = 1
while True:
        if r == N:
                ans += (r - l) * (r - l + 1) // 2
                break

        if sum_tmp + A[r] == xor_tmp ^ A[r]:
                sum_tmp += A[r]
                xor_tmp ^= A[r]
                r += 1
                continue

        ans += (r - l)

        if l+1 == r:
                l += 1
                r += 1
                sum_tmp = A[l]
                xor_tmp = A[l]
                continue

        sum_tmp -= A[l]
        xor_tmp ^= A[l]
        l += 1

print(ans)

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

ans = 0

sum_tmp = A[0]
xor_tmp = A[0]
l = 0
r = 1
while True:
        if r == N:
                ans += (r - l)
                if l == N - 1:
                        break
                else:
                        l += 1
                        continue

        if sum_tmp + A[r] == xor_tmp ^ A[r]:
                sum_tmp += A[r]
                xor_tmp ^= A[r]
                r += 1
                continue

        ans += (r - l)

        if l+1 == r:
                l += 1
                r += 1
                sum_tmp = A[l]
                xor_tmp = A[l]
                continue

        sum_tmp -= A[l]
        xor_tmp ^= A[l]
        l += 1

print(ans)

Solution (Python):
def D(n, a):
    n = len(a)
    count = 0
    
    l = 0
    r = 0
    
    sum = 0
    for l in range(n):
        while r < n:
            if sum & a[r] == 0:
                sum += a[r]
                x = sum
                r += 1
            else:
                break
        count += r - l
        sum -= a[l]
        
    return count

n = int(input())
a = list(map(int, input().split()))
print(D(n, a))

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

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

l, r = 0, 0
sum_a = 0
ans = 0
while r < n:
    
    if sum_a & a[r] == 0:
        sum_a += a[r]
        r += 1
        ans += r - l
    
    elif l == r:
        l += 1
        r += 1
    
    else:
        sum_a -= a[l]
        l += 1
    
print(ans)

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

Solution (Python):
def solve(n, a):
    ans, r, v = 0, 0, 0
    for l in range(n):
        while (r < n) and ((v+a[r]) == (v^a[r])):
            v += a[r]
            r += 1
        ans += r-l
        v ^= a[l]
    return ans

n = input()
a = map(int, raw_input().split())
print solve(n, a)

Solution (Python):
from sys import stdin

N = int(stdin.readline().rstrip())
A = [int(x) for x in stdin.readline().rstrip().split()]

sum = 0
xor_sum = 0
cnt = 0
r = 0

for l in range(N):
    while r < N and (sum + A[r] == (xor_sum ^ A[r])):
        sum += A[r]
        xor_sum ^= A[r]
        r += 1
    cnt += r - l
    sum -= A[l]
    xor_sum ^= A[l]

print(cnt)

Solution (Python):
N = int(input())
 
A = list(map(int,input().split()))
gou = A[0]
xor = A[0]
ans_max = 0
ans = 0
a = 0
b = 0
while 1:
  if gou == xor and a <= N-1:
    ans_max += 1
    if a >= N-1:
      a += 1
      continue
    a  += 1
    gou += A[a]
    xor ^= A[a]
  else:
    ans += ans_max
    ans_max -= 1
    gou -= A[b]
    xor ^= A[b]
    b += 1
    if b >= N:
      break
print(ans)

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

l = 0
r = 0
ans = 0
sum = A[0]
xor = A[0]
while True:
    if sum == xor:
        ans += r-l+1
        if r == n-1:
            break
        r += 1
        sum += A[r]
        xor ^= A[r]
    else:
        sum -= A[l]
        xor ^= A[l]
        if l == n-1:
            break
        l += 1

print(ans)

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

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

Solution (Python):
N = int(input())
A = list(map(int,input().split()))
ans = 0
num = 0
a = 0
x = 0
k = 0
for l in range(N):
    for j in range(k,N):
        a += A[j]
        x =  x ^ A[j]
        if a  != x:
            ans += j - l
            k = j
            x = x ^ A[l] ^ A[j]
            a = a - A[l] - A[j]
            break
        elif j == N-1:
            ans += N-l
            k = j
            x = x ^ A[l] ^ A[j]
            a = a - A[l] - A[j]
print(ans )

Solution (Python):
N=int(input())
A=list(map(int,input().split()))
count=0
F=0
B=0
SUM=0



while F<N:
    while True:
        SUMtest=SUM+A[B]
        XORtest=SUM^A[B]

        if SUMtest==XORtest:
            if B==N-1:
                for i in range(F,N):
                    count=count+(N-i)
                F=N
                break
                
            else:
                B+=1
                SUM=SUMtest

        else:
            count=count+(B-F)
            SUM=SUM-A[F]
            F+=1
            break


print(count)

Solution (Python):
def d_xor_sum_2(N, A):
    r = 0  
    total, xor_sum = 0, 0  
    ans = 0

    for l in range(N):  
        while r < N and (total + A[r] == xor_sum ^ A[r]):
            
            total += A[r]
            xor_sum ^= A[r]
            r += 1  
        ans += r - l  
        
        total -= A[l]
        xor_sum ^= A[l]  
        
        
    return ans

N = int(input())
A = [int(i) for i in input().split()]
print(d_xor_sum_2(N, A))

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

j = 0
s = 0; t = 0
ans = 0
for i in range(N):
    while j < N and (s + A[j]) == (t ^ A[j]):
        s += A[j]; t ^= A[j]
        j += 1
    s -= A[i]; t -= A[i]
    ans += j-i
    if i == j:
        j += 1
print(ans)

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

ans = 0
s = [0]
xs = [0]

for i in x:
    s.append(s[-1]+i)
    xs.append(xs[-1]^i)

def search(start, ok, ng):
    if abs(ok-ng) <= 1:
            return ok
    mid = (ok+ng) // 2
    if s[mid] - s[start] == xs[mid] ^ xs[start]:
        return search(start, mid, ng)
    else:
        return search(start, ok, mid)

for i in range(n+1):
    ans += search(i, i, n+1) - i
print(ans)

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

ans = 0
s = [0]
xs = [0]

for i in x:
    s.append(s[-1]+i)
    xs.append(xs[-1]^i)

def search(start, ok, ng):
    if abs(ok-ng) <= 1:
        if ok == ng:
            return ok
        mid = ok + 1
        if s[mid] - s[start] == xs[mid] ^ xs[start]:
            return mid
        else:
            return ok
    mid = (ok+ng) // 2
    if s[mid] - s[start] == xs[mid] ^ xs[start]:
        return search(start, mid, ng)
    else:
        return search(start, ok, mid)

for i in range(n+1):
    ans += search(i, i, n) - i
print(ans)

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

ans = 0
r = 0
s_sum = 0
x_sum = 0
for l in range(n):  
    while r < n and x_sum & a_list[r] == 0:  
        s_sum += a_list[r]
        x_sum ^= a_list[r]
        r += 1
    ans += r - l  
    s_sum -= a_list[l]  
    x_sum ^= a_list[l]  

print(ans)

Solution (Python):


import math
import sys
import itertools
import numpy as np

n =int(input())
a = [int(i) for i in input().split()]
answer = 0
asum = a[0]
xsum = a[0]
r=0
for l in range(0,n):
    while r < n-1:
        asum = asum + a[r+1]
        xsum = xsum ^ a[r+1]

        if asum == xsum:
            r += 1
        else:
            asum = asum - a[r+1]
            xsum = xsum ^ a[r+1]
            break


    answer += (r-l + 1)
    asum = asum - a[l]
    xsum = xsum - a[l]

print(answer)

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

i = 1
j = 0
c = a[0]
ans = n
num = 0

while i <= n-1:
    if c ^ a[i] == c + a[i]:
        c = c ^ a[i]
        num += 1
        ans += num
        i += 1
    else:
        c = c & ~a[j]
        j += 1
        num -= 1

print(ans)

Solution (Python):
if __name__ == '__main__':
    N = int(raw_input())
    A = map(int, raw_input().split())
    head = 0
    tail = 0
    num_2 = 0
    num_10 = 0
    ans = 0
    now_len = 0
    while(head < N):
        num_2 = num_2 ^ A[head]
        num_10 += A[head]
        if num_2 == num_10:
            head += 1
            now_len += 1
            ans += now_len
        else:
            num_2 ^= A[tail]
            num_10 -= A[tail]
            num_2 ^= A[head]
            num_10 -= A[head]
            tail += 1
            now_len -= 1

    print(ans)

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

ans = 0
sum_a = 0
r = 0
for l in range(n):
    while r < n:
        x = a[r]
        if sum_a & x != 0:
            break
        sum_a += x
        r += 1
    ans += r - l
    sum_a -= a[l]
print(ans)