We need to count subarrays a[l..r] with length >=3 such that a[l] XOR a[r] = sum_{i=l+1}^{r-1} a[i].
Let n <= 2e5, a_i up to 2^30, but sums can be large (up to n * 2^30) however XOR result is bounded by up to 2^30-1? Actually XOR of two numbers < 2^30 gives < 2^30. So if sum of middle elements equals that XOR, the sum must also be < 2^30. But the sum of many numbers could exceed 2^30; but if it equals XOR, it must be within that bound. So only subarrays where sum of middle elements < 2^30 are possible. Since a_i are positive integers (>=1), the sum grows with length. So length can't be too large: because sum >= (r-l-1) * 1. So we need r-l-1 < 2^30. That's huge, but we have n up to 2e5, so all lengths possible. However, the sum could be large and not equal to XOR, but we need equality.
Naively O(n^2) is impossible. Need to find smarter method.
Let’s define prefix XOR and prefix sums? The condition: a[l] XOR a[r] = (prefix_sum[r-1] - prefix_sum[l]) where prefix_sum[i] = sum_{1..i} a_i. But we have XOR of two numbers. Could we transform?
Observation: For any subarray, let s = sum_{i=l+1}^{r-1} a_i. We need a[l] XOR a[r] = s. Since s >= r-l-1 (minimum). Also a[l] XOR a[r] <= max(a[l], a[r]) *? Actually XOR of two numbers < 2^30, so s < 2^30. Thus r-l-1 < 2^30, which is always true for n up to 2e5. So not limiting.
But maybe we can use property: Since a_i are positive, the sum grows quickly. For fixed l, as we increase r, sum_{mid} increases, while XOR is bounded by around max value of a (up to ~1e9). Actually a_i < 2^30 ≈ 1e9, so XOR also < 1e9. So the sum cannot exceed about 1e9. Since a_i >= 1, the length of middle part is at most ~1e9 which is huge, but because we have only n=2e5, the sum can exceed 1e9 if many numbers are large. However, the sum can exceed 1e9 for large subarrays, but if it equals XOR, it must be less than 1e9. So for a fixed l, only a limited number of r values can satisfy the sum < 2^30? Actually we need equality, not just sum < 2^30, but the sum must equal a[l] XOR a[r] which is < 2^30, so sum < 2^30 is necessary. So the sum of middle elements must be less than 2^30. Since each a_i >= 1, the number of middle elements cannot exceed 2^30 - 1. That's huge, but again n only 2e5, so no practical limit. However, if a_i can be large, the sum might exceed 2^30 quickly. For typical values, 2^30 ≈ 1.07e9. Sum of 2e5 numbers each up to 1e9 can be huge. But we only consider subarrays where sum of middle elements is less than 2^30. That restricts the length for a given l to relatively small r, because sum grows. Since a_i are positive, for a fixed l, as r increases, sum_{l+1}^{r-1} increases monotonically. When it exceeds 2^30, further r will have sum >2^30, so equality can't hold because XOR is less than 2^30. But careful: could XOR also be larger than 2^30? No, since a_i < 2^30, XOR < 2^30. So sum must be less than 2^30. So for each l, we only need to consider r such that sum_{mid} < 2^30. That number might be limited because sum of consecutive numbers grows; but worst-case if a_i = 1, then sum_{mid} = r-l-1. So we need r-l-1 < 2^30. That's 1e9, but n is only 2e5, so all r are allowed. So this bound doesn't cut. But if a_i are 1, the sum grows slowly; but still maximum length is n, so r-l-1 ≤ n ≈ 2e5, which is far less than 2^30. So the bound is not restrictive. So we cannot rely solely on sum bound to limit search.
Alternative approach: The condition a[l] XOR a[r] = sum of middle. Since the sum is positive, we have a[l] XOR a[r] >= 1. So we need l and r such that the sum of interior equals the XOR. This is reminiscent of problems like "count subarrays where sum equals xor" but with borders. Actually typical problem: count subarrays where sum equals xor of all elements. That's known to have O(n log max) using the fact that for positive numbers, sum >= xor, and as we extend, the sum increases faster, so only O(log max) length for each start. But here we have only sum of middle (excluding borders) and xor of borders.
We could think of fixing l and then scanning r to the right, maintaining sum of interior. The interior sum initially when r=l+2: sum = a[l+1]. So condition: a[l] XOR a[l+2] == a[l+1]. So length-3 subarrays are easy to check.
As r increases, sum increases by a[r-1] (since new interior includes the previous last interior element? Actually when we increase r to r+1, new interior includes a[r] as additional element? Wait: For subarray [l..r], interior is indices l+1,...,r-1. So when we fix l and increase r by 1, interior gains a[r-1]? Let's define: For given l, define r > l+1. When we go from r to r+1, the new interior indices are l+1..r, so it adds a[r] (since previously interior ended at r-1, now includes a[r] as the new last interior? Actually previous interior: l+1..r-1; new interior: l+1..r. So added element is a[r]. So sum_{mid}(l,r+1) = sum_{mid}(l,r) + a[r]. So we can maintain sum.
Condition: sum_{mid}(l,r) = a[l] XOR a[r].
For fixed l, as r increases, the left side (sum) increases, while the right side (a[l] XOR a[r]) may vary arbitrarily as a[r] changes. So not monotonic. However, we can use the fact that sum grows, but XOR is bounded (less than 2^30). So for a fixed l, once sum exceeds some threshold (like > 2^30), we can break because XOR cannot exceed 2^30. But 2^30 is about 1e9, and a_i can be up to 1e9, so sum may exceed quickly if we have large numbers. But worst-case: if all a_i = 1, sum increases by 1 each step. To reach 2^30 ≈ 1e9, we need 1e9 steps, but n only 2e5, so sum never reaches that huge bound. So break condition not helpful.
But maybe we can exploit that a[l] XOR a[r] is at most about 2 * max a_i? Actually XOR of two numbers less than 2^30 is < 2^30. So sum < 2^30. So for each l, we only need to consider r such that sum_{mid} < 2^30. If a_i are as large as 2^30-1, then with just one interior element sum could be ~1e9, so still under. Actually if interior has a single element, sum = a[l+1] which is < 2^30, so it's ok. For two interior elements, sum = a[l+1]+a[l+2] might exceed 2^30 if both are near 2^30. For example, a_i ≈ 2^30-1, sum of two ≈ 2^31 - 2, which > 2^30. So that subarray would not be good because sum > possible XOR. So for large a_i, only short subarrays can have sum < 2^30. That provides a limit: since a_i up to ~1e9, the number of interior elements before sum exceeds 2^30 is at most about 2 (if each is ~5e8, then maybe 2-3). In general, for each l, the number of r such that sum_{mid} < 2^30 is limited because the sum of positive numbers grows, and as soon as it exceeds 2^30, further r will only increase sum, so all later r will have sum >2^30, so cannot satisfy equality. So we can break.
But is it always true that for fixed l, sum_{mid} is non-decreasing as r increases? Yes, because we add positive numbers. So once sum_{mid} > 2^30, it stays >2^30 for larger r. Thus we only need to consider r up to where sum_{mid} <= 2^30. Since 2^30 is about 1e9, and a_i are positive, the number of steps until sum exceeds 2^30 is limited by the size of a_i. In worst-case, if all a_i = 1, we need 2^30 steps to exceed. But n is only 2e5, so we can't have that many steps; we have at most n-l-1 r values. So the maximum number of r we might need to check for each l is up to n-l-1, which could be O(n) leading to O(n^2). So the bound doesn't reduce worst-case complexity for small a_i.
But perhaps we can use a different technique: Since XOR and sum are both integers, but XOR is bitwise, maybe we can use meet-in-the-middle or prefix XOR? Another thought: Rearrange condition: a[l] XOR a[r] = (prefix_sum[r-1] - prefix_sum[l]). Let's denote P[i] = prefix sum up to i. Then condition: a[l] XOR a[r] = P[r-1] - P[l].
We could try to iterate over l, and for each l, we need to find r > l+1 such that P[r-1] = P[l] + (a[l] XOR a[r]). That's not simple.
Alternatively, iterate over possible r and consider l. But symmetric.
Maybe we can use the fact that a[l] XOR a[r] = sum of middle. Since sum is additive, we can think of two pointers? But XOR not monotonic.
Consider rewriting as: sum_{i=l}^{r} a_i = a[l] + a[r] + (a[l] XOR a[r])? Actually total sum from l to r = a[l] + a[r] + sum_{mid} = a[l] + a[r] + (a[l] XOR a[r]). So condition equivalent to: total sum = a[l] + a[r] + (a[l] XOR a[r]).
Not sure helpful.
Another perspective: For subarray of length 3, condition is a[l] XOR a[l+2] = a[l+1]. That can be checked O(1) per l.
For longer subarrays, the sum of interior can be large. But maybe there are not many long good subarrays because the sum grows quickly relative to XOR, and for given l, as r increases, sum increases while XOR may fluctuate but is bounded. So for small a_i values (like 1), sum increases slowly, but XOR can also take values up to maybe ~1? Actually a[l] and a[r] are small if all ones: a[l]=1, a[r]=1 => XOR=0, not allowed because sum positive; a[l]=1, a[r]=1 gives XOR=0, sum of interior positive => not equal. a[l]=1, a[r]=1 gives sum maybe 0 if interior length zero? But length≥3 so interior at least 1. So sum at least 1, so equality impossible. But a[l]=1, a[r]=2? But values are 1 only. So with all ones, no good subarray except maybe if there are different values. So worst-case input might have small values that allow many solutions? We need to think of worst-case number of good subarrays. Could be O(n^2)? Probably not, but we need algorithm better than O(n^2). Usually such problems have solution O(n log C) or O(n sqrt n) etc.
We need to count subarrays where sum of interior equals XOR of borders. Let's denote b_i = a_i. Condition: a[l] XOR a[r] = S(l+1, r-1). Since both sides are nonnegative integers, and S grows as O(length * max). But XOR is bounded by 2^30.
Idea: For fixed l, we can iterate r while S < 2^30, because once S >= 2^30, equality impossible (since XOR < 2^30). So for each l, the number of r we need to check is limited by the number of steps until S exceeds 2^30. This number could be large if a_i are very small, e.g., 1. But with a_i=1, S = r-l-1. To exceed 2^30, we need r-l-1 > 2^30, i.e., >1e9. But n is only 2e5, so S never exceeds 2^30 for any r within array. So for all r, S < 2^30? Let's compute max possible S: worst-case sum of all interior elements for l=1, r=n: sum of a[2..n-1] which is at most (n-2)*max a_i ≈ 2e5 * 1e9 ≈ 2e14, which is >2^30 (~1e9). So S can exceed 2^30. But for l fixed, as r increases, S eventually may exceed 2^30 if a_i are large enough or many numbers. For small a_i=1, S = r-l-1, max r-l-1 <= n-2 ~2e5, which is < 1e9, so S always < 2^30 for all subarrays when all a_i are 1? Actually 2e5 is less than 1e9, so yes. So for small values, the condition S < 2^30 holds for all possible r. So we cannot break early based on sum exceeding 2^30.
Thus worst-case input might be all ones (or small numbers) where the sum never exceeds the bound, and we may have many potential r to check. However, can there be many good subarrays with small numbers? Let's test small numbers. For all ones: a[l]=1, a[r]=1 gives XOR=0; interior sum is r-l-1, which is >0 for any length>=3. So equality never holds. So number of good subarrays is 0. So it's fine. But maybe there are inputs with many matches, like pattern that yields many equalities. Could be like alternating powers of two? Need to see maximum possible number of good subarrays under constraints. Possibly O(n log max) because for each l, there are only O(log max) possible r? Not obvious.
Let's try to bound the number of good subarrays. Condition: sum = XOR. Since XOR ≤ max(a[l], a[r]) * 2? Actually XOR can be up to about 2^30. But sum of interior is sum of positive integers. For fixed l, as r increases, sum increases monotonically. However, a[l] XOR a[r] changes with a[r]. The sum might equal XOR only when they happen to coincide. Could there be many r for same l? Possibly many if a[r] are chosen to adjust XOR accordingly. But XOR is at most 2^30, so if sum grows beyond 2^30, no more matches. So for each l, we only need to consider r such that sum < 2^30. That's a bound on the length of considered subarrays from l. The length (number of r) is limited by how many steps until sum exceeds 2^30. In worst-case, if a_i are very small (like 1), sum grows as length. So to exceed 2^30, we need length > 2^30, which is huge, but array length is only 2e5, so for that specific l, all r within array might have sum < 2^30. So we could potentially need to check all O(n) r for each l, leading to O(n^2). However, maybe we can argue that if a_i are small, then a[l] XOR a[r] is also small? Because if all numbers are small (bounded by some B), then XOR is also bounded by something like < 2^{⌈log2 B⌉}. But sum of many small numbers could be larger than that bound, but if they are very small, sum increases slowly. But the array length n is limited, so sum might still be within bound. For example, if all a_i = 1, sum at most 2e5, XOR of two ones is 0, which is less than sum except when sum=0. So no matches. But if numbers are like 1 and 2 arranged, could there be many matches? Let's think.
We need to analyze complexity based on value ranges. Maybe there is known solution: iterate over l, extend r while sum < 2 * max? Actually in typical "count subarrays where sum = xor" problem, we can use property that for positive integers, if we fix left index, as we extend right, the sum increases, and the xor also changes but the number of distinct prefixes where sum and xor differ in a bounded way leads to O(n log max) because sum - xor is non-decreasing? Actually for subarray sum vs xor, we have: For any subarray, sum >= xor. And as we extend, sum - xor is non-decreasing? Not exactly. But there is known technique: For each left, we move right until sum >= 2*maxbit? Wait, I recall problem from Codeforces: "1462E1" no. Actually "Good Subarrays" problems often use the fact that if sum of subarray equals xor, then the subarray length is limited because sum grows faster than xor; the maximum length for which equality can hold is about log(max). Because for any set of numbers, if the sum equals the xor, then each bit appears at most once? Not exactly.
Let's think: For a set of numbers, sum = xor if and only if there is no carry in binary addition, i.e., the numbers are such that their binary representations have disjoint sets of 1 bits. Because sum equals xor when there is no carry in addition. Actually xor is sum without carry. So sum = xor iff for each bit, at most one number has a 1 in that bit. Since numbers are positive, this condition is necessary and sufficient: sum == xor iff for every bit, the count of 1's among the numbers is 0 or 1. Because sum adds bits with carries; if any bit has two or more 1s, the xor will have 0 there but sum will have a carry, making sum > xor. So sum = xor implies that the numbers are such that their bits are disjoint.
Wait, but this condition is for the sum of a set of numbers, not necessarily a subarray? However, for any collection of integers, sum = xor if and only if there is no carry when adding them, i.e., the numbers have no common set bits. More precisely, the binary addition of numbers yields xor only if for each bit, the total count of ones is at most 1. Because addition without carry is just xor. So sum = xor iff the numbers are pairwise bitwise disjoint. Indeed, if you have multiple numbers, the sum in binary will have carries if any column sum >=2. So for sum to equal xor, each column sum must be 0 or 1.
Now in our problem, we have sum of interior elements (multiple numbers). So condition is that the sum of interior elements equals a[l] XOR a[r]. But the right side is a single number (XOR of two numbers). So we are comparing sum of a set (interior) to a single number (XOR). The condition sum = xor_of_two. That's not the same as sum = xor of the same set. So above condition does not directly apply.
But we can rearrange: a[l] XOR a[r] = sum_{mid}. Let’s denote X = a[l] XOR a[r]. Then we need sum_{mid} = X. So sum of interior equals X. So interior sum is a single number. That is just an equation, not a condition on bits of interior. However, we can also think: For a given l, we need to find r such that sum_{mid} equals the XOR of two known numbers (a[l] and a[r]). This resembles classic two-sum but with XOR.
Could we use hashing or prefix sums? For each r, we could compute prefix sum up to r-1, and we need P[r-1] - P[l] = a[l] XOR a[r]. So P[r-1] = P[l] + a[l] XOR a[r]. This is not straightforward.
Another approach: Since values are up to 2^30, we could try to fix l and r within a limited window of size about 60? Maybe because sum grows quickly relative to XOR, but as we argued for small numbers it may not. However, maybe we can use the fact that for a given l, as r increases, the sum increases by a[r-1] (which is at least 1), while the XOR changes by a new a[r] value that is not directly related. But if we consider both sides as functions of r, maybe we can bound the number of r where they can be equal because sum is growing and XOR is bounded by 2^30. But if sum is bounded by 2^30, then for each l, the number of r such that sum_{mid} <= 2^30 is limited. But if a_i are all 1, sum_{mid} = r-l-1, so to have sum <= 2^30, we need r-l-1 <= 2^30, which is huge, but array length only 2e5, so still O(n) possible. But maybe in that case, the XOR a[l] XOR a[r] can take only limited values? If all numbers are 1, then a[l] and a[r] are both 1, XOR=0 always, so condition sum=0 impossible. So no matches. But could there be a pathological case where many matches occur, and also sum is small? For many matches, we need a[l] XOR a[r] equal to sum of interior. If numbers are small, XOR also small. Could we construct many triples? For length 3, condition is a[l] XOR a[l+2] = a[l+1]. If we design array such that for many l, this holds, we could have O(n) good subarrays (since length 3 subarrays are many). But longer subarrays could also exist. Could the total number be O(n log C) or O(n sqrt n)? Perhaps the intended solution is O(n * something like 60) by noting that for a fixed l, as r increases, sum grows and soon exceeds 2^30, but if a_i are small, XOR is also small, so sum must also be small; thus r-l-1 must be small because each a_i >=1, so sum >= r-l-1. So if XOR is small (e.g., < some threshold), then r-l-1 <= XOR < threshold. That gives a bound on length! Indeed, for a given l, if a[l] XOR a[r] = S, then S is some value. Since S = sum_{mid} >= (r-l-1) * min a_i (which is at least 1). So r-l-1 <= S. So the length of the interior is bounded by S, which is the XOR value. And XOR value is at most something like 2^30, but that's not helpful. However, for a fixed l, as r changes, the XOR value a[l] XOR a[r] can vary. But importantly, if we have a specific l, the maximum possible XOR with any a[r] is bounded by about 2^30, but that's still large. However, if a[l] is small, XOR could be large if a[r] is large. But then S = XOR would be large, possibly allowing long interior. So length bound may not be small universally.
But we can bound by the fact that a[r] is also part of the array. Maybe we can use two pointers: iterate l, maintain r such that sum < some limit? Not clear.
Let's think differently: Since a[l] XOR a[r] = sum_{mid}, and both sides are non-negative, we could rewrite as a[l] XOR a[r] = (prefix_sum[r-1] - prefix_sum[l]). For each r, we need to find l such that prefix_sum[r-1] - prefix_sum[l] = a[l] XOR a[r]. Rearranged: prefix_sum[r-1] = prefix_sum[l] + a[l] XOR a[r]. This is like we need to check for each l, does there exist r with that condition. Perhaps we can precompute something: For each position i, we have prefix sum P[i] and a[i]. Then condition becomes: P[r-1] - a[l] XOR a[r] = P[l]. For fixed r, we need to find l < r-1 such that P[l] equals something that depends on a[l]. That's still messy.
Maybe we can think of the condition as: a[l] XOR a[r] = sum_{mid}. Note that sum_{mid} can be expressed as total_sum - a[l] - a[r] if we consider full sum from l to r. So total_sum = a[l] + a[r] + sum_{mid} = a[l] + a[r] + (a[l] XOR a[r]). So condition is equivalent to total_sum = a[l] + a[r] + (a[l] XOR a[r]). For any two numbers x and y, we have x + y + (x XOR y) = 2*(x & y) + (x XOR y) + (x XOR y)? Actually identity: x + y = (x XOR y) + 2*(x & y). So x + y + (x XOR y) = 2*(x XOR y) + 2*(x & y)? Wait: x + y = (x XOR y) + 2*(x & y). Then adding (x XOR y) gives x + y + (x XOR y) = 2*(x XOR y) + 2*(x & y) = 2*( (x XOR y) + (x & y) ) = 2*(x | y). Because x|y = (x XOR y) + (x & y). So total_sum = 2*(a[l] | a[r]). So condition becomes:
sum_{i=l}^{r} a_i = 2 * (a[l] | a[r]).
That's a nice simplification! Let's verify:
Let x = a[l], y = a[r].
We have x + y + (x XOR y) = x + y + (x XOR y). Using identity: x + y = (x XOR y) + 2*(x & y). Then x + y + (x XOR y) = (x XOR y) + 2*(x & y) + (x XOR y) = 2*(x XOR y) + 2*(x & y) = 2*((x XOR y) + (x & y)) = 2*(x | y). Indeed, because x|y = (x XOR y) | (x & y) = (x XOR y) + (x & y) (since they are disjoint). So it's correct.
Thus the condition reduces to:
Total sum of subarray from l to r (inclusive) = 2 * (a[l] | a[r]).
And we also have length >=3, but that is automatically if l+1 ≤ r-1, i.e., at least one element between, which is equivalent to r ≥ l+2.
So we need to count pairs (l, r) with r ≥ l+2 such that sum_{l..r} = 2 * (a[l] | a[r]).
Great! This is much simpler. Now we have a condition that relates the total sum of the subarray to the bitwise OR of the two endpoints. Since sum is at least a[l] + a[r] + something, and OR is at most something.
Now we can try to count such pairs efficiently.
Let S(l, r) = prefix_sum[r] - prefix_sum[l-1]. Condition: S(l, r) = 2 * (a[l] | a[r]).
Observations:
-
Since a[l] and a[r] are positive (<2^30), OR is also < 2^30. So right side is at most 2*(2^30-1) < 2^31, about 2e9. But total sum S(l,r) could be larger, especially for long subarrays. So equality imposes that the sum must be relatively small, bounded by about 2^31. Since n up to 2e5, the sum can be huge (up to about 2e5*1e9 = 2e14) so many pairs will have sum much larger and cannot satisfy.
-
For a fixed l, as r increases, S(l,r) increases. The right side 2*(a[l] | a[r]) can change as a[r] changes, but it's at most 2maxOR, which is bounded by about 2^31. So once S(l,r) exceeds some threshold (like 2^31), further r will have even larger sum, so they cannot satisfy equality. So for each l, we only need to consider r such that S(l,r) ≤ 2MAX_OR, where MAX_OR is the maximum possible OR, which is less than 2^31. Since a_i < 2^30, MAX_OR < 2^30, times 2 < 2^31. So threshold is about 2^31 ≈ 2.147e9. So S(l,r) must be ≤ about 2.15e9. That is a strong bound. Since S(l,r) is sum of positive integers, as we increase r, S grows. So the number of r for which S(l,r) ≤ 2.15e9 is limited because each step adds at least min a_i (which is at least 1). So at most about 2.15e9 r's? That's not helpful, but in practice a_i could be 1, so S grows slowly; however, the array length is only 2e5, so for a given l, the number of r such that S(l,r) ≤ 2.15e9 could be up to n-l+1, which could be O(n). But note that the right side 2*(a[l]|a[r]) is at least 2 * a[l] (since OR ≥ a[l]), and also at least 2 * a[r]. So 2*(a[l]|a[r]) >= 2a[l] and >= 2a[r]. So sum must be at least that. For a[l] large, say a[l]=1e9, then S(l,r) must be at least 2e9, which is near threshold. So for l with large a[l], only very few r (maybe 1 or 2) satisfy because sum quickly exceeds threshold. For l with small a[l], the threshold is still 2^31, but a[l] small means 2*a[l] maybe small, so many r might be possible as long as sum ≤ 2^31. However, sum of many numbers could still exceed 2^31 if numbers are moderate. But if all numbers are small like 1, sum S(l,r) = (r-l+1) * 1 = length. To keep S ≤ 2.15e9, length must be ≤ 2.15e9, which is true for any subarray within n=2e5. So indeed for all r we have S ≤ 2e5, well below threshold. So for small numbers, the condition S ≤ 2^31 is always true, so we can't prune.
But maybe we can use the structure of OR: 2*(a[l]|a[r]) is even (multiple of 2). So sum must be even. Not big.
We need a more efficient counting method. Let's explore the equation sum = 2*(or). Write sum = sum_{i=l}^{r} a_i. Since a_i are positive, sum is at least a[l]+a[r]. Also OR is at most a[l]+a[r]? Actually OR ≤ a[l]+a[r] (since bitwise OR ≤ sum). So 2OR ≤ 2(a[l]+a[r])? Not directly.
Another identity: a[l] + a[r] ≤ 2OR? Because OR includes all bits that are set in either, and sum may have carries. Actually x + y = (x|y) + (x&y). Since (x&y) ≥ 0, we have x+y ≥ (x|y). So 2(x|y) could be larger or smaller than x+y? For example, x=1, y=2: x|y=3, 23=6, x+y=3; so 2OR > sum. For x=3, y=5: OR=7, 27=14, sum=8. So 2OR is generally larger than sum of just the two endpoints. The total sum includes interior elements, so it could be larger.
Maybe we can brute force over one side with limited range due to OR bits? Since a_i < 2^30, we could iterate over possible OR values? But OR depends on two endpoints.
Another approach: Because sum = 2OR, and OR is an integer that is determined by bits of a[l] and a[r]. Maybe we can fix l and then for possible r, we have condition: prefix_sum[r] - prefix_sum[l-1] = 2(a[l] | a[r]). Rearranged: prefix_sum[r] - 2*a[r]? Not directly.
Let's denote P[i] = prefix sum up to i (1-indexed). Then condition: P[r] - P[l-1] = 2*(a[l] | a[r]). This can be rewritten as:
P[r] - 2a[r] = P[l-1] - 2(a[l] | a[r]) + 2*a[l]? Not helpful.
For a fixed l, we could try to iterate over possible values of OR? Since OR is bitwise OR, for fixed l, as we vary r, a[r] can take many values, and OR can change. But maybe we can bound the number of distinct OR values for a fixed l when scanning r while sum is small? However, we saw that for small a_i, sum may not be small bound.
But perhaps we can use two pointers from both ends? Not.
Maybe we can use meet-in-the-middle on bits: For each l, we need to find r such that sum_{l..r} = 2*(a[l] | a[r]). Note that the left side is at least a[l] + a[r] + (r-l-1)*min. But we also have that sum_{l..r} is at most some bound? Actually no bound except given by array.
Another idea: Since sum = 2OR, and OR is at least max(a[l], a[r]), we have sum ≥ 2max(a[l], a[r]). So the subarray must contain at least enough total sum to cover twice the larger endpoint. This might limit length for large values.
Perhaps we can derive that for a fixed l, as r increases, sum grows linearly, while 2OR grows at most to 2 (max bitwise OR with a[l]), which could be at most 2*( (2^30-1) | a[l] ) = 2* (2^30-1) if a[l] is not covering all bits? Actually the OR with a[l] can be at most (2^30-1), so 2OR ≤ 2(2^30-1) ≈ 2^31-2. So sum is bounded by about 2.15e9. Therefore, for any good subarray, the total sum must be ≤ 2.15e9. Since the sum of the whole array could be much larger, good subarrays must be relatively short if the numbers are not extremely small. Specifically, if the numbers are at least 1, the length of the subarray (number of elements) cannot exceed 2.15e9, which is not restrictive. But if the numbers are at least some minimum value m, then length ≤ 2.15e9 / m. For m=1, length can be up to 2e5, which is okay. So length could be up to entire array, but then sum would be sum of whole array, which could be >2.15e9 if numbers are moderate. For example, if average value is 10^5, sum of 2e5 numbers is 2e10 > 2e9, so whole array sum is too large. So for many arrays, good subarrays must be relatively short because sum grows quickly.
But can we quantify the maximum possible length of a good subarray? Since sum = 2OR and OR ≤ 2^30, sum ≤ 2^31. Therefore, the total sum of the subarray cannot exceed about 2.15e9. So if the array contains numbers that are not all tiny, the subarray length is limited. However, if all numbers are 1, sum of whole array (2e5) is 2e5 < 2.15e9, so whole array could be good if condition holds. But condition is specific: sum = 2OR. For all ones, OR = 1, 2*OR = 2, but sum = length, so for length>2 not equal. So whole array not good. But length could be up to 2e5 and still sum ≤ 2e5 < 2.15e9, so length not restricted.
Thus we cannot rely on length bound for all cases.
We need an algorithm that runs within O(n * something like log max) by exploiting the bound on sum, but also handle small numbers efficiently. Could we use a technique where we iterate over l and consider r up to some point where sum exceeds 2maxOR? Since maxOR is at most 2^30-1, we have threshold T = 2(2^30-1) ≈ 2.147e9. For each l, we can extend r while sum_{l..r} ≤ T. Since sum increases, once sum > T, further r will have sum > T and cannot satisfy (since 2*OR ≤ T). So we only need to consider r such that sum_{l..r} ≤ T. For each l, the number of such r is at most the number of steps until sum exceeds T. Since each step adds a positive integer, the number of steps is at most T / min_ai (where min_ai is the minimum in the array). In worst-case min_ai=1, this could be up to 2e9, but array length limited, so maximum steps is n-l+1, which is up to 2e5. So for each l, we may need to examine up to O(n) r if min_ai=1? Wait, if min_ai=1, sum grows by at least 1 each step. To exceed T=2.15e9, you need over 2.15e9 steps, but the array only has 2e5 steps total. So for any l, the number of r from l to n such that sum ≤ T could be all of them (up to n-l+1), because the total sum from l to n may be less than T. For example, if all a_i=1 and n=2e5, sum_{l..n} = n-l+1 ≤ 2e5, which is < T. So indeed, for each l, all r≥l are considered (since sum never exceeds T). That would be O(n^2) if we check all pairs.
But maybe in such a scenario (all ones), there are no good subarrays, so we could break early if condition fails? But we still need to check potential matches. We could try to find a smarter way to count matches without enumerating all pairs.
The condition S = 2*(a[l]|a[r]) is reminiscent of an equation that can be rearranged as: S/2 = a[l]|a[r]. Since S is sum, and OR is integer. Because S must be even. So we can check if S is even, then compute OR = S/2, and require that OR = a[l] | a[r]. So for each pair (l,r) with sum even, we need that the bitwise OR of a[l] and a[r] equals S/2.
Given l and r, OR is determined by a[l] and a[r]. Could we, for each l, maintain a map from OR to something? Not easy because OR depends on both.
Another angle: Since OR = a[l] | a[r], that means that all bits set in a[l] or a[r] are exactly the bits set in S/2. Conversely, S/2 must have at least the bits of a[l] and a[r]. Also, a[l] and a[r] must be subsets (in bitwise sense) of S/2, and their union must be exactly S/2. So given a[l] and S/2, we require that a[r] is such that a[l] | a[r] = S/2. That is equivalent to: a[r] is a subset of S/2, and contains all bits of S/2 that are not in a[l]. In other words, a[r] must satisfy (a[r] & ~a[l]) = (S/2) & ~a[l] (i.e., the bits missing from a[l] must be set in a[r]), and also a[r] must not have any bits outside S/2. So a[r] must be of the form: a[r] = (S/2) & ~a[l] union any subset of bits of a[l]? Actually to get OR exactly S/2, we need that a[r] does not have any bits not in S/2, and must have all bits of S/2 that are not in a[l]. Bits that are in a[l] can be either 0 or 1 in a[r] — they don't affect OR because they are already covered by a[l]. However, if a[r] has a bit that is set in a[l], it's okay; but if a[r] has a bit not in S/2, OR would have that extra bit, making OR larger than S/2, not allowed. So condition: (a[r] & ~S/2) = 0, and (a[r] | a[l]) = S/2, which is equivalent to (a[r] & ~a[l]) must equal (S/2 & ~a[l]), and also a[r] must be a subset of S/2. So a[r] is determined uniquely in terms of bits not in a[l]: those bits must be present. Bits in a[l] can be arbitrary as long as a[r] ⊆ S/2? Actually if a[r] sets a bit that is already in a[l], it doesn't affect OR. But it must not set bits outside S/2. So a[r] can be any number such that:
- a[r] ⊆ S/2 (i.e., (a[r] | S/2) == S/2, or a[r] & ~(S/2) = 0)
- (a[r] & ~a[l]) = (S/2 & ~a[l]) (i.e., the bits that are in S/2 but not in a[l] must be set in a[r]; bits that are in a[l] can be 0 or 1, but subject to a[r] ⊆ S/2).
So a[r] must have exactly the bits of (S/2 \ a[l]) forced to 1; for bits in a[l] ∩ S/2, they may be 0 or 1. So there are 2^{popcount(a[l] & S/2)} possibilities for a[r]. But a[r] is a specific array value, not any number; we need a[r] to equal one of the array entries. So for fixed l and given S, we need to count how many r > l+1 such that a[r] satisfies the above condition and also sum condition S = prefix_sum[r] - prefix_sum[l-1] (which is the total sum). But S depends on r, so it's not fixed independently.
Alternative: For each l, we can iterate r while sum <= T, compute S = sum_{l..r}, check if S even, and then check if a[l] | a[r] == S/2. That's straightforward, and in worst-case we may need to check O(n^2) pairs if T is large relative to array sum. But perhaps we can argue that for each l, the number of r to check is O(log C) or O(sqrt(n)) due to some constraints. Let's examine more carefully.
We have sum = S(l,r) and condition S(l,r) = 2*(a[l]|a[r]). Since a[l]|a[r] is at least max(a[l], a[r]), we have S(l,r) >= 2max(a[l], a[r]). Also S(l,r) = sum of all elements from l to r, which is at least a[l] + a[r] + (r-l-1)min. But more importantly, S(l,r) is at least a[l] + a[r]. However, 2max could be much larger than a[l]+a[r] if one endpoint is large relative to the other. For example, a[l]=1, a[r]=1000, then 2max=2000, while a[l]+a[r]=1001. So S must be at least 2000. So the subarray must have additional sum of at least 2000 - (a[l]+a[r]) = max - min? Actually 2*max - (a[l]+a[r]) = max - min. So interior must provide at least max - min sum. So for large differences, the subarray must be long enough to accumulate that sum.
On the other hand, if a[l] and a[r] are both small, say 1 and 2, then 2*max=4, a[l]+a[r]=3, so interior must sum at least 1. That's easy.
Now, think about the maximum possible length of a good subarray given that sum ≤ 2^31. If numbers are as small as 1, length can be up to 2^31, but n is only 2e5, so length could be entire array. However, is it possible to have many good subarrays when numbers are all 1? Let's test: a[l]=1, a[r]=1 gives OR=1, 2OR=2, sum = r-l+1. So condition is r-l+1 = 2, but we need at least 3 elements, so impossible. For a[l]=1, a[r]=2, OR=3, 2OR=6, sum = length (since all ones?) Wait if all ones, a[r] would be 1, not 2. So need mixed values.
Consider array of all 1's except occasional numbers. But good subarrays require sum = 2OR. Since OR is at least max, sum must be at least 2max. With many 1's, sum roughly equals length, so length must be about 2max. If max is large, length must be large; if max is small, length must be small (like 2max). For small max, length is small, so subarrays are short. So maybe the total number of good subarrays is bounded by O(n * log(max))? Let's try to prove a bound.
Idea: For a fixed l, as r increases, sum S increases, while 2OR is at least 2a[l] (since OR >= a[l]) and also at least 2a[r]. But a[r] may vary. However, note that S(l,r) >= a[l] + a[r] + (r-l-1) * min, but more simply S(l,r) grows as O((r-l+1)avg). Meanwhile 2OR is bounded by 2 (max value in array) which is at most 2*(2^30-1). So S(l,r) will exceed that bound for sufficiently large r if the average is not too small. But if all numbers are 1, average is 1, and bound is about 2e9, so S(l,n) ≈ n-l+1 ≤ 2e5 << 2e9, so bound never exceeded. So in that worst-case scenario, S(l,r) is always below the bound, so we might have to check all O(n^2) pairs? But maybe in that scenario, the condition is very restrictive and can be checked quickly using other properties? For all ones, we saw no good subarrays because OR =1 and 2OR=2, but sum >=3 for any subarray length≥3, so no matches. But we still need to confirm that for all ones, we can quickly determine that no subarray works without checking all pairs. But what about a mix of small numbers that could produce many matches? Could there be an input that yields Θ(n^2) good subarrays? Let's think: For each pair (l,r) with l+2 ≤ r, we need sum = 2OR. Could there be a family where this holds for many pairs? For example, if we set a[i] = 0? But a[i] >=1. Perhaps if we set all a[i] = 0? Not allowed. If we set all a[i] = 2^k? Then OR of two powers of two is sum of them if distinct bits, else same bit gives that bit. For two equal powers, OR = that power. For distinct powers, OR = sum. So 2OR would be 2(power) or 2*(sum). Sum of subarray includes many numbers. Hard to get many matches.
Let's try to see maximum possible number of good subarrays. The problem constraints n=2e5, so O(n^2) is too slow. Usually such problems have solution O(n * log max) or O(n * sqrt(max)). I suspect the intended solution uses the bound that sum = 2OR implies that sum is not too large and also that the length of the subarray is limited because OR is at most 2max(a[l], a[r])? Actually OR ≤ a[l] + a[r]? Not always: 1|2=3 >1+2? 1+2=3, equal. In general, OR ≤ a[l] + a[r] because a[l]+a[r] includes OR plus AND. Actually x+y = (x|y)+(x&y) >= x|y. So OR ≤ sum of the two numbers. Thus 2OR ≤ 2(a[l]+a[r]) = 2a[l]+2a[r]. So sum = 2OR ≤ 2a[l]+2a[r]. But sum also includes interior elements. So interior sum = sum - a[l] - a[r] = 2OR - a[l] - a[r] ≤ (2a[l]+2a[r]) - a[l] - a[r] = a[l]+a[r]. So interior sum ≤ a[l]+a[r]. That's an interesting bound: sum_{mid} ≤ a[l] + a[r]. Since interior sum is nonnegative, we have 0 ≤ sum_{mid} ≤ a[l] + a[r]. Moreover, from the original condition sum_{mid} = a[l] XOR a[r], and we know a[l] XOR a[r] ≤ a[l] + a[r] (since XOR ≤ sum), consistent.
Now, interior sum = a[l] XOR a[r] ≤ a[l] + a[r]. But more importantly, we also have sum_{mid} = 2OR - a[l] - a[r]. Since OR ≤ a[l]+a[r], we have 2OR - a[l]-a[r] ≤ a[l]+a[r]. That's same.
This inequality gives a bound on the interior sum in terms of the endpoints. For a given l, as r increases, interior sum increases (since we add more elements). But we also have that interior sum ≤ a[l] + a[r]. So for the subarray to be good, it must satisfy that the sum of all interior elements (which grows with r) is at most a[l] + a[r]. Since a[r] can vary, but note that a[l] + a[r] is at most 2max(a[l], a[r]). So the interior sum cannot exceed the sum of the two endpoints. This is a very strong restriction! Because interior sum is sum of positive integers between l and r (excluding endpoints). For the subarray to be good, the sum of all those numbers must be ≤ a[l] + a[r]. Since each interior number is at least 1, the number of interior elements (r-l-1) must satisfy r-l-1 ≤ a[l] + a[r]. So the length of the subarray is bounded by a[l] + a[r] + 2. But a[l] + a[r] could be up to 2(2^30-1) ≈ 2e9, but in practice, a_i are up to about 1e9, but n is only 2e5, so that bound is not restrictive. However, more importantly, since interior sum ≤ a[l] + a[r], and interior sum itself is at least the number of interior elements (if each at least 1), we get r-l-1 ≤ a[l] + a[r]. That's a bound on distance between l and r. But still could be large if a[l]+a[r] is large (up to 2e9). But we also have interior sum = a[l] XOR a[r] which is typically smaller than a[l]+a[r]. Actually XOR can be as high as about max(a[l],a[r])2? For example, 2^29 and 2^30? XOR can be near sum in some cases. But maybe we can derive a stronger bound: Since XOR ≤ max(a[l], a[r]) * 2? Actually for two numbers, max XOR is when bits are complementary, which can be up to (2^{k+1}-1) if both numbers have bits up to k. So XOR can be up to about 2max(a[l], a[r]) - 1? Example: a[l]=2^k, a[r]=2^k-1 gives XOR = 2^k + (2^k-1) = 2^{k+1}-1, which is about 2max-1. So XOR ≤ 2max(a[l], a[r]). Then interior sum = XOR ≤ 2max. So r-l-1 ≤ 2max(a[l],a[r]) (since each interior at least 1). So distance is bounded by 2*max. Since max(a[l],a[r]) can be up to 2^30, that still allows up to 2e9, not helpful.
But maybe we can use that for a fixed l, as r increases, a[r] can be large, but then interior sum must be ≤ a[l] + a[r] and also = a[l] XOR a[r]. Note that a[l] XOR a[r] is at most a[l] + a[r] (always), but often it is much smaller, especially if numbers share bits. Could we bound the number of r for a given l by something like O(log max) based on the fact that interior sum grows and a[l] XOR a[r] is limited? Not obviously.
Let's consider another transformation: Using the identity sum = 2*(a[l]|a[r]), we have sum/2 = a[l] | a[r]. Since sum is sum of numbers from l to r, it can be expressed as P[r] - P[l-1]. So P[r] - P[l-1] = 2 * (a[l] | a[r]). For fixed l, we can think of scanning r and checking if (P[r] - P[l-1]) is even and equals 2*(a[l] | a[r]). This is like an equation where right side depends on a[r]. Could we precompute for each possible OR value the positions where a[r] yields that OR with a[l]? Since a[l] is fixed, for each possible a[r], OR = a[l] | a[r]. So the condition becomes: P[r] - P[l-1] = 2 * (a[l] | a[r]). That is P[r] = P[l-1] + 2*(a[l] | a[r]). So for each r, we need to check if there exists l such that ... Not easy.
Maybe we can iterate over r and maintain something for l? For each r, we need to count l such that sum(l,r) = 2*(a[l]|a[r]). That's symmetric.
Given the constraints, typical approach for problems where sum of subarray is bounded by something like 2*max is to use two pointers with the observation that for fixed left, as we move right, the sum increases and the max also might increase, but here the condition involves OR of endpoints, not max of subarray.
Wait, perhaps we can use meet-in-the-middle on bits: Because a[i] < 2^30, the number of distinct values of a[i] might be limited? Not necessarily; up to 2e5 distinct.
But maybe we can use a hash map for each possible OR? That seems large.
Another idea: Because the condition sum = 2*(a[l]|a[r]) and sum is the total sum of the subarray, which includes both endpoints and interior. If we think about binary representation, note