跳到正文

题解

题解 AT4142 【[ARC098B] Xor Sum 2】

Cylete约 1 分钟
原文
文章信息与版本

文章信息

发布
2020年11月20日
收录
2026年10月14日
更新
2026年11月14日
篇幅
约 1 分钟
获赞
14

版本

  1. v1当前

感觉其他题解没讲清楚

我们考虑什么时候

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

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

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

再用尺取法就比较显然了

C++
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;
}
原文
抓取于
  1. JodieZhou

    “AtCoder RemoteJudge 暂不可用,详见置顶帖”--我这样是怎么回事?