码迷,mamicode.com
首页 > 其他好文 > 详细

CF1109A Sasha and a Bit of Relax

时间:2019-02-17 10:40:45      阅读:196      评论:0      收藏:0      [点我收藏+]

标签:make   main   sha   表示   lin   force   end   its   .com   

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

CF1109A Sasha and a Bit of Relax

标签:make   main   sha   表示   lin   force   end   its   .com   

原文地址:https://www.cnblogs.com/jklover/p/10390272.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!