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. iterator删除元素

    总结 在需要的删除等操作时,不能使用简单的foreach,因为其底层依然用的是Iterator,但是调用的是集合中的remove方法. 使用迭代器对象调用其中的remove方法,以保证线程同步.

  2. legend3---2、网站的代码里面的/也是代表根目录

    legend3---2.网站的代码里面的/也是代表根目录 一.总结 一句话总结: 不过这个根目录在public下面 1.mysql删除数据库命令? drop database <数据库名> ...

  3. jQuery file upload callback options

    autoUpload By default, files added to the widget are uploaded as soon as the user clicks on the star ...

  4. leetcode-mid-backtracking-17. Letter Combinations of a Phone Number

    mycode  68.26% class Solution(object): def letterCombinations(self, digits): """ :typ ...

  5. 【C++进阶】getline

    在<istream>中的getline函数有两种重载形式: istream& getline (char* s, streamsize n );istream& getli ...

  6. RotateZoom.cpp 20180622

    20180622代码加入随意变换图像大小 批处理框架先不看:-B src3.bmp 10 1 30 2  0.1 3 tar.bmp src2.bmp 37.5 2.1 tar // RotateZo ...

  7. rename()函数(包含更改索引列列名的方法)

    1 rename()可以更换列名和行名,必须写上columns或index,否则无效 import pandas as pd df = pd.DataFrame({'a':[1,2], 'b':[3, ...

  8. 【工具安装】VMware 安装教程

    介绍:介绍一下 VMware 的安装. 0x01. 下载软件 打开官网 VMware Workstation Pro 点击立即下载即可.  也可以直接使用迅雷,添加下载任务,比浏览器下载速度快些,提 ...

  9. vue--路由嵌套

    路由嵌套的SPA实现的步骤: A(/a)组件需要嵌套B组件(/b)和C组件(/c) ①准备嵌套其它组价的父组件 指定一个容器在A组件指定一个容器<router-view></rout ...

  10. win10安装anaconda及tensorflow1.9版本

    前言 因为之前的anaconda的conda命令不能用,又找不到原因,所以就决定重装anaconda,然后再装个tensorflow环境.. 正文 可以去官网下载,也可以去清华的开源软件镜像站下载ht ...