We need to count subarrays [l, r] such that by permuting bits within each number (i.e., rearranging the bits of each number arbitrarily), we can achieve that the XOR of the numbers equals 0.
Observation: Permuting bits within each number means that for each number a_i, we can replace it with any number that has the same multiset of bits (i.e., same number of 1-bits). In binary representation, the number of 1 bits (popcount) is invariant under swapping bits. Actually, swapping bits just rearranges positions, so the set of bits (their positions) changes but the count of 1's stays the same. However, can we get any number with exactly that many ones? Yes, if we consider infinite leading zeros? The numbers have some maximum bit positions, but we can also consider that swapping bits can produce numbers with bits set at arbitrarily high positions if we allow leading zeros? Let's think: The binary representation of a number has infinite leading zeros. The operation "swap any pair of bits in its binary representation" means we can exchange any two bits, including leading zeros? Usually bits of a number are considered as the finite representation, but swapping with leading zeros effectively shifts bits to higher positions? Actually, if we consider the infinite binary representation, there are infinitely many zeros. Swapping a 1 with a leading zero effectively moves the 1 to a higher position, creating a larger number. So yes, we can generate any number that has exactly k ones (where k = popcount(a_i)), because we can place those k ones at any set of distinct positions (any bits). However, is there any restriction due to the original number's finite bit-length? No, we can swap with zeros beyond the most significant bit, effectively extending the number. So indeed, for each a_i, we can transform it into any integer (≥0) that has exactly popcount(a_i) bits set. The range up to 10^18 doesn't constrain because we can make arbitrarily large numbers? But in the problem, we are allowed to swap bits in its binary representation. The representation presumably is infinite (with leading zeros). So yes, we can produce arbitrarily large numbers, as long as the count of 1's is fixed. For example, 6 (110) can become 2^60 + ... So the numbers can become huge. But does that matter for XOR? Since XOR is bitwise, and we can choose positions independently for each number, we can align bits.
We need to know if there exists an assignment of numbers b_i (each with popcount(b_i) = popcount(a_i)) such that XOR_{i=l}^r b_i = 0.
Equivalently, we need to check whether the multiset of popcounts (k_i) can be combined such that we can place ones in positions so that overall each bit position has even parity.
We can think of it as we have r-l+1 groups of ones, each group of size k_i. We need to distribute these ones into bit positions (maybe multiple per position?) But each number's ones are distinct positions within that number: each number must have exactly k_i distinct bits set to 1. The XOR sum across numbers: for each bit position, the total number of numbers that have a 1 in that position mod 2 determines whether that bit is 1 or 0 in final XOR. We want all bits to have even parity (i.e., XOR = 0). So we need to assign to each number a set of positions (size = k_i) such that for every bit position, the total count of numbers assigned to that position is even.
This is reminiscent of a combinatorial design: Given a list of integers (sizes), can we partition the multiset of ones into pairs per bit? Equivalent: We have total sum S = sum_{i} k_i. We need to partition the total ones into bits, each bit receiving an even number. That is possible if and only if S is even? Not exactly: If we have groups, they must be assigned to bits such that each bit gets an even number. But each group's ones must be assigned to distinct bits for that number (can't assign two ones of same number to same bit because a number can only have at most one 1 per bit). So each number's ones must occupy distinct bits. So we need to find an assignment of each number's k_i ones to bit positions such that overall each position gets an even count.
This is equivalent to checking if the multiset of k_i can be partitioned into some multiset of even-sized groups? Actually, think of it as a binary matrix with rows = numbers, columns = bits, each row has exactly k_i ones, each column sum must be even. The existence of such a matrix over GF(2) with given row sums? But we are not constrained by number of columns, we can introduce arbitrarily many columns. However, the matrix must be binary and row sums fixed. We can choose as many columns as we want, each column sum must be even (including zero). So we need to know if we can represent each row as a sum of some columns (vectors) such that the sum of all rows is zero vector (since total XOR zero means mod 2 sum of all rows = 0 vector). Actually XOR of numbers = 0 means that for each column, the sum of that column across rows is 0 mod 2. So the vector sum of rows = 0.
We have rows v_i (binary vectors of possibly infinite length). We need to choose v_i such that weight(v_i) = k_i and sum_i v_i = 0. Equivalently, we need to partition the set of all ones (sum_i k_i ones) into pairs that belong to the same column. But each row's ones must be in distinct columns; so each row cannot have two ones in same column. So we need to pair up the ones from possibly different rows (or within same row? Actually, if a column has even number of ones, that means that the ones in that column come from an even number of rows. Could it be that the same row contributes two ones to the same column? No, because each row has at most one 1 per column. So each column's ones come from distinct rows. So we need to pair up the ones across rows such that each row's ones are placed in distinct columns, and each column gets an even number (maybe 0,2,4,...). This is essentially a multigraph where rows are vertices, and we have to allocate each row's degree equal to k_i, and we need to add edges (each edge corresponds to a column with exactly 2 ones? Actually, a column could have more than 2 ones, e.g., 4 ones. But then we could decompose that column into 2 pairs? However, a column with 4 ones is allowed; it still yields even sum. But if we can have columns with arbitrary even count, we can model it as multiple edges per column. However, for existence, it's enough to consider columns with exactly 2 ones, because any column with 2m ones can be replaced by m columns each with 2 ones (by splitting the column's ones into pairs). But careful: If we split a column with 4 ones into two columns each with 2 ones, does it affect the ability to assign distinct positions per row? The rows that had a 1 in that column originally would now have 1's in two different columns. That's allowed. So if there exists an assignment using columns with arbitrary even counts, there also exists an assignment using only columns of size 2 (by splitting each column into pairs). However, we must ensure that each row does not have more than one 1 in a column, but splitting avoids that. So the existence reduces to: Can we pair up the ones (each belonging to some row) into pairs such that the two ones in a pair belong to distinct rows? That is, we need to partition the multiset of k_i ones (where each row contributes k_i labeled "slots") into pairs such that no pair contains two slots from the same row (since same row cannot have two ones in the same column, but if they were in different columns, they could be paired? Wait, a pair corresponds to a column that has exactly two ones. In that column, the two ones come from two rows. Could we have a column with two ones from the same row? No, because each row cannot have two ones in the same column (binary). So each pair must involve two different rows. So we need to partition all slots into pairs where each pair consists of slots from two distinct rows. This is possible if and only if the maximum k_i <= sum_{j != i} k_j? Actually, that's a condition for a simple graph: We have a multigraph where each row i has k_i "half-edges". We want to pair up half-edges into edges connecting two distinct vertices. This is exactly forming a simple graph (no loops) from a given degree sequence (allowing multiple edges between same pair). The existence condition for such a pairing is that the total sum of degrees is even, and no vertex's degree exceeds the sum of degrees of all other vertices. Because we cannot pair a vertex's half-edges with itself; they must be matched to others. The necessary and sufficient condition for a multigraph without loops (but allowing multiple edges) with given degrees d_i is that sum d_i is even and max(d_i) <= sum_{j != i} d_j for all i, which is equivalent to max(d_i) <= total - max(d_i) => max(d_i) <= total/2. Indeed, this is the Erdős–Gallai condition for a graphical sequence when loops are not allowed. Actually, for a simple graph (without multiple edges) there are more conditions, but if multiple edges allowed, the only condition is total even and max <= total - max, i.e., max <= total/2. This is because we can greedily pair the largest with others. So the condition is: sum k_i is even, and the largest k_i <= sum_{j != i} k_j, i.e., 2*max(k_i) <= sum k_i.
But is that also sufficient when we require each row to have distinct columns? Pairing into edges yields a set of columns each with two ones. Then we assign each edge to a distinct column (bit position). That's valid because each row's edges correspond to distinct columns (since each edge is a distinct column). Also, each row's degree equals k_i, so it gets k_i distinct columns. So yes.
Thus, the condition for a set of numbers to be good is: let S = sum_{i} popcount(a_i) and let M = max_i popcount(a_i). Then the subarray is good iff S is even and M <= S/2.
Wait, is there any other nuance? Could we use columns with more than two ones to circumvent the max > S/2 condition? Suppose we have degrees: k1=3, k2=1, k3=1. Then total S=5 (odd) -> cannot have even total? Actually total must be even because each column contributes an even number to sum; sum of column sums = S; each column sum even => S even. So odd sum impossible. So S odd => not good.
Now consider k1=3, k2=3. S=6 even, M=3, S/2=3 => M <= 3 condition satisfied. Can we arrange? Yes, pair three edges between row1 and row2, giving three columns each with ones in both rows. XOR zero? Each column contributes two ones => even parity, so XOR 0. Works.
Consider k1=4, k2=1, k3=1. S=6, M=4 > 3. Condition fails. Can we still achieve XOR 0? We need to assign ones to columns. Let's try: Row1 has 4 ones. They need to be placed such that each column containing a 1 from row1 must have odd total ones? Actually we need overall even parity per column. If row1 puts a 1 in a column, then to make even, the total number of ones in that column must be even, so there must be an odd number of other ones in that column. Since each other row can contribute at most one 1 per column, the number of other rows with a 1 in that column must be odd. So each column where row1 has a 1 must have at least one other row's 1, and the total number of other rows' ones in that column must be odd. Could we have columns with three ones: row1 + two others? But we have only two other rows with total 2 ones. Could we have one column with row1 and both others (2 others) gives 3 ones -> odd, not even. So that column would have parity 1, not good. Could we have column with row1 only? That would be odd. So we need each column where row1 appears to have an odd number of other ones, i.e., 1,3,5,... But we only have two other rows, so the maximum number of other ones in a column is 2 (if both others put a 1 there). 2 is even, so 1+even = odd? Wait: if row1 contributes 1, and other rows contribute X (mod2). To have total even, we need 1 + X ≡ 0 mod2 => X ≡ 1 mod2, i.e., X odd. So the number of other ones in that column must be odd. With two other rows, possible odd numbers: 1 or 3? But we only have at most 2, so only possible odd is 1. So each column where row1 appears must have exactly one other row (i.e., one of the two) present. So row1's 4 ones must be paired with exactly one other row per column. That means the total number of ones from other rows used in columns with row1 is exactly 4. But other rows together have only 2 ones. So impossible. Could we use columns where row1 does not appear? Those columns could have 0 or 2 ones from other rows. But that doesn't help consume row1's ones. So indeed impossible. So condition seems necessary and sufficient.
What about cases where S even but M > S/2? Then it's impossible because the row with max count would need to be matched with others, each column where it appears requires an odd number of others, total others needed = at least M (since each of its ones needs at least one other, and because each other row can contribute at most 1 per column and total others sum to S - M, we need M <= S - M? Actually more precisely: For each column that includes the max row, the number of other rows present must be odd and at least 1. So the total number of other-row-ones placed in columns that include the max row must be at least M (since each such column requires at least one other one). But the total available other ones is S - M. Thus we need S - M >= M => S >= 2M => M <= S/2. So necessary. And as argued, if M <= S/2 and S even, we can construct by pairing the half-edges arbitrarily (graphical condition). So condition is both necessary and sufficient.
Thus, the problem reduces to: Given array of popcounts b_i = popcount(a_i). Count number of subarrays [l, r] such that sum b_i is even and max(b_i) <= sum(b_i)/2.
Now we need to efficiently count subarrays satisfying these constraints. n up to 3e5, b_i up to at most 60 (since a_i <= 1e18 ~ 2^60). Actually maximum popcount of a 64-bit number is 64, but 1e18 < 2^60 so max popcount <= 60.
So b_i are small (≤60). This is crucial.
We need to count subarrays where S even and max <= S/2.
Let’s denote S = sum of b_i in subarray. Condition: 2*max ≤ S, and S even.
This is reminiscent of counting subarrays with constraints on sum relative to max. Since b_i are small, the sum S of a subarray length L can be at most 60L, but that's not bounded. However, we can use the fact that the condition 2max ≤ S is equivalent to S >= 2*max. Since max is at least 1 for positive numbers? Actually b_i can be zero? a_i ≥ 1 so popcount ≥ 1. So max ≥1.
Now, the condition is S >= 2M, with S even.
We can attempt to count all subarrays and subtract those that violate the condition. But total subarrays is O(n^2), need O(n log n) or O(n) solution.
Observation: Since b_i ≤ 60, the condition S >= 2M is often satisfied except when subarray is short or contains a large M relative to sum. For large lengths, sum is large relative to max. In fact, for any subarray, if length L >= some threshold, automatically S >= 2M? Not necessarily if many b_i are zeros? But b_i>=1, so sum >= L. Max ≤60. So if L >= 60, then S >= L ≥ 60, while max ≤60, so S ≥ L ≥ 60 => S ≥ max? Actually we need S ≥ 2M, so if L >= 120, then S >= L >= 120 >= 2*60? 2M max 120, so L >=120 ensures S >= L >= 120 >= 2M? But M could be 60, 2M=120, so L=120 gives S >=120, which might be exactly if all b_i=1? Wait if all b_i=1, then S = L, max=1, then condition S >= 2 => L >=2, which is easy. So worst case for condition is when many numbers have small popcount, and one number has large popcount. For subarray containing a large M, say M=60, we need sum of all b_i in subarray >=120. If the other b_i are small, say all 1's, then we need length at least 61? Actually if M=60 and others are 1, then sum = 60 + (L-1)*1 = L+59. We need L+59 >= 120 => L >= 61. So if the subarray includes the 60, we need at least 61 elements (including the large one) to satisfy sum condition. That's not too large.
But n=3e5, so length up to 3e5, but we can potentially enumerate subarrays with "bad" conditions that are relatively short, because the condition fails only when sum is small relative to max. Since max is bounded by 60, the sum needed is at most 120. So if subarray length exceeds, say, 120 (or maybe more if many are 0? but no zeros), then sum will be at least length, which likely exceeds 120. However, consider b_i can be as small as 1, so length 120 gives sum at least 120, which meets the threshold 260 = 120. So for subarrays of length > 120, sum >= length >= 121 > 120 => S > 2M for any M ≤60? If M=60, need S>=120. Length 121 gives S >=121 >120, so condition holds (provided S even? parity might still fail). Actually S could be odd. So parity condition still needs checking. But for long subarrays, parity can be either; we need to count those with even sum. But even sum condition is easier: we can precompute prefix sums modulo 2. But we also need the max condition. But for long subarrays, max condition almost always holds except maybe if M is near 60 and sum just above threshold? For length > 120, sum >120 so S >= 2M holds. For length =120, sum could be exactly 120 if all b_i=1, then M=1, condition holds. So indeed, for subarrays length > 120, S >= length > 120? Actually if length = 121, sum >=121, M ≤60, 2M ≤120, so S ≥121 >120 ≥2M. So S > 2M, condition holds strictly. So for any subarray with length > 120, condition S >= 2M automatically holds (since minimal sum is length, and length > 120 > 260). But wait, what if b_i can be 0? a_i >=1 so popcount at least 1, so minimum b_i =1. So length >120 gives S >=121 >120 >=2M. So yes, for any subarray with length >= 121, the max condition is automatically satisfied. Therefore, the only subarrays that could violate the max condition are those of length at most 120. That's a key observation: because max popcount <= 60, the condition fails only when sum is less than 2M. Since sum is at least length, length must be less than 2M <= 120. So length <= 119 maybe? Actually if length = 120, sum could be 120 (if all ones) and M could be 1, condition holds. But could it fail for length = 120? For it to fail, we need S < 2M. Since S >= length =120, we need 2M >120 => M >60, impossible. So length >=121 always safe. So the max condition is only restrictive for subarrays of length at most 120. That's great.
Now, we need to count all subarrays (l,r) such that:
- sum of b_i is even.
- max(b_i) <= sum/2.
We can count total subarrays with even sum and subtract those with even sum but violating max condition. However, the max condition violation only occurs for short subarrays (length <=120). So we can count all subarrays with even sum (which is easy using prefix parity counts) and then subtract the number of subarrays with even sum that have max > sum/2. Since these subarrays are short, we can brute-force over all O(n * 120) of them.
But careful: The condition for good subarray is both S even and S >= 2M. So we need to count subarrays that satisfy both. Alternatively, count all subarrays with S even (which we can compute as: number of pairs (l-1, r) with prefix parity equal). Then subtract those with S even but S < 2M. And also we must not count those with S odd? They are not counted anyway because we only count even S. So the plan: total_even = count of subarrays with even sum. Then subtract count_bad_even = number of subarrays with even sum and max > sum/2 (i.e., 2max > sum). Since these are the only ones that would be incorrectly counted as good if we only check even sum. But wait, is it possible that a subarray with odd sum could be good? No, because we argued total sum must be even for existence. So only even sum subarrays can be good, and among those, those with 2max <= sum are good. So the answer = total_even - count_bad_even.
Now, total_even can be computed: Let prefix sums mod 2: p[0]=0, p[i]= (p[i-1] + b_i) % 2. Number of subarrays with even sum = number of pairs (i<j) with p[i] = p[j]. So we can count frequencies of 0 and 1. That's O(n).
Now, count_bad_even: subarrays with even sum and 2*max > sum. Since length <= 120, we can enumerate all subarrays of length up to 120? But n is 300k, enumerating all subarrays of length up to 120 would be O(n * 120) = 36 million, which is okay in C++ with optimization. However, we must also compute sum and max for each subarray efficiently. We can precompute b_i. Then for each starting index l, we can expand to r up to min(n, l+119) (since length up to 120, but length 120 is actually safe? Actually we need to consider subarrays of length up to 120? Let's check: if length = 120, sum >= 120, max <=60 => 2max <=120 => condition holds always? Actually if sum = 120 exactly, and max=60, then 2max=120, condition holds (equality). So only strict violation is 2max > sum. For length = 120, could we have 2max > sum? That would require sum < 2max. Since sum >=120, and 2max <=120, sum < 2max would imply sum < 2max <=120 => sum <=119, but sum>=120 for length 120 because each b_i >=1? Actually b_i >=1, so sum >= length =120. So sum cannot be <=119. So length =120 cannot violate. So we only need to consider length up to 119? Let's check length=119: minimum sum =119, maximum possible max =60, 2max=120. Could have sum=119, max=60 => 2max=120 >119, violation possible. So length up to 119 can be violating. So we need to consider subarrays of length L <= 119. So for each l, r from l to min(n, l+118) because number of elements is r-l+1 <= 119. That's up to 119 per l => about 35.7 million iterations. That's acceptable. But we must also check parity and sum condition. Additionally, we must be careful: for length 119, sum could be as low as 119 (if all ones). With max=60, violation occurs. But is it possible to have max=60 and sum=119? Need the rest 118 numbers to sum to 59, average about 0.5, but b_i are at least 1, so impossible: if we have a 60, the other 118 numbers each at least 1 gives sum at least 60+118=178 >119. So actual sum for length 119 with a 60 is much larger. So maybe violation can only occur for even smaller lengths? Let's bound: For violation, we need sum < 2M. Let M be the max. Suppose we have subarray of length L. The minimal sum given that one element is M and the rest are at least 1 is M + (L-1)*1 = M + L -1. For violation, we need M + L -1 < 2M => L -1 < M => L <= M. So if the max is M, the subarray length must be at most M for violation to be possible? Let's derive: sum >= M + (L-1)*1 = M + L -1. Condition for violation: sum < 2M => M + L -1 < 2M => L -1 < M => L <= M. Because L and M are integers. So indeed, for a violation to be possible, the length L must be at most M. Since M ≤ 60, L ≤ 60. Wait, is that always? What if some other numbers are larger than 1? That would increase sum, making violation less likely. So the most favorable for violation is when all other numbers are as small as possible (i.e., 1). So the necessary condition for violation is L ≤ M. Because if L > M, then even with minimal others (1s), sum = M + (L-1) >= M + M = 2M? Actually if L > M, then L-1 >= M, so sum >= M + M = 2M, so sum >= 2M, no violation. If L = M+1, then L-1 = M, sum >= M+M = 2M, so sum >= 2M, equality possible if others are all 1. But sum >=2M, not strict <. So violation requires sum < 2M, so with L = M+1 and others all 1, sum = 2M, not <, so no violation. So indeed L must be <= M for violation. Since M ≤ 60, we only need to consider subarrays of length at most 60. That's even better!
But careful: The condition for good is S even and 2M <= S. Violation is 2M > S. As argued, if L >= M+1, then even with minimal others sum >= 2M, so 2M <= S, condition holds automatically. So violation can only happen when L <= M. Since M ≤ 60, L ≤ 60. So we only need to check subarrays of length up to 60. That reduces the brute-force to at most n * 60 = 18 million, which is very comfortable.
But wait: What about the case where some b_i can be 0? We said a_i >=1 so popcount at least 1, but can a_i be power of two? Yes, popcount can be 1. So min is 1. So the reasoning holds: minimal other values are 1. So indeed, for L > M, sum >= M + (L-1)*1 >= M + M = 2M, so violation impossible. So we only need to consider subarrays of length at most 60. However, consider if the max is not unique? That doesn't matter. So we can limit our check to subarrays with length <= 60. But is it possible that L > M but sum is odd? That doesn't matter for violation; we only consider even sum for subtraction. But we are subtracting bad even subarrays. So for L > M, even if sum is even, they are automatically good because max condition holds. So they are not subtracted. So we only need to count bad even subarrays among those with length <= 60. But wait: Could there be a subarray with length > M but sum < 2M due to some numbers being 0? But there are no zeros. So safe.
Thus, count_bad_even = number of subarrays with length ≤ 60 (actually length ≤ 60, but we should also ensure length ≤ M for violation, but we can just check all subarrays with length ≤ 60 and test condition; those with length >60 will never be bad, but checking them would be wasteful. So we can restrict to length up to 60.
Now, for each starting index l, we consider r from l to min(n, l+59) (since length up to 60). Compute cumulative sum and track max. For each such subarray, if sum is even and 2*max > sum, then it's bad and we subtract from total_even.
But is it possible that a subarray of length <=60 has even sum and satisfies max condition? Then it's good, and we don't subtract. So we only subtract those that are even sum and violate max condition.
Now, careful: Are we sure that for subarrays with length >60, the max condition always holds? Yes, as argued. But what about length exactly 61? L=61, M <=60, then sum >= 61 (since each >=1). 2M <=120, and sum >=61 may be less than 2M if M=60? 2M=120, sum could be as low as 61 if all ones except a 60? Wait, to get max=60, you need at least one element with 60. Others minimum 1. So minimal sum for length 61 with M=60 is 60 + 60*1? Actually there are 60 other elements each at least 1, so sum >= 60 + 60 = 120. So sum =120 exactly possible if the 60 other elements are all 1. Then sum=120, 2M=120, condition holds (equality). So no violation. For any larger length, sum only increases. So safe.
Thus algorithm:
- Read n, array a. Compute b[i] = popcount(a[i]).
- Compute prefix parity: pref[0]=0; for i=1..n: pref[i] = (pref[i-1] + b[i]) % 2.
- Count total_even = sum over each parity of C(cnt,2). Let cnt0 = number of pref indices with 0, cnt1 with 1. total_even = cnt0*(cnt0-1)/2 + cnt1*(cnt1-1)/2.
- Compute bad_even = 0.
For each l from 1 to n:
let sum = 0, maxb = 0.
for r from l to min(n, l+59): // length 60
sum += b[r];
maxb = max(maxb, b[r]);
if sum % 2 == 0:
if 2maxb > sum: // violation
bad_even++;
Note: We need to include subarray of length 1? For single element, sum = b_i, max = b_i, condition: S even? For good, we need S even and 2M <= S => 2b_i <= b_i => b_i <=0, impossible since b_i>=1. So single-element subarrays are never good. Our total_even includes subarrays with even sum: for a single element, sum = b_i. b_i even? Could be even, e.g., b_i=2. Then total_even would count that subarray? Actually subarray with l=r is counted in prefix pairs when i=j? No, subarray sum from l to r inclusive corresponds to pref[r] - pref[l-1]. For l=r, we need pref[l-1] and pref[l] with same parity. In our total_even counting using pairs (i<j), we didn't include i=j. So single-element subarrays are not counted because they correspond to i=j, but we only count i<j. However, subarray of length 1 is included when we consider i=j? Typically number of subarrays with even sum includes those of length 1 if the element itself is even. But the formula using prefix parity: sum_{l..r} even means pref[l-1] and pref[r] have same parity. Counting all unordered pairs of indices with same parity includes pairs where l-1 = r? Actually indices range from 0 to n. A subarray corresponds to a pair (i, j) with i < j, where i = l-1, j = r. So we need i < j. So length 1 subarray corresponds to i = l-1, j = l, with i<j. So it is included. Wait: For length 1, l=r, then i = l-1, j = l, so i<j indeed. So pairs (i,j) with i<j, not i≤j. So we count all subarrays, including length 1. So total_even includes single-element subarrays if that element's popcount is even. Our condition for good requires sum even and 2M <= S. For single element, S = b_i, M=b_i. Condition 2b_i <= b_i => b_i=0, impossible. So such subarray is not good. So they should be subtracted if they have even sum. In our bad_even count, we need to include those single-element subarrays with even sum, because they violate max condition. And our loop for each l with r from l to l+59 includes r=l (length 1). So they will be caught: sum = b[l], max = b[l], if b[l] even then 2max = 2*b[l] > b[l] (since b[l]>=2) -> bad_even++. Good.
Now, what about subarrays where sum is even and 2max == sum? That is not bad, it's good. So we only subtract when 2max > sum. So condition is strict.
Edge case: sum=0? But b_i >=1 so sum can't be 0.
Now, we also need to consider subarrays with length up to 60 that have even sum but 2*max <= sum, these are good and are not subtracted. But are they counted in total_even? Yes, and we don't subtract them, so they remain in answer. Good.
Thus answer = total_even - bad_even.
But is it possible that a subarray with length > 60 has even sum but violates max condition due to some numbers being zero? But we established no zeros. So safe.
Thus solution seems straightforward.
Let's test with provided examples.
Example1:
3
6 7 14
popcounts: 6=110 (popcount 2), 7=111 (3), 14=1110 (3). b=[2,3,3].
Compute total_even:
pref:0, then 2%2=0, (0+3)%2=1, (1+3)%2=0 => pref=[0,0,1,0]
Indices 0..3. Parity0: indices 0,1,3 => count=3; parity1: index2 =>1.
total_even = C(3,2)+C(1,2)=3+0=3.
List all subarrays:
[1,1]: sum=2 even, max=2, 22=4>2 => bad.
[1,2]: sum=5 odd => not counted in total_even? Actually total_even counts only even sums; this sum odd so not in total_even. So not good.
[1,3]: sum=8 even, max=3, 23=6 <=8 => good.
[2,2]: sum=3 odd.
[2,3]: sum=6 even, max=3, 2*3=6 <=6 => good.
[3,3]: sum=3 odd.
So good subarrays: (1,3) and (2,3) -> 2. Our formula: total_even = 3 (subarrays with even sum: [1,1], [1,3], [2,3]). bad_even = those even sum and violation: [1,1] qualifies as bad, also check any others? [1,3] and [2,3] are not bad. So bad_even=1. Answer=3-1=2. matches.
Example2:
4
1 2 1 16
popcounts: 1->1, 2->1, 1->1, 16->1 (since 10000 ->1). So all b_i=1.
pref: 0,1,0,1,0 -> parity0 indices:0,2,4 count=3; parity1:1,3 count=2. total_even = C(3,2)+C(2,2)=3+1=4.
Now check subarrays with even sum and violation? Since all b_i=1, sum = length, max=1. Condition: 2*1=2 <= sum? For length >=2, sum>=2 => condition holds. For length=1, sum=1 odd, not even. So no even sum subarray violates max condition because the only even sum subarrays have length even (2,4, etc) and sum>=2, condition holds. length=0 not considered. So bad_even=0. Answer=4. matches example: (1,2),(2,3),(3,4),(1,4) are good? Check: For (1,2): sum=2 even, max=1, 2<=2 => good. They are indeed counted.
So algorithm works.
Now consider case where b_i can be larger. e.g., n=1, a=2 (popcount=1). total_even: pref:0,1 => parity0:1 (index0), parity1:1 (index1) => total_even = 0+0=0? Actually indices:0 (0) and 1 (1). Count0=1, count1=1. C(1,2)=0 each => total_even=0. So answer 0. correct.
Another test: n=1, a=3 (popcount=2). total_even: pref:0,0? 2%2=0 -> pref[1]=0. indices:0,1 both 0 => count0=2, count1=0 => total_even = C(2,2)=1. bad_even: for l=1,r=1: sum=2 even, max=2, 2*2=4>2 => bad_even=1. Answer=0. Good.
Now we need to be careful about overflow? sum of b_i up to 60*3e5 = 18e6, fits int.
Now, can there be any subarray with even sum and 2max > sum that has length > 60? As argued, no, because min sum >= length, and length > 60 => sum > 60 >= 2max? Actually max <=60, so 2max <=120. If length >120, sum>120, so automatically sum >= 2max? But we argued length > M is sufficient, but M could be as low as 1. Length > 1 ensures sum >=2? But if M=1, length 2 gives sum >=2, condition holds. So for length > M, condition holds. Since M <=60, length >60 ensures condition holds. But is it possible that length <=60 but M is small, say M=1, condition always holds because 21=2 <= sum? If length=1, sum=1 odd, so not even. If length=2, sum=2 even? Actually sum could be 2 if both ones => even, 2*1=2 <=2 holds. So no violation. So only potential violations occur when M is relatively large and length is small.
Thus algorithm is O(n * 60) ~ 18e6, fine.
But wait, are we sure that the condition for subarray being good is exactly S even and 2*max <= S? Let's double-check logic: We argued that existence of such assignment is equivalent to being able to pair the bits across numbers into columns of two (or even columns) such that each row gets exactly k_i bits. That's the graphical sequence condition for multigraphs without loops: sum even and max <= sum/2. However, is there any nuance about the fact that we are allowed to assign bits to arbitrarily high positions? In the graph model, each edge corresponds to a column with two ones. That's fine. But what about columns with more than two ones? They could be used to circumvent the condition? We argued that if condition holds, we can pair all half-edges arbitrarily, constructing a multigraph with given degrees. This yields an assignment where each column has exactly two ones. That's valid. Conversely, if condition fails (max > sum/2), any assignment would require that in columns where the max row has a 1, there must be odd number of other ones, and the total other ones needed would be at least max, but only sum - max available, leading to contradiction. However, could there be an assignment that uses columns with more than two ones, making it possible that a single column could cover multiple ones from the max row? But a column cannot have more than one 1 from the same row, because a number cannot have two 1s in the same bit position. So the max row can contribute at most one 1 per column. So each column that contains the max row uses exactly 1 of its ones. To make total even per column, that column must have an odd number of other rows' ones. So the total number of other rows' ones used in columns where max row appears is exactly the number of columns where max row appears, which equals k_max (the degree of that vertex). In each such column, there must be at least 1 other row's 1, and the total other rows' ones used in those columns is at least k_max. However, could some column have more than 1 other row's 1, reducing the number of columns needed? Actually the max row's ones must be placed in distinct columns. Suppose max row has degree M. It occupies M distinct columns. In each of these columns, to have even parity, the total number of other rows' ones in that column must be odd. The minimum odd number is 1. So total other rows' ones used in those columns is at least M (if each column has exactly 1 other). Could we have a column with 3 other rows' ones? That would use 3 others in one column, still total others used in those columns would be sum of odd numbers, which is at least M (since each odd >=1). So total others used >= M. But we only have total other ones = S - M. So necessary condition is S - M >= M => S >= 2M. So condition is necessary regardless of column sizes. And if S even and S >= 2M, we can construct using only columns of size 2. So condition is sufficient.
Thus condition is correct.
Now, we must be careful about the case where some b_i = 0? The problem says 1 ≤ a_i, so popcount≥1. So fine.
Now, we can implement.
Complexities: O(n * 60) ~ 18 million, with simple operations: addition, max, condition check. That's fine in C++.
But wait: n up to 300k, 60 per start is 18 million, but we also need to compute prefix parity for total_even, which is O(n). So overall fine.
Potential pitfalls: We must ensure that we count bad_even only for subarrays with even sum and 2*max > sum. In our loop, we compute sum incrementally, track max, and if sum%2==0, check condition.
But is it possible that a subarray with length > 60 could be bad? Let's double-check the bound: Suppose b_i can be as high as 60. But what about a_i larger than 10^18? It says up to 10^18, so popcount at most 60 because 2^60 ~ 1e18, actually 2^60 is about 1.15e18, so up to 60 bits. So max popcount <= 60. So our bound holds.
But what if we have b_i = 60 and many b_j = 1? For length = 61, sum = 60 + 601 = 120. That's equal to 260. So condition holds. For length = 60, sum could be as low as 60 + 59*1 = 119, which is < 120 => violation possible. So length up to 60 inclusive could violate. So we need to check length up to 60. That matches earlier reasoning that length <= M can violate, with M up to 60.
Now, we also need to consider that max might not be the element with the highest popcount if there are ties. That's fine.
Now, total_even counting: Use long long for counts because n up to 3e5, total subarrays ~ 4.5e10, which fits in 64-bit (max about 4.5e10, well within 2^63). So use long long.
Now, edge case: When sum=0? Not possible.
Now, implement.
Let's test with some random small cases via brute force to ensure correctness.
But before coding, consider if any subarray with length <=60 could satisfy S even and S >= 2M but we mistakenly not subtract? No, we only subtract if S even and 2M > S. So good.
Now, but what about the parity condition: We're subtracting bad_even from total_even. However, is it possible that a subarray with odd sum still satisfies the condition? No, because we argued sum must be even. So good subarrays are subset of even-sum subarrays. So answer = total_even - (# even-sum subarrays that are not good). That's correct.
Now, is it possible that some even-sum subarray with 2M <= S is still not good due to some other reason? We argued the condition is sufficient. Let's test with a small example: b = [2,2,2]. S=6 even, M=2, 2M=4 <=6. Can we construct assignment? Need three numbers each with two ones, total 6 ones. Need to pair them into columns such that each column even. Could we do: Use three columns each with two ones from two different numbers? For three numbers A,B,C with degree 2 each, total degree 6. To satisfy each row degree 2, we need a multigraph on 3 vertices with degrees 2 each. That is possible: A-B, A-C, B-C (each edge representing a column with two ones). So yes.
Another example: b = [1,1,1,1] (all ones). S=4 even, M=1, 2M=2 <=4. Can we do? Need four numbers each with one 1, total 4 ones. Need columns with even counts. Could use two columns, each with two ones: assign first two numbers to column1, last two to column2. Then each number gets exactly one 1. XOR=0. Works.
b = [2,2] S=4 even, M=2, 2M=4<=4. Need two numbers each with two ones. Total 4 ones. We can use two columns each with two ones from both numbers? That would give each number two ones, but they would be the same columns? Actually each column can have at most one 1 per number. So we can have two columns: column1 has both numbers set to 1; column2 has both numbers set to 1. Then each number has two ones (in column1 and column2). XOR: each column has two ones => 0. Works.
b = [3,1] S=4 even, M=3, 2M=6 >4, not good. Indeed, can't. b = [3,3] S=6 even, M=3, 2M=6<=6, good. We can pair: each number three ones. Need a 3-regular multigraph on 2 vertices? That's possible: have three parallel edges between the two vertices. That gives each vertex degree 3. Works.
b = [3,2,1] S=6 even, M=3, 2M=6<=6, good? Check: degrees 3,2,1. Sum=6. Graphical? Condition: max=3 <= sum/2=3 ok, sum even. Is there a multigraph without loops? We need to assign edges such that degrees are 3,2,1. This is possible? Let's try: vertices A(3), B(2), C(1). We need total 3 edges (since each edge contributes 2 to sum). So number of edges = sum/2 =3. We need to allocate degrees: A 3, B 2, C 1. This is possible: edge AB, AB, AC? That gives A:2+1=3, B:2, C:1. Works. So good.
b = [4,1,1] S=6 even, M=4, 2M=8>6, not good. Indeed.
Thus condition seems correct.
Now, check if any nuance about zero popcount? Not applicable.
Thus answer algorithm stands.
Now, consider performance: For each l, we iterate at most 60 steps. But 60 is constant, so O(n). Actually O(60n) ~ 18 million, fine.
But we must be careful: 60 is derived from max popcount of a_i. Since a_i up to 10^18, popcount <= 60. But could there be numbers with popcount 0? Not.
Implementation details:
-
Use __builtin_popcountll for 64-bit.
-
Compute b[i] as int.
-
Compute total_even:
long long cnt[2] = {1,0}; // pref[0]=0
int cur = 0;
for i=1..n:
cur ^= (b[i] & 1); // parity addition mod2? Actually we need sum modulo 2, so we can do cur = (cur + b[i]) % 2.
But b[i] could be >1, so we need mod2 addition. We can just do cur = (cur + b[i]) & 1.
cnt[cur]++;
Then total_even = cnt[0](cnt[0]-1)/2 + cnt[1](cnt[1]-1)/2.
-
Compute bad_even:
long long bad = 0;
for (int l = 0; l < n; l++) { // 0-indexed
int sum = 0;
int maxb = 0;
for (int r = l; r < n && r - l < 60; r++) { // length <=60
sum += b[r];
if (b[r] > maxb) maxb = b[r];
if ((sum & 1) == 0) {
if (2*maxb > sum) {
bad++;
}
}
}
}
But careful: The condition r-l < 60 means we consider lengths up to 60? Since r-l is the difference, length = r-l+1. For length 60, we need r-l <=59. So to include length exactly 60, we need r-l <=59. But we argued length 60 may not be bad? Actually length 60 could be bad if M=60 and sum<120? But with M=60, sum minimal = 60 + 591 = 119 if all others are 1. But 119 < 120, so violation possible. However, is that achievable? To have max=60 and others all 1, we need at least one element with popcount 60, and 59 other elements each popcount 1. That's possible only if we have 60 elements in the subarray. So length=60, with one 60 and 59 ones. sum=119, which is odd. So not even. Could sum be even and less than 120? For even sum, we need sum even. Minimal even sum given constraints? If we have one 60 and 59 ones, sum=119 odd. To get even, we need to change some of the ones to something else? But we can only change popcounts by swapping bits? Wait b_i are fixed popcounts from original numbers. They are given. So we need to see if there exists a subarray of length 60 where the max is 60 and sum is even and less than 120. Since sum would be 60 + sum of others. Others sum is at least 59 (if all ones). To get even total, 60 + others sum even => others sum even. Minimal others sum with even parity and at least 59 is 60 (if we replace one of the ones with a popcount 2). So sum = 120, which is not < 120. Next possibility: Others sum = 58? But cannot because there are 59 others, each at least 1, so sum >=59. For sum even and <120, possible others sum = 59? 59 is odd, total 119 odd. Others sum = 61 would be 121 odd. So the smallest even total with max=60 and length 60 is 60+60=120 (if others sum =60). So sum=120. That's equal to 2M. So condition holds (2M <= sum). So no violation. For length 60, could we have max=59? Then 2M=118. Minimal sum with max=59 and length 60: 59 + 591 = 118 (if all others are 1). That's even? 59+59=118 even. So sum=118, which is < 2M? 2M=118, sum=118 => not >, condition holds equality. So violation requires strict >. So length 60 cannot produce strict > because minimal sum for given M and length L is M + (L-1)*1 = M+L-1. For violation we need M+L-1 < 2M => L-1 < M => L <= M. For L=60, this would require M >= 60? Actually L <= M, so if L=60, M must be at least 60. But as we saw, with M=60, minimal sum = 60+59=119, which is odd, and the next even sum is 120, which equals 2M. So violation not possible for length=60 because for M=60, minimal even sum