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 deque

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

buf = deque()
tmp = 0
ans = 0

for a in aaa:
    while tmp & a:
        rm = buf.popleft()
        tmp ^= rm
    buf.append(a)
    tmp ^= a
    ans += len(buf)
print(ans)

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

Range = []
x = a[0]
y = a[0]
end = 0
for i in range(n):
    start = i
    while x == y:
        end = end + 1
        if end >= n:
            end = end - 1
            break
        x = x + a[end]
        y = y ^ a[end]
    else:
        x = x - a[end]
        y = y ^ a[end]
        end = end - 1
    Range.append([start, end])
    x = x - a[i]
    y = y ^ a[i]
    if start == end and end < n - 1:
        end = end + 1
        x = x + a[i + 1]
        y = y ^ a[i + 1]

ans = 0
for i in range(len(Range)):
    ans = ans + Range[i][1] - Range[i][0] + 1
print(ans)

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

xors=0
sums=0
r=0
for l in range(n):
    while (r != n and xors^A[r] == sums+A[r]):
        sums += A[r]
        xors ^= A[r]
        r += 1
    count+=r-l
    xors^=A[l]
    sums-=A[l]
print(count)

Solution (Python):
from collections import deque
N = int(input())
A = list(map(int,input().split()))
r = -1
a = 0
b = 0
n = 0
for l in range(N):
    for i in range(r+1,N):
        bb = bin(A[i]).count("1")
        if bin(a+A[i]).count("1")==b+bb:
            a += A[i]
            b += bb
            r = i
            n += r - l + 1
        else:
            break
    a -= A[l]
    b -= bin(A[l]).count("1")
    if r == N-1:
        break
print(n)

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

f = {}
right = 0
accum = A[0]
for left in range(N):
    if right < left:
        right = left
        accum = A[left]

    while(right < N-1):
        if accum & A[right+1] == 0:
            accum += A[right+1]
            right += 1
        else:
            break

    f[left] = right
    accum -= A[left]

answer = 0
for i in range(N):
    answer += f[i] - i + 1

print(answer)

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

l = 0 
r = 0 
S = 0 
X = 0 

ans = 0
while l<N:
    if S==X and r<=N:
        if r<N:
            S += A[r]
            X ^= A[r]
        r += 1
    else:
        S -= A[l]
        X ^= A[l]
        ans += r - l - 1
        l += 1

print(ans)

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

sum = 0
ans = 0
i = 0
j = 0
while True:
    if i < N and (sum | A[i] == sum + A[i]):
        sum += A[i]
        i += 1
        ans += i-j
    elif i < N:
        while sum | A[i] != sum+A[i]:
            sum -= A[j]
            j += 1
    else:
        break
print(ans)

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

j, s, tot=0, 0, 0
for i in range(n):
  while j<n:
    if (s+a[j])==(s^a[j]):
      s+=a[j]
      j+=1
    else:
      break
  tot+=(j-i)
  s-=a[i]

print(tot)

Solution (Python):
N=int(input())
A=[int(i) for i in input().split()]
dp=[0 for i in range(N)]
AL=A[0]
AR=A[0]
flag0=0
for i in range(1,N):
    AL=AL^A[i]
    AR=AR+A[i]
    if AL!=AR:
        dp[0]=i
        AL=A[i]^AL
        AR-=A[i]
        flag0=1
        break
if flag0==0:
    dp[0]=N
for i in range(1,N):
    AL=AL^A[i-1]
    AR=AR-A[i-1]
    flag=0
    for j in range(dp[i-1],N):
        AL=AL^A[j]
        AR=AR+A[j]
        if AL!=AR:
            dp[i]=j
            AL=A[j]^AL
            AR-=A[j]
            flag=1
            break
    if flag==0:
        dp[i]=N
