BZOJ 2728: [HNOI2012]与非

OrzpopoQQQ大爷

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
typedef long long ll;
ll base[70],all,tot;
ll query(ll x){
	if(x<0)return -1;
	ll ans=0,now=0;
	for(int i=1;i<=tot;i++)
	if((now|base[i])<=x)now|=base[i],ans|=1ll<<tot-i;
	return ans;
}
bool vis[70];
ll a[1005];
int main(){
	//freopen("a.in","r",stdin);
	int n,k;ll l,r;
	scanf("%d%d%lld%lld",&n,&k,&l,&r);
	all=(1ll<<k)-1;
	for(int i=1;i<=n;i++)scanf("%lld",&a[i]);
	for(int i=k-1;i>=0;i--)
	if(!vis[i]){
		ll now=all;
		for(int j=1;j<=n;j++)
		if(a[j]>>i&1)now&=a[j];
		else now&=~a[j]&all;
		base[++tot]=now;
		for(int j=0;j<=i;j++)
		if(now>>j&1)vis[j]=1;
	}
	printf("%lld\n",query(r)-query(l-1));
	return 0;
}


你可能感兴趣的:(BZOJ 2728: [HNOI2012]与非)