省选专练之CF1054D. Changing Array

版权声明:LeoJAM Presents https://blog.csdn.net/fcb_x/article/details/83189075

outputstandard output
At a break Vanya came to the class and saw an array of
n k n k -bit integers a 1 , a 2 , , a n a 1 , a 2 , … , a n on the board. An integer x x is called a k k
-bit integer if
0 x 2 k 1. 0 ≤ x ≤ 2 k − 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 ) i ( 1 ≤ i ≤ n ) and replace the number a i a i
with the number
¯ ¯ ¯ ¯ ¯¯¯¯
a i a i . We define
¯ ¯ ¯ ¯¯¯ x x for a k k -bit integer x x as the k k -bit integer such that all its k k bits differ from the corresponding bits of x x
Vanya does not like the number
0 0 Therefore, he likes such segments
[ l , r ] ( 1 l r n ) [ l , r ] ( 1 ≤ l ≤ r ≤ n )
such that
a l a l + 1 a r 0 a l ⊕ a l + 1 ⊕ … ⊕ a r ≠ 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.

有趣的贪心。
我们发现一段序列的异或前缀和实际只有两种。
因为你选择异或的值是永远一样的。
维护前缀值。
注意 0 0 的情况。

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

猜你喜欢

转载自blog.csdn.net/fcb_x/article/details/83189075