print(sum(dp)-(N*(N-1))//2)

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

Mat = [[float('inf')] * (N - 1) + [N] for d in range(20)]
prev = [-1] * 20

for i, A in enumerate(As):
    binA = format(A, 'b').zfill(20)
    for d in range(20):
        if binA[d] == '1':
            if prev[d] != -1:
                Mat[d][prev[d]] = i
            prev[d] = i

minM = list(map(min, *Mat))

for i in reversed(range(N - 1)):
    minM[i] = min(minM[i], minM[i + 1])

print(sum(minM) - N * (N - 1) // 2)

Solution (Python):
from collections import defaultdict

N = int(input())

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

BIT = defaultdict(int)

ans = 0
start = 0
for i in range(1,N+1):
    a = A[i]
    bit = 0
    before = 0
    
    while(a>0):
        bit += 1
        if a%2==1:
            if BIT[bit]>before:
                before = BIT[bit]
            BIT[bit] = i
        a //= 2

    
    if before>start:start=before
    ans += i-start
    
print(ans)

Solution (Python):




C = {}


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

    C[1] = 1
    comb = 1
    for n in range(2, N):
        C[n] = comb
        comb += n

    ANS = 0
    x = 0
    s = 0
    length = 0
    R = 0

    for L in range(N):
        x ^= A[L]
        s += A[L]
        length += 1

        if x == s:
            ANS += length - 1

        else:
            while R <= L:
                R += 1
                x ^= A[R - 1]
                s -= A[R - 1]
                length -= 1

                if x == s:
                    ANS += length - 1
                    break
    ANS += N
    print(ANS)


if __name__ == '__main__':
    main()

Solution (Python):
def main():
    n = int(input())
    aa = list(map(int, input().split()))
    pairs = []
    c = 0
    i = j = 0
    while j < n:
        a = aa[j]
        if c & a:
            pairs.append([i, j])
            for ii in range(i, j):
                c ^= aa[ii]
                if c & a == 0:
                    i = ii + 1
                    break
        else:
            c |= a
            j += 1
    pairs.append([i, n])
    end = 0
    ans = 0
    for i, j in pairs:
        d = j - i
        ans += d * (d + 1) // 2
        if i < end:
            d = end - i
            ans -= d * (d + 1) // 2
        end = j
    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
import math
def solve():
    global N, A, PA, XA, i
    N = int(input())
    A = list(map(int, input().split()))

    PA = [0]*(N+1)
    XA = [0]*(N+1)
    PA[0] = A[0]
    XA[0] = A[0]

    for i in range(1,N):
        PA[i] = PA[i-1] + A[i]
        XA[i] = XA[i-1] ^ A[i]

    count = 0
    for i in range(N):
        r = bs(i, N, solver) - 1
        count += r-i+1
    print(count)

def bs(ng, ok, solver):  
    """
    input:
        ng: max number known to be ng. default -1
        ok: min number known to be ok. default max number + 1
    output:
        the number that is OK firstly
    """
    while math.fabs(ok-ng) > 1:
        mid = (ok+ng)//2
        if solver(mid):
            ok = mid
        else:
            ng = mid
    return ok

def solver(x):
    if PA[x] - PA[i-1] == XA[x] ^ XA[i-1]:
        return False
    else:
        return True

if __name__ == "__main__":
    solve()

Solution (Python):


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

l=0
r=0
ans=0

xsum=0
s=0

while(l < n):

    if s == xsum and r <= n:
        if r < n:
            s += a[r]
            xsum ^= a[r]
        r+=1
    else:
        s -= a[l]
        xsum ^= a[l]
        ans += r-l-1
        l+=1


print(ans)

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

ans = 0
f = 0
t = -1
tmp = 0
while f < N:
  nt = t + 1
  if f == nt or (nt < N and tmp + A[nt] == tmp ^ A[nt]):
    tmp += A[nt]
    t = nt
  else:
    l = nt - f
    ans += l
    tmp -= A[f]
    f += 1
print(ans)

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

counter = [-1 for _ in range(20)]
ret = 0
prv = 0
for i in range(N):
    ai = A[i]
    diff = prv + 1
    ai_b = format(ai, '020b')
    for j in range(20):
        bit = ai_b[j]
        if bit == '1':
            if counter[j] != -1:
                diff = min(diff, i - counter[j])
            counter[j] = i
    prv = diff
    ret += diff
print(ret)

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

sums = [0] * (n + 1)
xors = [0] * (n + 1)

for i in xrange(n):
    sums[i + 1] = sums[i] + nums[i]
    xors[i + 1] = xors[i] ^ nums[i]

results = 0

for i in xrange(0, n):
    left, right = i + 1, n + 1
    while right - left > 1:
        mid = (left + right) / 2
        if sums[mid] - sums[i] == xors[mid] ^ xors[i]:
            left = mid
        else:
            right = mid
    results += left - i

print results

Solution (Python):


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

A = list(map(lambda x: bin(x)[2:].zfill(20),
             A))

admissible = [N-1] * N

def update(i, adm):
    if adm < admissible[i]:
        admissible[i] = adm

for k in range(20):
    
    prev = []
    curr = []
    for i in range(N):
        
        
        
        curr.append(i)
        if A[i][k] == "1":
            for j in prev:
                update(j, i-1)
            prev = curr
            curr = []



ans = 0
for i in range(N):
    ans += admissible[i] - i + 1
print(ans)

Solution (Python):
import numpy as np

def ii():
    return int(input())


def lii():
    return list(map(int, input().split(' ')))


def lvi(N):
    l = []
    for _ in range(N):
        l.append(ii())
    return l


def lv(N):
    l = []
    for _ in range(N):
        l.append(input())
    return l


def main():
    N = ii()
    A = lii()
    A = [-1] + A  

    ans = 0
    p = [0 for i in range(20)]
    min_l = 0
    for i in range(1, N+1):
        m = i
        for d in range(20):
            if (A[i] >> d) % 2 == 1:
                m = min(m, i - p[d])
                p[d] = i
        min_l = max(min_l, i  - m)
        m = min(m, i - min_l)
        
        ans += m

    return ans


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

Solution (Python):
n = int(input()) + 1
a = list(map(int, input().split())) + [2**21-1]

cnt = 0
l = 0
r = 0
s = 0
x = 0
while l < n-1:
    while r < n and s == x:
        s += a[r]
        x ^= a[r]
        r += 1
    cnt += (r-l - 1)
    s -= a[l]
    x ^= a[l]
    l += 1
print(cnt)

Solution (Python):
N = input()
As = map(int,raw_input().split())

msk = 0
sm = 0
l = 0
r = 0

while r < N:
    while As[r]&msk != 0:
        msk^=As[l]
        l+=1
    msk|=As[r]
    sm+=(r-l+1)
    r+=1
print sm

Solution (Python):

from itertools import accumulate
def inpl(): return map(int, input().split())

N = int(input())
A = list(inpl())
l = 0
r = 0
S = 0
ans = 0

while l < N and r < N:
    while r < N:
        if S + A[r] == S ^ A[r]:
            S += A[r]
            ans += (r-l+1)
            r += 1
        else:
            break
    if r > l:
        S -= A[l]
        l += 1
        
    else:
        r += 1
        l += 1

print(ans)

Solution (Python):

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

counts = [0] * 20

res = 0
left = 0
right = 0

r = bin(A[right])
for i in range(len(r)-2):
    counts[i] += int(r[-(i+1)])

while left < N and right < N:
    if max(counts) >= 2:
        r = bin(A[left])
        for i in range(len(r)-2):
            counts[i] -= int(r[-(i+1)])
        left += 1
    else:
        res += right - left + 1
        
        right += 1
        if right < N:
            r = bin(A[right])
            for i in range(len(r)-2):
                counts[i] += int(r[-(i+1)])

print(res)

Solution (Python):
n = int(input())
tmp = list(map(int,input().split()))
now = 0
nowsum = 0
start = 0
end = 0
ans = 0
while(True):
    if end == n:
        print(ans)
        quit()
    if tmp[end] ^ nowsum == nowsum + tmp[end]:
        now += 1
        ans += now
        nowsum += tmp[end]
        end += 1
    else:
        nowsum -= tmp[start]
        start += 1
        now -= 1

Solution (Python):
from sys import stdin
from collections import deque
from itertools import repeat
def main():
    n = int(stdin.readline())
    a = map(int, stdin.readline().split(), repeat(10, n))
    ans = 0
    q = deque()
    p = 0
    for i, x in enumerate(a):
        while p & x:
            p ^= q.popleft()[1]
        q.append((i, x))
        ans += i - q[0][0] + 1
        p ^= x
    print ans
main()

Solution (Python):
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**15
mod = 10**9+7

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)


def main():
    n = I()
    a = LI()
    r = 1
    rr = 0
    x = a[0]
    for l in range(n):
        while True:
            if r == n:
                break
            if (x & a[r]) > 0:
                break
            x += a[r]
            r += 1
        x -= a[l]
        rr += r - l

    return rr




print(main())

Solution (Python):

import sys

input=sys.stdin.readline

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

ans=0

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