And It‘s Non-Zero 题解

Problem - B - Codeforces

题意:对于 [ l , r ] [l,r] [l,r]之间的整数,最少删除几个数字能使剩下的数字进行AND运算得到的结果非0?

题解:使运算结果非0,相当于使运算结果的二进制形式下至少有一位是1。要想使第 i i i位是1,需要使剩下的数字的第 i i i位都是1。对于第 i i i位,统计 [ l , r ] [l,r] [l,r]中有多少个数字的第 i i i位为0,表示我们至少要删多少个数字才能使第 i i i位为1。答案是 m i n ( s [ r ] [ i ] − s [ l − 1 ] [ i ] ) min(s[r][i] - s[l-1][i]) min(s[r][i]s[l1][i])

#ifndef DEBUG
#pragma GCC optimize("O2")
#endif

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<string>
#include<iostream>
#include<sstream>
#include<vector>
#include<list>
#include<stack>
#include<queue>
#include<set>
#include<map>
#include<bitset>
#include<limits.h>
#include<assert.h>
using namespace std;
#define all(X) (X).begin(), (X).end()
#define rep(I,A,B) for(int I=(A);I<(B);I++)
#define repd(I,A,B) for(int I=(A);I>(B);I--)
#define sort_unique(c) (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
typedef long long LL;
typedef pair<int,int> PII;
typedef pair<LL,LL> PLL;
typedef vector<int> VI;
typedef vector<PII> VPII;
typedef vector<LL> VLL;
typedef vector<PLL> VPLL;
template<class T> void _R(T &x) { cin >> x; }
void _R(int &x) { scanf("%d", &x); }
void _R(int64_t &x) { scanf("%lld", &x); }
void _R(double &x) { scanf("%lf", &x); }
void _R(char &x) { scanf(" %c", &x); }
void _R(char *x) { scanf("%s", x); }
void R() {}
template<class T, class... U> void R(T &&head, U &&... tail) { _R(head); R(tail...); }
template<class T> void _W(const T &x) { cout << x; }
void _W(const int &x) { printf("%d", x); }
void _W(const int64_t &x) { printf("%lld", x); }
void _W(const double &x) { printf("%.16f", x); }
void _W(const char &x) { putchar(x); }
void _W(const char *x) { printf("%s", x); }
template<class T,class U> void _W(const pair<T,U> &x) {_W("{"); _W(x.first); putchar(','); _W(x.second); _W("}");}
template<class T> void _W(const vector<T> &x) { for (auto i = x.begin(); i != x.end(); _W(*i++)) if (i != x.cbegin()) putchar(' '); }
void W() { }
template<class T, class... U> void W(const T &head, const U &... tail) { _W(head); putchar(sizeof...(tail) ? ' ' : '\n'); W(tail...); }

int main()
{
    #ifdef DEBUG
    freopen("1.in","r",stdin);
    //freopen("tmp.out","w",stdout);
    #endif

	int t;R(t);
	
	vector<VI> s(200001,VI(32));
	rep(i,1,200001) {
		rep(j,0,32) {
			if(!(i & (1<<j))) s[i][j]=1;
			s[i][j] += s[i-1][j];
		}
		//W(i,':',s[i]);
	}
	
	rep(_,0,t) {
		int l,r,ans=200001;
		R(l,r);
		rep(i,0,32)
			ans=min(ans,s[r][i] - s[l-1][i]);
		W(ans);
	}
	
    return 0;
}
  • 17
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值