ARC 098 D - Xor Sum 2

Problem Statement

There is an integer sequence A of length N.

Find the number of the pairs of integers l and r (1≤lrN) that satisfy the following condition:

  • Al xor Al+1 xor … xor Ar=Al + Al+1 + … + Ar

Here, xor denotes the bitwise exclusive OR.

 

Definition of XOR

 

Constraints

  • 1≤N≤2×105
  • 0≤Ai<220
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A1 A2  AN

Output

Print the number of the pairs of integers l and r (1≤lrN) that satisfy the condition.

Sample Input 1

4
2 5 4 6

Sample Output 1

5

(l,r)=(1,1),(2,2),(3,3),(4,4) clearly satisfy the condition. (l,r)=(1,2) also satisfies the condition, since A1 xor A2=A1 + A2=7. There are no other pairs that satisfy the condition, so the answer is 5.

Sample Input 2

9
0 0 0 0 0 0 0 0 0

Sample Output 2

45

Sample Input 3

19
885 8 1 128 83 32 256 206 639 16 4 128 689 32 8 64 885 969 1

Sample Output 3

37


发现 xor 0是没有影响的,所以可以把0忽视掉(用一个链表一样的东西)。
因为一旦有两个数 a[i] & a[j] != 0,那么就是不满足的,所以不算0的话长度一定不超过20,所以直接暴力做就行了。

#include<cstring>
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<ctime>
#define ll long long
using namespace std;
const int maxn=200005;

int n,a[maxn],lef[maxn];
ll ans=0;

int main(){
	scanf("%d",&n);
	for(int i=1,las=0;i<=n;i++){
		scanf("%d",a+i),lef[i]=las;
		if(a[i]) las=i;
	}
	
	for(int i=1,now,j;i<=n;i++){
		now=a[i];
		
		for(j=lef[i];j;now^=a[j],j=lef[j]) if(now&a[j]) break;
		
		ans+=(ll)(i-j);
		
	}
	
	cout<<ans<<endl;
	return 0;
}

  

 

猜你喜欢

转载自www.cnblogs.com/JYYHH/p/9095418.html