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

Xor Sum 2 AtCoder - 4142 (异或前缀和性质+ 双指针)的更多相关文章

  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. 2014百度之星第三题Xor Sum(字典树+异或运算)

    Xor Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 132768/132768 K (Java/Others) Total ...

  3. HDU 4825 Xor Sum (trie树处理异或)

    Xor Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 132768/132768 K (Java/Others)Total S ...

  4. AtCoder Beginner Contest 098 D - Xor Sum 2

    D - Xor Sum 2 Time limit : 2sec / Memory limit : 1024MB Score : 500 points Problem Statement There i ...

  5. Xor Sum(讲解异或)【字典树】

    Xor Sum 题目链接(点击) Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 132768/132768 K (Java/Other ...

  6. 字典树-百度之星-Xor Sum

    Xor Sum Problem Description Zeus 和 Prometheus 做了一个游戏,Prometheus 给 Zeus 一个集合,集合中包括了N个正整数,随后 Prometheu ...

  7. UVALive4682 XOR Sum

    UVALive4682 XOR Sum 题意 给定一个数组, 求连续子序列中异或值最大的值. 题解 假设答案区间为 [L, R], 则答案为 XOR[L, R], 可以将区间分解为 XOR[L,R] ...

  8. [BZOJ3261] 最大异或和 (异或前缀和,可持久化Trie)

    Description 给定一个非负整数序列{a},初始长度为N. 有M个操作,有以下两种操作类型: 1.Ax:添加操作,表示在序列末尾添加一个数x,序列的长度N+1. 2.Q l r x:询问操作, ...

  9. P2420 让我们异或吧 (树链剖分,异或前缀和)

    题目描述 异或是一种神奇的运算,大部分人把它总结成不进位加法. 在生活中-xor运算也很常见.比如,对于一个问题的回答,是为1,否为0.那么: (A是否是男生 )xor( B是否是男生)=A和B是否能 ...

随机推荐

  1. python基础之 数据格式化

    %还是format 皇城PK Python中格式化字符串目前有两种阵营:%和format,我们应该选择哪种呢? 自从Python2.6引入了format这个格式化字符串的方法之后,我认为%还是form ...

  2. 前台ajax传数组,后台java接收

    后端 //添加 @RequestMapping(value = "checkChoise") @ResponseBody ResultJson checkChoise(@Reque ...

  3. uncaught syntaxerror unexpected token U JSON

    uncaught syntaxerror unexpected token U JSON The parameter for the JSON.parse may be returning nothi ...

  4. C/C++中的 void 和 void*

    转自:https://blog.csdn.net/tonglin12138/article/details/86516789 写在前面:开门见山,大家看下面这个例子: unsigned char er ...

  5. oracle中awr性能报告阅读笔记

    1.对于数据库运行的业务不太了解,那么awr性能报告中最需要关注的是Top 5 Timed Foreground Events和Time Model Statistics两个部分

  6. python-接口开发flask模块(一)工具类准备

    我们常常听说测试http接口.测试java接口,测试socket接口等等:那么python这么强大的语言当然也可以用来开发接口了. flask模块介绍: python中用来开发接口的模块:flask, ...

  7. 那些年我们经历的BT面试题

    初入职场面试的我到处碰壁,以下是我个人对几道面试题的小总结: 1.一列数字的规则如下:1,1,2,3,5,8,13,21, 34........ 求第30位数字是多少,用递规和非递归两种方法算法实现. ...

  8. fiddler之数据分析和查看(inspectors)-抓包

    在instpectors中主要是对请求和响应进行查看和分享,监听请求的响应内容.他有多个分页标签.界面分上下两部分,上面部分显示请求的相关信息:下面部分显示响应相关信息.简单说明常用的几个分页标签 一 ...

  9. TCP中SYN洪水攻击

    在查看TCP标识位SYN时,顺便关注了一下SYN Flood,从网上查阅一些资料加以整理,SYN洪水攻击利用TCP三次握手. 1.SYN洪水介绍 当一个系统(客户端C)尝试和一个提供了服务的系统(服务 ...

  10. 【linux开发】Linux下配置java环境 安装eclipse

    配置JDK环境 本文转自:http://www.cnblogs.com/fnng/archive/2013/01/30/2883815.html,有修改 下载 登录oracle的网站去下载JDK1.8 ...