省选专练之CF1054D. Changing Array

outputstandard output
At a break Vanya came to the class and saw an array of
n k n k nk-bit integers a 1 , a 2 , … , a n a 1 , a 2 , … , a n a1,a2,,anon the board. An integer x x xis called a k k k
-bit integer if
0 ≤ x ≤ 2 k − 1. 0 ≤ x ≤ 2 k − 1 . 0x2k1.

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 ) i(1in)and replace the number a i a i ai
with the number
¯ ¯ ¯ ¯ ¯¯¯¯ ¯¯¯¯
a i a i ai. We define
¯ ¯ ¯ ¯¯¯ ¯¯¯ x x x for a k k k-bit integer x x xas the k k k-bit integer such that all its k k kbits differ from the corresponding bits of x x x
Vanya does not like the number
0 0 0 Therefore, he likes such segments
[ l , r ] ( 1 ≤ l ≤ r ≤ n ) [ l , r ] ( 1 ≤ l ≤ r ≤ n ) [l,r](1lrn)
such that
a l ⊕ a l + 1 ⊕ … ⊕ a r ≠ 0 a l ⊕ a l + 1 ⊕ … ⊕ a r ≠ 0 alal+1ar̸=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 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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值