牛客练习赛45 D Data Structure

在这里插入图片描述
在这里插入图片描述

题解

所有数或上数 x x x,相当于让 x x x中为1的位,固定为1,其余位需要再考虑最大化
所有数与上数 x x x,相当于让 x x x中为0的位,固定为0,其余位需要再考虑最大化
随着操作1和2的增多,被固定的位越来越多,我们需要考虑的位越来越少
如何最大化?从高位开始贪心,如果出现了不少于K个1,那么这一位就为1,否则为0
具体方法比较巧妙,见代码

代码
#include<bits/stdc++.h>
#define N 200010
#define sc(x) scanf("%d",&x)
#define scc(x,y) scanf("%d%d",&x,&y)
using namespace std;

int a[N],v[32],n,m,q,fg,ans;

void spy(){
	ans=0;
	for(int i=30;i>=0;i--) if (!(fg>>i&1)){
		int tm=ans|(1<<i),cnt=0,x=0;
		for (int j=1;j<=n;j++){
			x|=a[j];
			if ((x&tm)==tm) cnt++,x=0;
		}
		if (cnt>=m) ans|=(1<<i);
	}
}

int main(){
	scc(n,m);
	for (int i=1;i<=n;i++) sc(a[i]);
	sc(q); 
	spy();
	while(q--){
		int op,x;sc(op);
		if (op<3){
			op=2-op;
			sc(x); int tm=fg;
			for (int i=0;i<=30;i++) 
				if ((x>>i&1)==op) tm|=(1<<i),v[i]=op;
			if (tm!=fg) fg=tm,spy();
		}else{
			int tm=ans;
			for (int i=0;i<=30;i++)
				if (fg>>i&1) tm|=v[i]<<i;
			printf("%d\n",tm);
		}
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值