1. 程式人生 > >省選專練之CF1054D. Changing Array

省選專練之CF1054D. Changing Array

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