【ATcoder】Xor Sum 2

题目大意:给定一个 N 个点的序列,求有多少个区间满足\(\oplus_{i=l}^ra[i]=\sum\limits_{i=l}^ra[i]\)

题解:
小结论:\(a\oplus b=a+b\rightarrow a\&b=0\)
对每个点来说,考虑向右延伸能够满足条件的右端点的位置,显然右端点的位置单调不减,双指针扫一遍即可。

代码如下

#include <cstdio>
using namespace std;
const int maxn=2e5+10;

int n,a[maxn];
long long ans;

void solve(){
	scanf("%d",&n);
	for(int i=1;i<=n;i++)scanf("%d",&a[i]);
	for(int l=1,r=1,now=0;l<=n;){
		while(r<=n&&now+a[r]==(now^a[r]))now^=a[r++];
		ans+=r-l,now^=a[l++];
	}
	printf("%lld\n",ans);
}

int main(){
	solve();
	return 0;
}
posted @ 2019-03-01 21:51  shellpicker  阅读(173)  评论(0编辑  收藏  举报