省選專練之CF1054D. Changing Array
outputstandard output
At a break Vanya came to the class and saw an array of
-bit integers
on the board. An integer
is called a
-bit integer if
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
and replace the number
with the number
. We define
for a
-bit integer
as the
-bit integer such that all its
bits differ from the corresponding bits of
Vanya does not like the number
Therefore, he likes such segments
such that
, where
denotes the bitwise XOR operation. Determine the maximum number of segments he likes Vanya can get applying zero or more operations described above.
有趣的貪心。
我們發現一段序列的異或字首和實際只有兩種。
因為你選擇異或的值是永遠一樣的。
維護字首值。
注意
的情況。
#include<bits/stdc++.h>
using namespace std;
#define int long long
map<int,int>mmp;
int pre=0;
int n,k,Mx;
int ans=0;
signed main(){
cin>>n>>k;
Mx=(1<<k)-1;
ans=n*(int)(n+1)/2;
mmp[0]=1;
for(int i=1;i<=n;++i){
int x;
cin>>x;
int A=pre^x;
int B=A^Mx;
if(!mmp.count(A))mmp[A]=0;
if(!mmp.count(B))mmp[B]=0;
if(mmp[A]<=mmp[B]){
ans-=mmp[A];
pre=A;
mmp[A]++;
}
else{
ans-=mmp[B];
pre=B;
mmp[B]++;
}
}
cout<<ans;
}