Codeforces 1113C: Sasha and a Bit of Relax(位运算|异或)
阅读原文时间:2023年07月10日阅读:2

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 aaa with nnn integers. You need to count the number of funny pairs (l,r) (l≤r)(l,r)\ (l≤r)(l,r) (l≤r). To check if a pair (l,r)(l,r)(l,r) is a funny pair, take mid=l+r−12mid=\frac{l+r-1}{2}mid=2l+r−1​, then if r−l+1r−l+1r−l+1 is an even number and al⊕al+1⊕…⊕amid=amid+1⊕amid+2⊕…⊕ara_{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}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 lll to rrr 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.

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

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

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

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

Be as cool as Sasha, upsolve problems!

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

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

In the third example, there are no funny pairs.

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

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

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

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

/*************************************************************************
    > 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;
}

手机扫一扫

移动阅读更方便

阿里云服务器
腾讯云服务器
七牛云服务器

你可能感兴趣的文章