C. Sasha and a Bit of Relax
time limit per test 1 second memory limit per test 256 megabytes
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)(l≤r). To check if a pair (l,r) is a funny pair, take mid=(l+r−1)/2, then if r−l+1 is an even number and al⊕al+1⊕…⊕amid=amid+1⊕amid+2⊕…⊕ar, 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 (2≤n≤3⋅10^5) — the size of the array.
The second line contains n integers a1,a2,…,an (0≤ai<2^20) — array itself.
Output
Print one integer — the number of funny pairs. You should consider only pairs where r−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) as 2⊕3=4⊕5=1
In the second example, funny pairs are (2,3), (1,4)and (3,6)
In the third example, there are no funny pairs.
给你n个数,问你偶数个连续的数,前一半的异或值等于后一半的异或值有多少个
首先因为数最大是2^20,所以直接开数组保存,前i个异或值是x的有多少个
因为a[l]---a[r]的异或值应该为0,而且是a[l]-a[r]中取任意两段(要求两段不重复且加起来是整个长度)异或值是相等的
又因为a[l]-a[mid]==a[mid]--a[r]————a[l-1]==a[r]
所以我只要枚举r,然后把新的异或值保存,这样就保证一定是加上的答案是前面的,不是后面出现的
然后因为长度一定要是偶数个,所以l-1和r要么都是偶数,要么都是奇数
那么异或和数组也得开两个,一个是到第偶数个,一个是到第奇数个
//b[l-1]=b[r]&&(r-l+1)%2==0
//l-1为奇数,r为奇数;l-1为偶数,r为偶数
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=3e5+10;
int a[maxn],b[maxn];
int odd[(1<<20)+10],even[(1<<20)+10];
int main()
{
int n;
cin>>n;
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
b[i]=b[i-1]^a[i];
}
ll ans=0;
even[0]++;
for(int i=1;i<=n;i++)
{
if(i%2) //r为奇数
{
ans+=odd[b[i]];
odd[b[i]]++;
}
else
{
ans+=even[b[i]];
even[b[i]]++;
}
}
cout<<ans<<endl;
return 0;
}