We need to solve: Given an array A of N integers, compute sum over all L,R with 1 ≤ L ≤ R ≤ N of XOR of subarray A[L..R] i.e., F(L,R) = A[L] xor A[L+1] xor ... xor A[R]. We need the total sum.
We have up to T test cases up to 100k, but total N across test cases ≤ 100k. So overall O(N log maxA) or O(N * bit_count) is fine.
We need to compute sum of all subarray XORs.
We can approach by bits: For each bit position (0 to up to 30, since A[i] ≤ 1e9 < 2^30), we can compute how many subarrays have that bit set in their XOR result. Then total sum = sum over bits ( (count of subarrays with bit=1) << bit ).
So the problem reduces: For each bit, given an array of bits (0/1) from A (the bit at that position), compute the number of subarrays whose XOR has value 1 (i.e., odd number of 1s). Since XOR of bits is just parity of number of ones.
So we need, for a binary array b[1..N] (each 0 or 1), count number of subarrays (L,R) such that sum of b[L..R] is odd. That's a classic problem: Using prefix XOR. Define prefix parity p[i] = XOR of b[1..i] = (sum mod 2). p[0]=0. Then for subarray L..R, XOR = p[R] xor p[L-1] = 1 if p[R] != p[L-1]. So number of subarrays with XOR=1 is number of pairs (i<j) with p[i]!=p[j] (where i = L-1, j = R). That equals count0 * count1, where count0 = number of indices (including 0) with p=0, count1 = number with p=1.
Thus for each bit, we can compute count1 for that bit: Let cnt0 = 1 initially (prefix 0 before start), cnt1=0. Iterate through array, maintain current prefix parity, update counts, and for each element, after updating prefix, we can add to total number of subarrays ending at current position? Actually the total number of subarrays with odd XOR is sum over R of number of L such that p[L-1] != p[R]. That is, for each R, if p[R]=0 then add cnt1, else add cnt0. So we can accumulate.
But careful: For each bit, we need the count of subarrays where overall XOR has that bit = 1. So we can compute separately per bit.
Complexity: O(N * max_bit) = O(N * 30) = about 3e6, which is fine given total N=100k -> 3 million, plus T overhead. Acceptable.
But we can also do some optimization: For each element, we can iterate over set bits, but that might also be fine. However per-bit approach is simpler.
Edge Cases: N up to 1e5, values up to 1e9, so up to 30 bits (maybe 31 since 2^30 = 1073741824 > 1e9, so bits 0..29, maybe 30?). Actually 2^30 = 1073741824 > 1e9, so max bit is 29 (2^29=536870912). But to be safe we can go up to 31 (0..30). We'll use 0..30 inclusive? Since 1e9 < 2^30, the highest bit index is 29. But we can do 0..30. However we must ensure we cover all bits: 31 bits? Actually 10^9 in binary is about 30 bits (2^30 is 1,073,741,824). So bits 0..29 inclusive (since 2^29=536,870,912). So 30 bits total. To be safe, we could go up to 31 (0..30) because 2^30=1073741824, that's > 1e9, but still fine. Actually we can compute maximum bit needed: while ((1<<bit) <= max(A[i])) bit++; So we can find max element across all test cases? But we have multiple test cases and total N small, we can just compute per test case: find maxVal, set bits up to floor(log2(maxVal)) + 1. Or simply set to 31 (0..30) since total operations not heavy. But careful: 1e9 < 2^30, so bit index up to 29 inclusive. If we go up to 30 (i.e., 31 bits if we count 0..30), then bit 30 will always be zero, so count0 = N+1, count1 = 0 -> no subarrays. That's fine but wasteful. But given total N small, it's okay to loop over 31 bits. I'll use 31 bits: for bit = 0 to 30 inclusive.
Now we need to be careful about integer overflow: The total sum can be up to? N up to 1e5, number of subarrays = N(N+1)/2 ~ 5e9. Each XOR can be up to ~ 2^30 -1 ~ 1e9. So total sum could be up to ~5e9 * 1e9 = 5e18, which fits in 64-bit (2^63-1 ≈ 9e18). So we need 64-bit integer (long long in C++, int64 in Python). Python int is arbitrary precision, so fine.
Now we implement per test case:
Read N, list A.
Set answer = 0
For bit in range(31):
cnt0 = 1 (prefix parity 0 at index 0)
cnt1 = 0
parity = 0
for val in A:
b = (val >> bit) & 1
parity ^= b
if parity == 0:
answer += cnt1 * (1 << bit) # but careful: we can accumulate counts first then multiply? We need to add (1 << bit) times number of subarrays with odd XOR at this bit. Better: compute total count for this bit then add count * (1 << bit). More efficient: For each bit, compute count_odd = 0; then for each prefix update, after updating parity, if parity==0: count_odd += cnt1; else: count_odd += cnt0; then update cnt0/ cnt1 accordingly (increment appropriate counter). Then after loop, total_odd is the number of subarrays with XOR bit=1. Then answer += total_odd << bit.
But careful: The standard algorithm: maintain cnt0 and cnt1 of prefix parities seen so far. Initially we have p[0]=0 => cnt0=1, cnt1=0. For each index i (1-indexed), after processing A[i], compute new parity = previous parity XOR b. Then number of subarrays ending at i with odd XOR = if parity==0 then cnt1 else cnt0. Add that to total_odd. Then increment the count for the new parity: if parity==0 then cnt0++ else cnt1++.
Yes.
Thus per bit:
cnt0 = 1
cnt1 = 0
parity = 0
total_odd = 0
for each a in A:
b = (a >> bit) & 1
parity ^= b
if parity == 0:
total_odd += cnt1
cnt0 += 1
else:
total_odd += cnt0
cnt1 += 1
answer += total_odd << bit
We can also combine loops: For each element, we could update all bits at once, but simpler to do per bit.
Complexity: O(N * B) where B = 31. With total N = 1e5, ~3.1 million operations, plus overhead for Python loops, should be fine. However T up to 100k, but total N is limited. For many small test cases (N=1), the per-bit loops might be overhead but still total operations overall: For each test case we do B * N operations, but total N sum is 1e5, so total operations = B * (total N) = ~3.1e6, plus overhead of test cases. So fine.
But we must be careful about Python's performance with nested loops: for each test case, for bit in range(31): for a in A: ... That's 31 * total N loops ~ 3.1 million, which is perfectly fine.
Alternatively, we can process all bits in one pass: maintain for each bit the cnt0, cnt1, parity, total_odd. But that's similar in operations.
Let's check example: N=2, A=[1,2] => bits: For bit0 (1<<0=1): A bits: 1,0 => b: 1,0. Start: cnt0=1, cnt1=0, parity=0. a=1: b=1 => parity ^=1 => parity=1. Since parity=1: total_odd+=cnt0=1, cnt1=1. Next a=2: b=0 => parity stays 1. Since parity=1: total_odd+=cnt0=1 -> total_odd=2, cnt1=2 (since parity=1: cnt1++ => 2). So total_odd=2 => contribute 21=2. For bit1 (1<<1=2): A bits: 0,1 => b:0,1. Start parity=0, cnt0=1, cnt1=0. a=1: b=0 -> parity=0 -> total_odd+=cnt1=0, cnt0=2. a=2: b=1 -> parity=1 -> total_odd+=cnt0=2, cnt1=1. total_odd=2 => 22=4. For bits >=2: all zeros, total_odd=0. Sum=2+4=6. Matches.
Thus solution works.
Edge Cases: N=1, A=[0] => only subarray: XOR=0 => sum=0. For bit0: b=0 => parity remains 0; after first step, total_odd: parity=0 => add cnt1=0, cnt0 becomes 2; total_odd=0 => answer 0.
N=1, A=[5] => only subarray: 5. Bits: For each bit where 5 has 1 (bits0 and2): we should get count_odd=1. Our algorithm: For bit0: b=1 -> parity=1 -> total_odd+=cnt0=1 => total_odd=1, cnt1=1 => total_odd=1 => contribute 1. For bit2: similarly.
Works.
Now we must handle T up to 100k. Input reading in Python should be efficient: sys.stdin.read() and split.
Now we need to produce output per test case.
Thus we can write:
import sys
def solve():
data = sys.stdin.buffer.read().split()
it = iter(data)
t = int(next(it))
out = []
for _ in range(t):
n = int(next(it))
A = [int(next(it)) for _ in range(n)]
ans = 0
# find max bit needed to reduce loops? but 31 is fine.
max_val = max(A) if A else 0
bits = max_val.bit_length() # number of bits needed to represent, e.g., 0 -> 0 bits, but we still may need to consider? Actually if max_val=0, all numbers zero, answer=0, so we can skip loops. So we can compute bits and if bits==0: answer=0.
if max_val == 0:
out.append('0')
continue
for bit in range(bits): # bit from 0 to bits-1 inclusive
cnt0 = 1
cnt1 = 0
parity = 0
total_odd = 0
for a in A:
b = (a >> bit) & 1
parity ^= b
if parity == 0:
total_odd += cnt1
cnt0 += 1
else:
total_odd += cnt0
cnt1 += 1
ans += total_odd << bit
out.append(str(ans))
sys.stdout.write('\n'.join(out))
if name == "main":
solve()
But careful: max_val.bit_length() returns number of bits needed to represent max_val. For max_val=0, returns 0, so loop is skipped, ans stays 0. That's okay. For max_val>0, it will be up to 30. That's good.
But note: If some numbers have bits beyond max_val? Actually max_val is the maximum, so all bits above that are zero. So it's safe.
Now test with given example.
Ok.
Now we should think about alternative approach or edge cases: For each bit we are counting all subarrays with odd parity. This is correct.
Now check for potential overflow in Python? Not an issue.
Now one more detail: Since we are adding total_odd << bit, and total_odd can be up to N(N+1)/2 ≈ 5e9, shifting left bit (max 29) yields up to 5e9 * 2^29 ~ 2.68e18, which fits within Python int.
Now complexity: O(total_N * max_bit) with max_bit ≤ 30. So good.
Now we have to make sure that reading input is correct: The total N sum ≤ 100k, but T can be large, so we must read efficiently.
Implementation as above is fine.
But one nuance: When we compute bits = max_val.bit_length(), for each test case, we may have max_val small, which reduces loops. However if max_val is 1, bits=1 (bit0 only). That's good.
Now we must verify that if max_val=0, we output 0. Yes.
Now let's think about correctness proof: As described.
Now we can consider potential pitfalls: The prefix parity counting method counts subarrays (L,R) where L <= R. Starting with cnt0=1 accounts for prefix before index 1. The loop for each index i (1-indexed) processes element A[i] and updates parity after including it. Then the number of subarrays ending at i with odd XOR is count of prefix parity opposite to current parity among indices < i (including 0). That's exactly as we do. Then we increment count for current parity. So total_odd over all i equals number of subarrays with XOR having that bit set.
Thus correct.
Now consider test with all zeros: All bits zero, total_odd=0, ans=0.
Now consider N=100000, A all random. Works.
Now double-check: For bit0, we computed total_odd correctly. For each subarray, XOR's bit0 is 1 if number of ones odd. That's captured.
Now we can try some more examples manually:
N=3, A=[1,2,3] => A:1,2,3. Subarrays: [1]=1, [2]=2, [3]=3, [1,2]=1 xor2=3, [2,3]=2 xor3=1, [1,2,3]=1 xor2 xor3=0. Sum=1+2+3+3+1+0=10. Let's compute via bits: bit0: (1,0,1) => subarrays with odd parity: count? prefix parity: after 0:0; after1:1; after2:1; after3:0. Subarrays ending at: i=1: parity=1 -> cnt0=1 => odd=1; i=2: parity=1 -> cnt0=1 => odd=1; i=3: parity=0 -> cnt1=2? Wait compute: Initially cnt0=1,cnt1=0. i1: parity=1 -> total_odd+=cnt0=1 -> cnt1=1. i2: b=0, parity stays 1 -> total_odd+=cnt0=1 -> total_odd=2, cnt1=2. i3: b=1, parity=0 -> total_odd+=cnt1=2 -> total_odd=4, cnt0=2? Actually after i3: parity=0, we add cnt1=2 => total_odd=4, then cnt0++. So total_odd=4 => contribution 41=4. bit1: A bits: (0,1,1) => prefix parity: after0:0; after1:0; after2:1; after3:0. total_odd: i1: parity=0 -> add cnt1=0 => cnt0=2; i2: b=1, parity=1 -> add cnt0=2 => total_odd=2, cnt1=1; i3: b=1, parity=0 -> add cnt1=1 => total_odd=3, cnt0=3. So total_odd=3 => contribution 32=6. bit2: A bits: (0,0,0?) Actually 1 (001),2(010),3(011): bit2 is 0 for all => total_odd=0. Sum=4+6=10. Correct.
Now note: There's also bit2 for numbers like 4 etc. Works.
Now possible improvement: Instead of per bit loops, we could do a single pass using bitwise operations with prefix XORs? There is known method using contribution of each bit to answer using prefix XOR and counts per bit. That's essentially the same.
Now finalize.
But caution: In Python, using for bit in range(bits) inside for test case may be okay. But we can also precompute for each test case all bits in parallel: For each element, iterate through its bits set, but we need to track parity for each bit. That would also be O(N * popcount(A[i])) which on average maybe less than 30? But worst-case each A[i] can have 30 bits set, still O(N*30). So similar.
Given total N is 100k, 3 million operations is fine.
Now we must handle many test cases with small N: e.g., T=100k, N=1 each. Then total N=100k. Our algorithm: For each test case, we compute bits = max_val.bit_length() (which is maybe 1 for value 1, or 0 for 0). So per test case we do bits * N operations. Since N=1, bits maybe up to 30 if max_val=10^9, but that's only for test cases with max_val high. If all test cases have N=1 and max_val=10^9, then total operations = sum(bits) per test case, where bits maybe 30 each, sum bits * N = 30 * 100k = 3 million. Still okay. So performance is fine.
Now we should also consider that Python's loops overhead for small arrays many times may be non-negligible but still within limits because total operations ~3 million; each operation simple.
However, we need to be careful about the inner loop using Python's integer bit shifts and bitwise AND; but that's fast.
Thus solution seems solid.
Now we produce final answer.
But also consider that the problem may be from CodeChef (XORSUB?). Usually the problem "Polo the Penguin and the XOR" or something. The input constraints total N ≤ 100,000, T up to 100,000. So our solution fits.
Now I think we are done.
But I recall an alternative way: For each bit, number of subarrays with XOR bit 1 is count0 * count1, but careful: Is it exactly count0 * count1? If we have prefix parity array p[0..N], with p[0]=0. Then number of pairs (i<j) with p[i]!=p[j] equals (# of zeros)(# of ones). However, we must ensure that i < j, where i = L-1, j = R. That's exactly the number of subarrays with odd XOR. So we could compute total_odd = cnt0 * cnt1. Wait, is that the total? Let's test: For the earlier example bit0: b=[1,0] -> prefix parities: p0=0, p1=1, p2=1. cnt0: indices with parity 0: {0} ->1; cnt1: indices with parity 1: {1,2} ->2; product = 2. That matches total_odd=2. For bit1: b=[0,1] -> p0=0, p1=0, p2=1 -> cnt0=2, cnt1=1 -> product=2? But we computed total_odd=2 earlier? Actually earlier we computed total_odd=2? Let's recalc: For bit1, we computed total_odd=2? Actually we had: For bit1 (value 2) we had total_odd=2? In the example N=2 A=[1,2] we got total_odd=2 for bit1? Let's revisit: For bit1, we had bits: A1=0, A2=1. Prefix: p0=0; after A1 (0) -> p1=0; after A2 (1) -> p2=1. Subarrays: [1,1] (L=1,R=1): XOR=0 -> bit1=0; [2,2]: A2=2 -> bit1=1; [1,2]: XOR of 1 and 2 =3 -> binary 11, bit1=1? Actually 3 has bits 0 and 1 set, so bit1=1. So subarrays with bit1 set: [2,2] and [1,2] -> 2 subarrays. So total_odd=2. cnt0=2 (indices 0,1), cnt1=1 (index2) -> product = 2. So yes. For the N=3 example earlier: bit0: b=[1,0,1]; prefix: p0=0, p1=1, p2=1, p3=0; cnt0: indices 0,3 -> 2; cnt1: indices 1,2 -> 2; product=4. That's total_odd=4. bit1: b=[0,1,1]; prefix: p0=0, p1=0, p2=1, p3=0; cnt0: indices 0,1,3 ->3; cnt1: index2 ->1; product=3. So indeed total_odd = cnt0 * cnt1. That's simpler: Just compute counts of prefix parities (0 and 1) for each bit, then total_odd = cnt0 * cnt1. But careful: In this counting, we are counting all pairs (i,j) with i<j? Because product cnt0cnt1 counts ordered pairs where one is 0 and the other is 1, but without ordering it counts each unordered pair twice? Actually if we count all pairs (i,j) with p[i]=0, p[j]=1, that's cnt0 * cnt1. But we require i < j (i = L-1, j = R). However cnt0cnt1 counts all combinations (i from zeros, j from ones) but includes also those with i > j. But since we need i < j, it's not symmetric unless we consider order. Let's examine: For a given unordered pair of indices (i,j) with i<j, if p[i] != p[j], then exactly one of (i,j) will be counted if we count (i from zeros, j from ones) provided we respect ordering? cnt0cnt1 counts all ordered pairs where the first is a zero-index and the second is a one-index? Actually cnt0cnt1 as a product counts number of ways to choose one zero index and one one index, without imposing order (it's the number of unordered pairs if we consider combination? Typically, the number of ways to select one element from set of zeros and one from set of ones is |Z| * |O|, and that counts each unordered pair exactly once because you specify which set the first selection comes from? Actually when we say "choose a zero index and a one index", it's an ordered selection in the sense that we are selecting one from zeros and one from ones, but the pair (zero_index, one_index) is ordered in the sense that the first chosen is zero, second is one. That yields all pairs where zero index is first, one index second. But what about pairs where zero index > one index? That would be (zero_index, one_index) with zero_index > one_index, which is still a valid pair, but we need only those with i < j. But if p[i]=0 and p[j]=1 and i<j, then (i,j) is counted as a zero-first then one-second pair. If p[i]=1 and p[j]=0 with i<j, that's not counted as zero-first one-second; it would be counted if we allowed (one, zero) but we are not counting those. So cnt0cnt1 counts only those pairs where the zero index is the first element and the one index is the second. However we need pairs where p[i] != p[j] and i<j. That can be either (zero, one) with i<j or (one, zero) with i<j. So total number = (number of pairs where i<j, p[i]=0, p[j]=1) + (number of pairs where i<j, p[i]=1, p[j]=0). That is symmetric: For any unordered pair (i,j) with i<j, if they are different, it contributes exactly one to either term. So total number = (# of pairs with i<j and p[i]=0,p[j]=1) + (# of pairs with i<j and p[i]=1,p[j]=0). This is equal to? Let’s denote Z = set of indices with p=0, O = set with p=1. Number of ordered pairs (i,j) with i<j, i in Z, j in O is something like: For each zero index i, number of one indices j > i. That's not simply |Z||O|) because:
Total pairs = (|Z|+|O|)(|Z|+|O|-1)/2 = (N+1)(N)/2.
Same pairs = |Z|(|Z|-1)/2 + |O|(|O|-1)/2.
Difference = [ (|Z|^2 + 2|Z||O| + |O|^2 - (|Z|+|O|) ) - (|Z|^2 - |Z| + |O|^2 - |O|) ] /2 = [ 2|Z||O| - (|Z|+|O|) + (|Z|+|O|) ] /2 = (2|Z||O|)/2 = |Z||O|. Indeed it simplifies! Let's verify:
Total - Same = (|Z|+|O|)(|Z|+|O|-1)/2 - (|Z|(|Z|-1) + |O|(|O|-1))/2 = [ (|Z|+|O|)(|Z|+|O|-1) - (|Z|^2 -|Z| + |O|^2 -|O|) ] /2 = [ (|Z|^2 + 2|Z||O| + |O|^2 - |Z| - |O|) - (|Z|^2 -|Z| + |O|^2 -|O|) ]/2 = [ 2|Z||O| ] /2 = |Z||O|. So indeed the number of unordered pairs (i<j) with different parity is exactly |Z| * |O|. That's because in the expression, we accounted for both orders? Wait, check: If we have two sets, the number of unordered pairs with one from each set is |Z| * |O| (since each pair consists of one from Z and one from O, and that pairing is unique). Actually unordered pair (a,b) where a in Z, b in O is exactly |Z|*|O|. Because you pick one from Z and one from O, that uniquely defines an unordered pair (since they are distinct sets). That's correct: The number of unordered pairs crossing between two sets is indeed product of sizes. So our earlier thought that product counts unordered crossing pairs. So that matches: number of pairs (i,j) with i<j and p[i]!=p[j] = number of crossing unordered pairs = |Z| * |O|. So indeed total_odd = cnt0 * cnt1, where cnt0 = number of prefix indices (including 0) with parity 0, cnt1 = number with parity 1.
Thus we can simplify: For each bit, just compute counts of parity 0 and 1 after processing all elements, and then total_odd = cnt0 * cnt1. However, careful: This product counts unordered pairs (i<j) with different parity. Since we want subarrays, i = L-1, j = R, with i<j. That's exactly unordered pairs because we consider i and j distinct and order matters? Actually we require i < j, but the unordered pair (i,j) with i != j automatically has an order; but when we count crossing unordered pairs, each unordered crossing pair corresponds to exactly one ordered pair with the smaller index as first? Actually if we have an unordered pair {i,j} with i != j, there is a natural order: the smaller index is less than the larger. But the unordered pair does not specify which is smaller. However, when we count crossing unordered pairs, we count each set of two indices where one is in Z and one in O exactly once, regardless of which is smaller. But for each such unordered pair, exactly one of the two possible orders satisfies i < j (the order where the smaller index is first). So does each unordered crossing pair correspond to exactly one valid (i<j) pair? Yes, because given two distinct indices i and j, there is exactly one ordering where the first is less than the second. So the count of unordered pairs = count of ordered pairs (i<j). So product cnt0*cnt1 indeed equals number of (i,j) with i<j and p[i] != p[j].
Wait, check: Suppose we have Z indices: {0,2} and O indices: {1,3}. Unordered crossing pairs: (0,1), (0,3), (2,1), (2,3). That's 4. For each, i<j: (0,1): i=0<j=1 ok; (0,3): i=0<j=3 ok; (2,1): i=2<j=1 not ok; (2,3): i=2<j=3 ok. So only three satisfy i<j? Actually (2,1) does not satisfy i<j because 2 > 1. But our unordered pair {2,1} is same as {1,2}, but in our set O includes 1, Z includes 2. When we count unordered crossing pairs, the pair {1,2} is included? Yes, because one is from O (1) and one from Z (2). That's a crossing unordered pair. That unordered pair corresponds to ordered pair (1,2) with i=1<j=2? But i=1 is O, j=2 is Z. That ordered pair (i=1,j=2) would have p[i]=1, p[j]=0, which is also a case where p[i]!=p[j] and i<j. But does our product cnt0cnt1 count this? In the product, we select one element from Z and one from O. If we select Z=2 and O=1, that yields unordered pair {2,1} which is the same as {1,2}. But that unordered pair is counted once. The ordered pair with i<j that corresponds to {1,2} is either (1,2) or (2,1) depending on order. But there is only one unordered pair, and exactly one of the two possible orders has i<j, which is the order where the smaller index is first. Since the unordered pair contains both indices, the smaller index is 1, larger is 2. The valid ordered pair is (1,2). But does our selection of one from Z and one from O produce (1,2) when we consider the selection of O=1 and Z=2? However, when we pick one from Z and one from O, we are not specifying order; we are just picking the two indices. The unordered pair is {1,2}. That's one crossing unordered pair. For counting ordered pairs (i<j) with different parity, we can map each unordered crossing pair to exactly one ordered pair: the one where the smaller index is i and larger is j. So the number of unordered crossing pairs equals the number of ordered pairs i<j with different parity. Because given an unordered crossing pair, the ordering i<j is determined uniquely, and it satisfies parity difference. Conversely, any ordered pair i<j with different parity yields an unordered crossing pair. So the mapping is bijective. So indeed count = |Z||O|.
Thus we can just compute cnt0 and cnt1 after processing entire array for each bit, and then total_odd = cnt0 * cnt1. But careful: This counts all pairs i<j, but i can be 0? Yes, because prefix index 0 is included. So that's correct.
Thus algorithm per bit: Iterate through array to compute parity after each element and count how many times parity is 0 and 1. But we need final counts of parity states across all prefix positions (including initial 0). So we can simply:
cnt0 = 1 (for prefix 0)
cnt1 = 0
parity = 0
for a in A:
b = (a >> bit) & 1
parity ^= b
if parity == 0:
cnt0 += 1
else:
cnt1 += 1
total_odd = cnt0 * cnt1
That's even simpler, and avoids accumulating total_odd within loop. Then answer += total_odd << bit.
Let's test with earlier examples:
N=2, A=[1,2] bit0: parity evolution: start parity=0 -> cnt0=1. a=1: b=1 -> parity=1 -> cnt1=1. a=2: b=0 -> parity stays 1 -> cnt1=2. End: cnt0=1, cnt1=2 -> product=2 -> total_odd=2. bit1: start cnt0=1; a=1: b=0 -> parity=0 -> cnt0=2; a=2: b=1 -> parity=1 -> cnt1=1; product = 2*1=2. Works.
N=3, A=[1,2,3] bit0: start cnt0=1; a=1 (b=1) -> parity=1 -> cnt1=1; a=2 (b=0) -> parity=1 -> cnt1=2; a=3 (b=1) -> parity=0 -> cnt0=2; final cnt0=2, cnt1=2, product=4. bit1: start cnt0=1; a=1 (b=0) -> parity=0 -> cnt0=2; a=2 (b=1) -> parity=1 -> cnt1=1; a=3 (b=1) -> parity=0 -> cnt0=3; final cnt0=3, cnt1=1, product=3. Works.
So we can use this simplified version: iterate once to compute counts of parity values across all prefixes (including initial). Then total_odd = cnt0 * cnt1.
Thus per bit we just need to compute cnt0 and cnt1.
Now this is even simpler: For each bit, we need to compute parity of prefix sums of bits. That can be done by iterating through array and updating parity.
Complexity remains O(N * bits).
Now we could also compute all bits simultaneously: maintain an array of parities for each bit? Actually we need per bit parity state (0/1) and counts of how many prefixes have parity 0/1. Instead of separate loops, we could use an array of length bits: parity[bit] as integer 0/1, and cnt0[bit] and cnt1[bit] initially: cnt0[bit]=1, cnt1[bit]=0 for each bit. Then for each element a, for each bit in 0..bits-1, we compute b = (a>>bit)&1, update parity[bit] ^= b, then if parity[bit]==0: cnt0[bit] += 1 else cnt1[bit] += 1. After processing all elements, for each bit compute total_odd = cnt0[bit] * cnt1[bit] and add to answer. That still O(N * bits), but reduces loop overhead of iterating bits inside each test case? Actually we already have bits outer loop; switching to inner loop per element (for each element, loop over bits) is essentially same complexity. But which is more efficient in Python? Usually iterating over bits per element may be faster if we precompute bits per element? Possibly not. But given total N and bits small, either is fine. But we must be careful: In Python, nested loops: for a in A: for bit in range(bits): ... might be slightly faster if bits is small because the outer loop is over N and inner over bits, but the total operations still N*bits. Actually typical Python overhead: A for loop over N with inner loop over bits is similar to outer loop over bits with inner loop over N. But which one is better? The "bits outer, N inner" approach may have the inner loop as a Python for loop over list A each time, which means we traverse the whole list for each bit. That could cause many memory accesses. The "N outer, bits inner" approach traverses list once, and for each element processes bits. That might be more cache-friendly because it goes through array sequentially. But also bits inner loop is small (max 30). So it could be more efficient. But we must also consider that we need to maintain arrays for parity and counts per bit. That's fine.
Let's compare: Approach A: For each bit: iterate over all elements, compute b = (a>>bit)&1, update parity and counts. This does B * N iterations. Approach B: For each element: for each bit: compute b, update parity[bit] and counts[bit] accordingly. That also does N * B iterations. But approach B updates per-bit data structures each time, but that's similar. Approach B may be slightly faster because we only loop over the array once, and we can use bit iteration via while val>0 to only process set bits? But then we need to also account for bits that are 0 (they affect parity as 0, so we need to update parity only if bit changes? Actually if b=0, then parity doesn't change; but we still need to know whether parity stays same to increment count? Actually we need to update cnt0 or cnt1 based on parity after the element. If b=0, parity remains same as before. So we can increment the appropriate counter (cnt0 if parity remains 0, cnt1 if remains 1) without toggling. But if we only iterate over set bits, we miss updating counters for bits where b=0? Actually we must still update counters for all bits, because after each element, we need to record the new prefix parity state. The prefix parity state for each bit changes only if the bit in the element is 1; if it's 0, it stays the same. But we still need to increment either cnt0 or cnt1 for that bit based on its current parity (which is unchanged). So we cannot skip bits with b=0; we need to update counts for all bits at each step. Because the parity for that bit after processing the element is the same as before, but we need to count that prefix state. So we must include all bits each iteration. So we can't just iterate over set bits; we need to update all bits.
Thus both approaches similar.
Given that total N sum is only 100k, even B*N = 3M, which is negligible. So we can keep simple bits outer loop. That might be easier to understand.
But we might also worry about Python's speed with many test cases: For each test case we allocate loops. If we have many test cases with N=1, bits outer loop will call for bit in range(bits): that's at most 30. And then inner loop over A (size 1) is tiny. So overhead per test case: setting up loops, maybe not huge. But we might have 100k test cases, each doing maybe up to 30 loops. That's 3 million loops just for the outer bits loop, plus inner loops (but inner loops for N=1 add up to 100k * bits ~ 3 million). So overall maybe 6 million iterations, still fine.
But there is overhead of reading input and creating list A for each test case. That's necessary.
Thus I'll go with simple per-bit approach.
Now let's craft final solution.
Implementation plan:
- Read all input using sys.stdin.buffer.read().split()
- t = int(data[0])
- Use pointer idx = 1
- For each test case:
n = int(data[idx]); idx+=1
A = list(map(int, data[idx:idx+n])); idx+=n
if n == 0: (but n>=1)
max_val = max(A) if A else 0
bits = max_val.bit_length() # returns 0 if max_val==0
ans = 0
if bits>0:
for bit in range(bits):
cnt0 = 1 # prefix at index 0 has parity 0
cnt1 = 0
parity = 0
for val in A:
if (val >> bit) & 1:
parity ^= 1
if parity == 0:
cnt0 += 1
else:
cnt1 += 1
ans += (cnt0 * cnt1) << bit
output ans as string.
But note: In the loop, we are updating cnt0/cnt1 after each val. That includes after processing each element, we have a prefix state. So final cnt0 and cnt1 include all N+1 prefix states. So product gives total subarrays with odd XOR for that bit.
Now verify with example: 2, [1,2] gave ans=6.
Now test with all zeros: max_val=0 bits=0 -> ans stays 0.
Now test with N=100000, random. Should be fine.
Now we must ensure that for bits, we use bit from 0 to bits-1 inclusive. That covers all bits that could be set in any number. This is efficient.
Now potential pitfalls: For large numbers, bit_length gives 1 for 1, 2 for 2,3, 3 for 4..7, etc. So it's correct.
Now I'm thinking about the product cnt0*cnt1: For N=1e5, cnt0 and cnt1 can be up to N+1 = 100001, product up to ~1e10, which fits in Python int. Then shift left by up to 29 yields up to ~5e19, still within Python int.
Now what about negative numbers? Not relevant.
Now we can finalize solution.
But wait: There is a nuance: The XOR-sum of subarray is defined as A[L] XOR A[L+1] XOR ... XOR A[R]. That's the typical XOR. Our approach is correct.
Now we have a note: "XOR-sum of a list of numbers is the result of XOR-ing all of them. XOR-sum of (A[1] XOR A[2] XOR ... XOR A[N]) is defined as A[1] XOR (A[2] XOR (A[3] XOR ( ... XOR A[N])))." That's just standard XOR associativity. So nothing special.
Now implement.
One more thing: The input says total sum of N over all test cases will not exceed 100,000. So we don't have to worry about sum of N being huge.
Now I'll code.
But let's also consider an alternative: using prefix XOR directly, and counting per bit via frequency of prefixes with bit set? Actually that's same.
Now code in Python:
import sys
def main():
data = sys.stdin.buffer.read().split()
t = int(data[0])
pos = 1
out_lines = []
for _ in range(t):
n = int(data[pos]); pos += 1
A = list(map(int, data[pos:pos+n])); pos += n
max_val = max(A) if n > 0 else 0
bits = max_val.bit_length()
ans = 0
if bits > 0:
for b in range(bits):
cnt0 = 1
cnt1 = 0
parity = 0
for val in A:
if (val >> b) & 1:
parity ^= 1
if parity == 0:
cnt0 += 1
else:
cnt1 += 1
ans += (cnt0 * cnt1) << b
out_lines.append(str(ans))
sys.stdout.write("\n".join(out_lines))
if name == "main":
main()
Now test with sample:
Input:
1
2
1 2
Our code: bits = max(1,2)=2 -> bit_length(2)=2? Actually max_val=2, bit_length(2)=2 because binary '10' has length 2 (bits 0 and 1). So loop b=0,1.
b=0: cnt0=1, cnt1=0, parity=0. val=1: (1>>0)&1=1 -> parity=1; parity!=0 -> cnt1=1. val=2: (2>>0)&1=0 -> parity stays 1 -> cnt1=2. End: cnt0=1, cnt1=2 -> product=2 <<0 =2.
b=1: (2>>1)&1=1 for val=2? val=1: (1>>1)&1=0 parity stays 0 -> cnt0=2; val=2: (2>>1)&1=1 -> parity=1 -> cnt1=1; product=2*1=2 <<1 =4; ans=6. Output 6.
Great.
Now test with N=1, A=[0] => max_val=0, bits=0, ans=0 -> output 0.
Now test with N=3, A=[1,2,3] => computed ans=10. Let's run manually: max_val=3, bit_length(3)=2? Actually 3 binary '11' needs 2 bits (bits 0 and 1). So bits=2. b=0: cnt0=1; val=1 -> parity=1 -> cnt1=1; val=2 -> parity=1 -> cnt1=2; val=3 -> parity=0 -> cnt0=2; product=22=4 <<0 =4. b=1: val=1 b=0 -> parity0 cnt0=2; val=2 b=1 -> parity1 cnt1=1; val=3 b=1 -> parity0 cnt0=3; product=31=3 <<1 =6; ans=10. Good.
Now consider case where max_val=0 but some numbers are zero? All zeros: bits=0 ans=0. Correct.
Now what about numbers like 0 and 1? max_val=1 -> bits=1 (only bit0). Then we compute cnt0*cnt1 for bit0. That gives number of subarrays with XOR odd (i.e., odd number of ones). Since all numbers are 0 or 1, that's correct.
Now, potential issue: The product method assumes we count all prefix states. But we need to be sure that we include the initial prefix state (index 0) when counting. We set cnt0=1, cnt1=0. That's correct.
Now, about the product: If we have cnt0 and cnt1 counts after processing all elements, does cnt0*cnt1 count all pairs (i<j) across all prefix indices? Yes, because each unordered pair (i,j) with i≠j is uniquely determined by choosing one from Z and one from O. But careful: The product counts unordered pairs where one index is from Z and other from O. As argued, this corresponds exactly to ordered pairs i<j with different parity because given an unordered crossing pair, the smaller index is whichever is smaller, and that defines a unique ordered pair (i<j). And that ordered pair is counted exactly once because the unordered pair is counted once. So product works.
Thus we are good.
Now check with a scenario: Z indices: {0,2}, O indices: {1,3}. Unordered crossing pairs: {0,1},{0,3},{2,1},{2,3}. For each, the smaller index: {0,1}: 0<1; {0,3}: 0<3; {2,1}: 1<2? Actually {2,1} smaller=1, larger=2, ordered pair (1,2). That's a valid pair with i=1 (O), j=2 (Z). {2,3}: 2<3. So we have 4 ordered pairs: (0,1),(0,3),(1,2),(2,3). Are these all pairs (i<j) with different parity? Let's list all i<j pairs from indices 0,1,2,3: (0,1) diff, (0,2) both Z => same, (0,3) diff, (1,2) diff, (1,3) both O? Actually 1 in O, 3 in O? Wait O indices: {1,3} are both O? I defined O indices: {1,3}? Actually earlier I defined Z={0,2}, O={1,3}. So (1,3) both O, same parity, not diff. (2,3) diff. So diff pairs: (0,1),(0,3),(1,2),(2,3) -> 4. So matches product |Z||O|=22=4. Good.
Thus product works.
Now we can implement as described.
Now we should also consider that if we use the product method, we need to compute cnt0 and cnt1 after processing all elements, but we need to ensure that we count all prefix indices (including initial). Our loop updates counts after processing each element. So final cnt0 and cnt1 include the initial state (cnt0=1) plus updates for each element. So that's correct.
Now final code.
But there is an even more efficient method: Use prefix XOR directly: compute prefix XOR array P[0..N] (P[0]=0, P[i] = A[1] xor ... xor A[i]). Then for each bit, we need to count number of pairs (i<j) where the bit is set in P[i] xor P[j] = 1. That is equivalent to counting crossing pairs between zeros and ones in the bit of P. So we can compute for each bit, count0 = number of P[i] with bit 0, count1 = number with bit 1. Then product = answer per bit. This method would require computing P (N+1 ints) and then for each bit, iterating over P to count zeros and ones. That is also O(N * bits). But we need to store P. It might be simpler: Just compute P once (N+1) then for each bit, count. That would be: For bit in range(bits): count0 = sum(1 for p in P if ((p>>bit)&1)==0); count1 = len(P)-count0; total_odd = count0count1; ans+=total_odd<<bit. That also O(Nbits) but uses list comprehension per bit which may be slower than our incremental approach? Possibly but still fine. But our incremental approach per bit already iterates over A once per bit, which is similar.
However, note that our per-bit method uses only A, not extra array. That's fine.
But consider that we can combine both: compute prefix XOR P, then for each bit count zeros/ones in P. This requires only one pass to compute P (O(N)), then for each bit iterate over P (N+1 elements). That's also N*bits total. Which is similar but we have to iterate over P B times. That is essentially the same as per-bit over A. But per-bit over A we also iterate B times. So no difference.
We could also compute answer by iterating bits and using two counters while scanning A, as we did. That's fine.
Thus final solution is ready.
Now we need to produce final answer: Provide explanation and code.
Now final output.
But I recall that there might be a nuance: XOR-sum is defined as A[1] XOR (A[2] XOR (A[3] XOR ...)). However, XOR is associative, so it's fine.
Now we can deliver.
But wait, the problem statement says: "XOR-sum of a list of numbers is the result of XOR-ing all of them. XOR-sum of (A[1] XOR A[2] XOR ... XOR A[N]) is defined as A[1] XOR (A[2] XOR (A[3] XOR ( ... XOR A[N])))." That's just to clarify associativity.
Thus done.
Now I will produce final answer. ```python
import sys
def solve() -> None:
data = sys.stdin.buffer.read().split()
t = int(data[0])
pos = 1
out = []
for _ in range(t):
n = int(data[pos]); pos += 1
# read the array
A = list(map(int, data[pos:pos + n])); pos += n
max_val = max(A) if n else 0
bits = max_val.bit_length() # number of relevant bits
ans = 0
if bits:
for b in range(bits):
cnt0 = 1 # prefix[0] has parity 0
cnt1 = 0
parity = 0 # current xor of bits up to current element
for val in A:
if (val >> b) & 1:
parity ^= 1
if parity == 0:
cnt0 += 1
else:
cnt1 += 1
# number of subarrays with this bit = 1
ans += (cnt0 * cnt1) << b
out.append(str(ans))
sys.stdout.write("\n".join(out))
if name == "main":
solve()