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

转载于:https://www.cnblogs.com/jklover/p/10390272.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值