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 ;
}

AtCoder Beginner Contest 098 D - Xor Sum 2的更多相关文章

  1. AtCoder Regular Contest 098 D - Xor Sum 2 区间异或=相加 DP思想

    题意:给出n个数,求它的连续子序列中,满足下列公式,(l,r)的对数有多少对 Al xor Al+1 xor … xor Ar=Al + Al+1 + … + Ar 思路:由题意可以得到,连续子序列, ...

  2. Atcoder Beginner Contest 121 D - XOR World(区间异或和)

    题目链接:https://atcoder.jp/contests/abc121/tasks/abc121_d 题目很裸(Atcoder好像都比较裸 就给一个区间求异或和 n到1e12 肯定不能O(n) ...

  3. AtCoder Beginner Contest 179 E - Sequence Sum (模拟)

    题意:\(f(x,m)\)表示\(x\ mod\ m\),\(A_{1}=1\),而\(A_{n+1}=f(A^{2}_{n},M)\),求\(\sum^{n}_{i=1}A_{i}\). 题解:多算 ...

  4. AtCoder Beginner Contest 146_E - Rem of Sum is Num

    预处理即可 我们要找的是 (f[i] - f[j]) % k == i - j 移项可得 f[i] - i = f[j] - j 在 i - j <= k 的条件下 因此题目变成了,对于每个右端 ...

  5. AtCoder Regular Contest 098

    AtCoder Regular Contest 098 C - Attention 题意 给定一个只包含"E","W"字符串,可以花一的花费使他们互相转换.选定 ...

  6. AtCoder Beginner Contest 100 2018/06/16

    A - Happy Birthday! Time limit : 2sec / Memory limit : 1000MB Score: 100 points Problem Statement E8 ...

  7. AtCoder Beginner Contest 053 ABCD题

    A - ABC/ARC Time limit : 2sec / Memory limit : 256MB Score : 100 points Problem Statement Smeke has ...

  8. AtCoder Beginner Contest 137 F

    AtCoder Beginner Contest 137 F 数论鬼题(虽然不算特别数论) 希望你在浏览这篇题解前已经知道了费马小定理 利用用费马小定理构造函数\(g(x)=(x-i)^{P-1}\) ...

  9. AtCoder Beginner Contest 154 题解

    人生第一场 AtCoder,纪念一下 话说年后的 AtCoder 比赛怎么这么少啊(大雾 AtCoder Beginner Contest 154 题解 A - Remaining Balls We ...

随机推荐

  1. [coci2015-2016 coii] torrent【树形dp 二分】

    传送门:http://www.hsin.hr/coci/archive/2015_2016/ 进去之后点最下面那个. 这道题没有想出来,可惜了,其实不难的. 题目是两个“源”的,我们先考虑单源的问题. ...

  2. Java EE学习笔记(四)

    Spring的数据库开发 1.Spring JDBC 1).Spring JDBC模块的作用:Spring的JDBC模块负责数据库资源管理和错误处理,大大简化了开发人员对数据库的操作,使得开发人员可以 ...

  3. HDU 5869 Different GCD Subarray Query 树状数组 + 一些数学背景

    http://acm.hdu.edu.cn/showproblem.php?pid=5869 题意:给定一个数组,然后给出若干个询问,询问[L, R]中,有多少个子数组的gcd是不同的. 就是[L, ...

  4. (转)MySql的常用的语句

    MySQL常用语句 一.连接MySQL 格式: mysql -h 主机地址 -u 用户名 -p 用户密码 1.例1:连接到本机上的MYSQL. 首先在打开DOS窗口,然后进入目录 mysql bin, ...

  5. localStorage 和 sessionStorage的区别

    存储对象: 在主流浏览器中,添加了html5  Web Storage API 的接口,storage是一个存储对象,它包括会话存储(session storage)或本地存储(local stora ...

  6. hihocoder1766 字符串问题

    思路: 不断贪心增加即可. 实现: #include <iostream> #include <cstring> using namespace std; ][]; int m ...

  7. 引用library之——带有自定义属性的自定义控件的library包

    一般来讲,当自定义一个控件Panel并且此控件有自定义属性时(例如:panel:closedHandle="@drawable/foot_bar_right"),xml中需要定义此 ...

  8. MyBatis插入数据之后返回插入记录的id

    MyBatis插入数据的时候,返回该记录的id<insert id="insert" keyProperty="id" useGeneratedKeys= ...

  9. 修改本地dns域名对应的 ip

    C:\Windows\System32\drivers\etc 打开 hosts 文件 在浏览器 访问 http://a.com,就相当于访问 127.0.0.2 这个ip了

  10. 在eclipse中查看你用的tomcat的路径

    在eclipse中查看你用的tomcat的路径   打开eclipse,选择window->Preferences->Server->Runtime Environments选择你的 ...