【树状数组 前缀和】Codeforces Global Round 18 B. And It‘s Non-Zero

44 篇文章 4 订阅

原题地址:http://codeforces.com/contest/1615/problem/B

B. And It’s Non-Zero

time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output

You are given an array consisting of all integers from [l,r] inclusive. For example, if l=2 and r=5, the array would be [2,3,4,5]. What’s the minimum number of elements you can delete to make the bitwise AND of the array non-zero?

A bitwise AND is a binary operation that takes two equal-length binary representations and performs the AND operation on each pair of the corresponding bits.

Input
The first line contains one integer t (1≤t≤1e4) — the number of test cases. Then t cases follow.

The first line of each test case contains two integers l and r (1≤l≤r≤2e5) — the description of the array.

Output
For each test case, output a single integer — the answer to the problem.

Example
input

5
1 2
2 8
4 5
1 5
100000 200000

output

1
3
0
2
31072

Note
In the first test case, the array is [1,2]. Currently, the bitwise AND is 0, as 1 & 2=0. However, after deleting 1 (or 2), the array becomes [2] (or [1]), and the bitwise AND becomes 2 (or 1). This can be proven to be the optimal, so the answer is 1.

In the second test case, the array is [2,3,4,5,6,7,8]. Currently, the bitwise AND is 0. However, after deleting 4, 5, and 8, the array becomes [2,3,6,7], and the bitwise AND becomes 2. This can be proven to be the optimal, so the answer is 3. Note that there may be other ways to delete 3 elements.

题意:
给定一个区间[l,r],以[l,r]中的所有数构成一个序列。现在要使这个序列的按位与不为零(l&(l+1)&(l+2)&······&(r-1)&(r) != 0),求最少需要删去多少个数。

思路:
若要使按位与的和不等于0,需要始终有一个二进制位为1。
那么,要找到一个二进制位为1最多的位。其他该位为0的数的个数即为本题答案。
在这里插入图片描述

把[l,r]的所有数化为二进制,找出其二进制的哪一位最多。
由于题目数据规模较大,每一次都暴力求和显然不合适。
所以可以先对[1,200000]进行预处理,转化为二进制并存起来。
然后利用树状数组或线段树求每一个二进制位的区间和(区间查询)。
这样,对于每一组[l,r],只要查询在该区间范围内哪一个二进制位的和最大。

AC 代码:

#pragma GCC optimize(2)
#include<bits/stdc++.h>
using namespace std;
#define maxn 200000
int tree[200007][20];
int ansbit[20];

int lowbit(int k){
    return k & -k;
}

void add(int x,int k,int bit){
    while(x<=maxn){
    	tree[x][bit]+=k;
        x+=lowbit(x);
	}
}

int sum(int x,int bit){
	int ans=0;
	while(x!=0){
		ans+=tree[x][bit];
		x-=lowbit(x);
	}
	return ans;
}

void init(){//预处理,转化为2进制,并加入到树状数组
	int inbit;
	for(int i=1;i<=200000;++i){
		for(int j=17;j>=0;--j){
			if(i&(1<<j)) inbit=1;
			else inbit=0;
			add(i,inbit,j);
		}
	}
}
	
int main(){
   	int T,l,r;
   	init();
	cin>>T;
	while(T--){
		cin>>l>>r;
		memset(ansbit,0,sizeof(ansbit));
		for(int j=0;j<=17;j++){//每一个二进制位的和
			ansbit[j]=sum(r,j)-sum(l-1,j);//区间查询
		}
		int max0=0;
		for(int i=0;i<=17;i++){//求二进制位和的最大值
			max0=max(ansbit[i],max0);
		}
		cout<<r-l+1-max0<<endl;//区间长度-最大值
	}
	return 0;
}
  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
根据提供的引用内容,Codeforces Round 511 (Div. 1)是一个比赛的名称。然而,引用内容中没有提供与这个比赛相关的具体信息或问题。因此,我无法回答关于Codeforces Round 511 (Div. 1)的问题。如果您有关于这个比赛的具体问题,请提供更多的信息,我将尽力回答。 #### 引用[.reference_title] - *1* [Codeforces Round 860 (Div. 2)题解](https://blog.csdn.net/qq_60653991/article/details/129802687)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [Codeforces Round 867 (Div. 3)(A题到E题)](https://blog.csdn.net/wdgkd/article/details/130370975)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [Codeforces Round 872 (Div. 2)(前三道](https://blog.csdn.net/qq_68286180/article/details/130570952)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值