Codeforce 1054 D. Changing Array —— 區間異或和
At a break Vanya came to the class and saw an array of n k-bit integers a1,a2,…,an on the board. An integer x is called a k-bit integer if 0≤x≤2k−1.
Of course, Vanya was not able to resist and started changing the numbers written on the board. To ensure that no one will note anything, Vanya allowed himself to make only one type of changes: choose an index of the array i (1≤i≤n) and replace the number ai with the number ai¯¯¯¯. We define x¯¯¯ for a k-bit integer x as the k-bit integer such that all its k bits differ from the corresponding bits of x.
Vanya does not like the number 0. Therefore, he likes such segments [l,r] (1≤l≤r≤n) such that al⊕al+1⊕…⊕ar≠0, where ⊕ denotes the bitwise XOR operation. Determine the maximum number of segments he likes Vanya can get applying zero or more operations described above.
Input The first line of the input contains two integers n and k (1≤n≤200000, 1≤k≤30).
The next line contains n integers a1,a2,…,an (0≤ai≤2k−1), separated by spaces — the array of k-bit integers.
Output Print one integer — the maximum possible number of segments with XOR not equal to 0 that can be obtained by making several (possibly 0) operations described in the statement.
Examples inputCopy 3 2 1 3 0 outputCopy 5 inputCopy 6 3 1 4 4 7 3 4 outputCopy 19 Note In the first example if Vasya does not perform any operations, he gets an array that has 5 segments that Vanya likes. If he performs the operation with i=2, he gets an array [1,0,0], because 3¯¯¯=0 when k=2. This array has 3 segments that Vanya likes. Also, to get an array with 5 segments that Vanya likes, he can perform two operations with i=3 and with i=2. He then gets an array [1,0,3]. It can be proven that he can’t obtain 6 or more segments that he likes.
In the second example, to get 19 segments that Vanya likes, he can perform 4 operations with i=3, i=4, i=5, i=6 and get an array [1,4,3,0,4,3].
題意:
給你一個長度為n的串,你可以將任意多個位置的值變成它對的異或,問最多能有多少個區間,這個區間中的值異或和不為0;
題解:
一遍做過去,每次我們只需要讓字首異或和中的0最少就好了比如說1到x區間的異或和為a,那麼如果1到y這個區間的異或和為a的話,y到x區間的異或和就為0,那麼我們只需要記錄一下有多少字首異或和為a,用一個map記錄
#include<bits/stdc++.h>
using namespace std;
#define ll long long
map<ll,ll>map1;
int main()
{
int x,n,k,num=0;
scanf("%d%d",&n,&k);
int ret=(1<<k)-1;
ll sum=0;
for(int i=1;i<=n;i++)
{
scanf("%d",&x);
int ne=num^x;
int z1=0,z2=0;
if(!x)
z1++;
if(!ne)
z1++;
z1+=map1[ne];
x=ret-x;
int ne2=num^x;
if(!x)
z2++;
if(!ne2)
z2++;
z2+=map1[ne2];
if(z1<=z2)
sum+=i-z1,map1[ne]++,num=ne;
else
sum+=i-z2,map1[ne2]++,num=ne2;
}
printf("%lld\n",sum);
}