D - Xor Sum 2


Time limit : 2sec / Memory limit : 1024MB

Score : 500 points

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

Copy
4
2 5 4 6

Sample Output 1

Copy
5

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


Sample Input 2

Copy
9
0 0 0 0 0 0 0 0 0

Sample Output 2

Copy
45

Sample Input 3

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

Sample Output 3

Copy
37

 #include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <string>
#include <deque>
using namespace std;
#define ll long long
#define N 200009
#define gep(i,a,b) for(int i=a;i<=b;i++)
#define gepp(i,a,b) for(int i=a;i>=b;i--)
#define gep1(i,a,b) for(ll i=a;i<=b;i++)
#define gepp1(i,a,b) for(ll i=a;i>=b;i--)
#define mem(a,b) memset(a,b,sizeof(a))
int n,a[N];
ll sum[N],b[N];
/*
要想相加和等于异或和,就要区间的数二进制的每一位1只有一个
例如 1 2 4 8
如果 [l,r] 不可以,那么l++
如果[l,r] 可以,那么 r++
*/
int main()
{
scanf("%d",&n);
gep(i,,n) {
scanf("%d",&a[i]);
sum[i]=sum[i-]+a[i];
b[i]=b[i-]^a[i];
}
int l=;
ll ans=;
gep(r,,n){
for(;sum[r]-sum[l-]!=(b[r]^b[l-]);l++);//!=的优先级大于^,因此要加()
ans+=(r-l+);
//[l,r] :每一次的可以就是多了[l,r],[l+1,r],[l+2,r]……[r,r]共(r-l+1)个区间
//可以一定是连续的,一旦不可以了就要改变r,l
}
printf("%lld\n",ans);
return ;
}

最新文章

  1. Reversing Linked List
  2. iOS知识总结
  3. oracle select into 的时候提示未找到数据
  4. HTML编码规则、CSS属性声明顺序--简介
  5. spring mvc配置文件dispatcher-servlet.xml详解
  6. MVC使用的MetaModel代码生成器模板
  7. platform平台设备驱动简化示例代码
  8. window FILES——windows文件管理相关实例
  9. 201521123059 《Java程序设计》第七周学习总结
  10. Checksum软件的简单设计
  11. python使用随机的163账号发送邮件
  12. $Django python中使用redis, django中使用(封装了),redis开启事务(管道)
  13. Top值
  14. canvas简易画板。
  15. CentOS 与 Ubuntu 使用命令搭建 LAMP 环境
  16. Kali linux创建和删除用户
  17. Nuget CsvHelper 的使用
  18. uva1659(最大费用循环流)
  19. Oracle EBS PO rcv_shipment_headers 数据缺失
  20. 深入源码分析Java线程池的实现原理

热门文章

  1. shell脚本解析json文件
  2. hihocoder1860 最大异或和
  3. 安装ubuntu虚拟环境
  4. uvm_base——打好你的基础
  5. 解决在安装Fiddler4.6版本后,在手机上安装证书出现的问题解决方法
  6. React学习实例总结,包含yeoman安装、webpack构建
  7. dlopen与dlsym用法
  8. php 小坑记录
  9. php接口开发注意事项
  10. Gym 100342E Minima (暴力,单调队列)