Codeforces 1113C: Sasha and a Bit of Relax(位运算|异或)

time limit per test: 1 second
memory limit per test: 256 megabytes
input: standard input
output: standard output

Sasha likes programming. Once, during a very long contest, Sasha decided that he was a bit tired and needed to relax. So he did. But since Sasha isn’t an ordinary guy, he prefers to relax unusually. During leisure time Sasha likes to upsolve unsolved problems because upsolving is very useful.

Therefore, Sasha decided to upsolve the following problem:

You have an array aa with nn integers. You need to count the number of funny pairs (l,r) (lr)(l,r)\ (l≤r). To check if a pair (l,r)(l,r) is a funny pair, take mid=l+r12mid=\frac{l+r-1}{2}, then if rl+1r−l+1 is an even number and alal+1amid=amid+1amid+2ara_{l} \oplus a_{l+1} \oplus \ldots \oplus a_{m i d}=a_{m i d+1} \oplus a_{m i d+2} \oplus \ldots \oplus a_{r}, then the pair is funny. In other words, of elements of the left half of the subarray from ll to rr should be equal to of elements of the right half. Note that denotes the bitwise XOR operation.

It is time to continue solving the contest, so Sasha asked you to solve this task.

Input

The first line contains one integer n (2n3105)n\ (2≤n≤3⋅10^5) — the size of the array.

The second line contains nn integers a1,a2,,an(0ai<220)a_{1}, a_{2}, \dots, a_{n}\left(0 \leq a_{i}<2^{20}\right) — array itself.

Output

Print one integer — the number of funny pairs. You should consider only pairs where rl+1r−l+1 is even number.

Examples

input

5
1 2 3 4 5

output

1

input

6
3 2 2 3 7 6

output

3

input

3
42 4 2

output

0

Note

Be as cool as Sasha, upsolve problems!

In the first example, the only funny pair is (2,5)(2,5), as 23=45=12 \oplus 3=4 \oplus 5=1.

In the second example, funny pairs are (2,3)(2,3), (1,4)(1,4), and (3,6)(3,6).

In the third example, there are no funny pairs.

题意

nn个数,对于偶数长度的区间[l,r][l,r]mid=l+r12mid=\frac{l+r-1}{2},要求[l,mid],[mid+1,r][l,mid],[mid+1,r]两个区间内的数的异或值相等,问有多少个这样的区间

Solve

利用异或的性质:出现偶数次的数异或值为00

如果[l,mid],[mid+1,r][l,mid],[mid+1,r]区间数的异或值相等,则[l,r][l,r]区间的数的异或值为00

可以推出:如果当前位置的异或值出现过,并且之前出现的位置与当前出现位置下标的奇偶性相同,那么这两个位置之间的区域就是题目中要求的funny pairs

Code

/*************************************************************************
    > File Name: C.cpp
    > Author: WZY
    > Created Time: 2019年02月17日 19:59:36
 ************************************************************************/

#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define ms(a,b) memset(a,b,sizeof(a))
#define pi acos(-1.0)
#define INF 0x7f7f7f7f
const double E=exp(1);
const int maxn=1e6+10;
const int mod=1e9+7;
using namespace std;
ll sum[1<<20][2];
int main(int argc, char const *argv[])
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	int n;
	cin>>n;
	ll ans=0;
	ll x;
	ll res=0;
	sum[0][0]=1;
	for(int i=1;i<=n;i++)
	{
		cin>>x;
		res^=x;
		ans+=sum[res][i&1];
		sum[res][i&1]++;
	}
	cout<<ans<<endl;
	return 0;
}

posted @ 2019-02-17 22:13  友人-A  阅读(304)  评论(0编辑  收藏  举报