CF1109A Sasha and a Bit of Relax

CF1109A Sasha and a Bit of Relax

  • \(xorsum[l,r]\) 表示 \(a[l] \oplus a[l+1] \oplus a[l+2]... a[r-1] \oplus a[r]\).
  • 则数对 \((l,r)\) 满足 \(xorsum[l,mid]=xorsum[mid+1,r]\and 2|(r-l).\)

\[ xorsum[l,mid]=xorsum[mid+1,r]\\ \Leftrightarrow xorsum[l,r]=0\\ \Leftrightarrow xorsum[1,l-1]=xorsum[1,r]. \]

  • 于是只需开两个桶,分别记录奇数偶数位置上的 \(xor\) 前缀和出现次数.
  • 注意开始时 \(0\) 也在偶数位上出现了,需加入桶中.
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define mp make_pair
#define pii pair<int,int>
inline int read()
{
    int x=0;
    bool pos=1;
    char ch=getchar();
    for(;!isdigit(ch);ch=getchar())
        if(ch=='-')
            pos=0;
    for(;isdigit(ch);ch=getchar())
        x=x*10+ch-'0';
    return pos?x:-x;
}
int odd[(1<<20)+10],even[(1<<20)+10];
int main()
{
    int xorsum=0;
    int n=read();
    ll ans=0;
    ++even[0];
    for(int i=1;i<=n;++i)
        {
            int x=read();
            xorsum^=x;
            if(i&1)
                {
                    ans+=odd[xorsum];
                    ++odd[xorsum];
                }
            else
                {
                    ans+=even[xorsum];
                    ++even[xorsum];
                }
        }
    cout<<ans<<endl;
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/jklover/p/10390272.html
今日推荐