2020 cattle off winter training camp algorithm basis 4D. Subsegments XOR (bit operation)

Links: https://ac.nowcoder.com/acm/contest/3005/D

 

Meaning of the questions:

To an array, asked how many sub-sequences and XOR 0

Ideas:

First we need to define a prefix and an array SUM [i] , i from 1 to representatives of this exclusive OR and

So amazing things come, sum [lr] = sum [l-1] ^ sum [r], because they are part of the same or different up to 0, which is the common prefixes and different parts

If this is the exclusive OR and 0, then the sum [l-1] ^ sum [r] = 0, i.e. sum [l-1] = sum [r], so we only need to count each prefix value and to see how much the same prefix

Finally, the answer is Σcnt i * (cnt i -1)

#include<iostream>
#include<algorithm>
#include<map>
 using namespace std;
 const int maxn=2e5+10;
 typedef long long ll;
 ll a[maxn];
 map<ll,ll> s;
 int main()
 {
     ll ans=0,n,sum=0;
     cin>>n;
     s[0]=1;
     for(int i=1;i<=n;i++){
         scanf("%lld",&a[i]);
         sum^=a[i];
         ans+=s[sum];
         s[sum]+=1;
     }
    cout<<ans<<endl;
    return 0;
 }

 

Guess you like

Origin www.cnblogs.com/overrate-wsj/p/12299902.html