Cattle off winter field 4 d XOR

Here Insert Picture Description
This question is the use of prefixes and to solve, sum scratch constantly ^ represents the current state, if the principle sum [l-1] = sum [r] will be described l and r of the sub-segment and the exclusive OR is 0, then we use one of the outputs of the emergence of a number of memory map, because this number will appear every time a new subset of the number of occurrences (inclusive equal only to explain this possible ·) as for why we must map is possible because the array range appears too large, the direct use of explosive stack memory array will be 'code is very short

# include <bits/stdc++.h>
using namespace std;
map <long long int,int> m;
int main()
{
    long long n,sum=0,ans=0;
    cin>>n;
    m[0]=1;
    for(int i=1;i<=n;i++)
    {
        int x;
        cin>>x;
        sum^=x;
        ans+=m[sum]++;
    }
    cout<<ans;
    return 0;
}
Published 48 original articles · won praise 17 · views 4468

Guess you like

Origin blog.csdn.net/weixin_45757507/article/details/104272490