题解 AT4142 【[ARC098B] Xor Sum 2】

· · 题解

感觉其他题解没讲清楚

我们考虑什么时候

A[l] xor A[l + 1] xor … xor A[r] = A[l] + A[l + 1] + … + A[r] = s

对于s二进制中每个位置的1,在A[l] A[l + 1] … A[r]中只能出现一次

因为出现两次异或会变0,而加法会进位,此时就不相等了

再用尺取法就比较显然了

const int N = 2e5 + 10;

int n, s1, s2, ans;
int a[N];

signed main()
{
    read(n);
    for(R int i = 1; i <= n; i++) read(a[i]);
    for(R int i = 1, j = 0; i <= n; i++)
    {
        while(j + 1 <= n && s1 + a[j + 1] == (s2 ^ a[j + 1])) j++, s1 += a[j], s2 ^= a[j];
        ans += j - i + 1;
        s1 -= a[i];
        s2 ^= a[i];
    }
    writeln(ans);
    return 0;
}