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

题意:
给你一个含有N个整数的数组,
让你求出有多少对l和r,使之 a[l]+...+a[r] = a[l]^... ^a[r]
即让你找出有多少对l和r,使数组的l~r的前缀和和异或和相等。 思路:
我们知道这样的结论,
对于一个数组,他的异或前缀和有和sum前缀和类似的性质。
即我们用一个数组 prexor[i]来维护从1~i数组的异或值,那么 l~r的异或值可以表示为
prexor[r]^prexor[l-1] 并且异或和sum和有这样的性质,
即如果l~r 数组 a[l]~a[r] 的sum和与a[l]^……^a[r]的异或值不同时,那么l与任意一个r以及r之后的i。
不会满足 l到i的sum和与异或和相同 。因为前一会有不满足的情况,后继的数组无论如何也不可能满足。
那么我们来看本题,
我们就可以通过上面的这个性质和利用双指针的常用套路方法:
对于每一个l,求出使之使其满足sum和与异或和相等的最右边的r。
那么以l为左端点的pair个数就是 r-l+1个
,然后我们把 l向右移动一位,同时删除 a[l]对sum和以及异或和的贡献。
然后对于新的l,我们只需要从上一个状态的r继续向后判定是否符合即可。(因为l~r满足的话,l+1~r一定也满足)
直至l大于n的时候跳出操作,输出答案即可。
细节见代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#define ALL(x) (x).begin(), (x).end()
#define rt return
#define dll(x) scanf("%I64d",&x)
#define xll(x) printf("%I64d\n",x)
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define gg(x) getInt(&x)
#define db(x) cout<<"== [ "<<x<<" ] =="<<endl;
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}
ll powmod(ll a, ll b, ll MOD) {ll ans = ; while (b) {if (b % )ans = ans * a % MOD; a = a * a % MOD; b /= ;} return ans;}
inline void getInt(int* p);
const int maxn = ;
const int inf = 0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
int a[maxn];
int n;
ll ans = 0ll; int main()
{
//freopen("D:\\common_text\\code_stream\\in.txt","r",stdin);
//freopen("D:\\common_text\\code_stream\\out.txt","w",stdout);
gbtb;
cin >> n;
repd(i, , n)
{
cin >> a[i];
}
// ans=n;
ll sum = 0ll;
ll xo = 0ll;
int l = ;
int r = ;
ll cnt = 0ll;
while ()
{
while (r < n && sum + a[r + ] == (xo ^ a[r + ]))
{
r++;
sum += a[r];
xo ^= a[r];
}
cnt = r - l + ;
// ans+=(cnt*(cnt+1))/2;
ans += cnt;
sum -= a[l];
xo ^= a[l++];
if (l == n + )
{
break;
}
} cout << ans << endl; return ;
} inline void getInt(int* p) {
char ch;
do {
ch = getchar();
} while (ch == ' ' || ch == '\n');
if (ch == '-') {
*p = -(getchar() - '');
while ((ch = getchar()) >= '' && ch <= '') {
*p = *p * - ch + '';
}
}
else {
*p = ch - '';
while ((ch = getchar()) >= '' && ch <= '') {
*p = *p * + ch - '';
}
}
}
												

最新文章

  1. 记忆用户设置-提升程序的体验VB/C#
  2. 开启curl扩展(转)
  3. C/C++操作MySQL数据库——增、删、改、查
  4. Unicode explorer
  5. JS面向对象概述
  6. Collection Operators
  7. 进程内核栈、用户栈及 Linux 进程栈和线程栈的区别
  8. hdu 1394 Minimum Inversion Number (裸树状数组 求逆序数 &amp;&amp; 归并排序求逆序数)
  9. Java设计模式11:常用设计模式之代理模式(结构型模式)
  10. java.util.concurrent.Exchanger应用范例与原理浅析--转载
  11. JavaMail API 1.4.7邮件发送
  12. MySQL ERROR 1045错误解决办法
  13. [转] Maven镜像配置
  14. JS 的Date对象
  15. 疯狂Android第一章:Android环境配置以及基本概念
  16. 阿里2015在线研发project师笔试题(部分)
  17. Android Glide 加载图片
  18. 从零开始学 Web 之 ES6(六)ES6基础语法四
  19. mysql 操作sql语句 目录
  20. 22.用demo通过点击切换图片路径

热门文章

  1. pkill精确匹配进程名称
  2. 企业链表C语言实现
  3. 使用tushare获取股票实时分笔数据延时有多大
  4. zabbix分布式监控环境搭建
  5. C# WPF 擦出效果,刮图效果
  6. 【python】将json串写入文件,并以json格式读取出来
  7. Spring bean的自动装配属性
  8. html script生成二维码
  9. vimiumC的下载、配置与节点个性化
  10. 各种CNN模型