A. Bits
 

Let's denote as  the number of bits set ('1' bits) in the binary representation of the non-negative integer x.

You are given multiple queries consisting of pairs of integers l and r. For each query, find the x, such that l ≤ x ≤ r, and is maximum possible. If there are multiple such numbers find the smallest of them.

Input

The first line contains integer n — the number of queries (1 ≤ n ≤ 10000).

Each of the following n lines contain two integers li, ri — the arguments for the corresponding query (0 ≤ li ≤ ri ≤ 1018).

Output

For each query print the answer in a separate line.

Sample test(s)
input
3
1 2
2 4
1 10
output
1
3
7
Note

The binary representations of numbers from 1 to 10 are listed below:

110 = 12

210 = 102

310 = 112

410 = 1002

510 = 1012

610 = 1102

710 = 1112

810 = 10002

910 = 10012

1010 = 10102

 题意:给n个询问,每次询问你l,r之间的数,在二进制下1位数最多的是哪个数

题解:我们从l向上构造,在0位补齐,贪心从小位到大位的补齐,一定是最优

///

#include<bits/stdc++.h>
using namespace std;
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std ;
typedef long long ll;
typedef unsigned long long ull;
#define mem(a) memset(a,0,sizeof(a))
#define pb push_back
inline ll read()
{
ll x=,f=;char ch=getchar();
while(ch<''||ch>''){
if(ch=='-')f=-;ch=getchar();
}
while(ch>=''&&ch<=''){
x=x*+ch-'';ch=getchar();
}return x*f;
}
//****************************************
const int N=+;
#define mod 10000007
#define inf 1000000001
#define maxn 10000 int d[];
ll test(ll x,ll y) {
for(int i=;i<;i++) {
if(!(x&(1ll<<i))&&x+(1ll<<i)<=y)
x+=(1ll<<i);
}
return x;
}
int main() {
int n=read();ll l,r;
for(int i=;i<=n;i++) {
cin>>l>>r;
cout<<test(l,r)<<endl;
}
return ;
}

代码

Codeforces Round #276 (Div. 1) A. Bits 贪心的更多相关文章

  1. Codeforces Round #276 (Div. 1) A. Bits 二进制 贪心

    A. Bits Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/484/problem/A Des ...

  2. CF&&CC百套计划4 Codeforces Round #276 (Div. 1) A. Bits

    http://codeforces.com/contest/484/problem/A 题意: 询问[a,b]中二进制位1最多且最小的数 贪心,假设开始每一位都是1 从高位i开始枚举, 如果当前数&g ...

  3. Codeforces Round #276 (Div. 2)C. Bits(构造法)

    这道题直接去构造答案即可. 对于l的二进制表示,从右到左一位一位的使其变为1,当不能再变了(再变l就大于r了)时,答案就是l. 这种方法既可以保证答案大于等于l且小于等于r,也可以保证二进制表示时的1 ...

  4. Codeforces Round #276 (Div. 1)D.Kindergarten DP贪心

    D. Kindergarten     In a kindergarten, the children are being divided into groups. The teacher put t ...

  5. C. Bits (Codeforces Round #276 (Div. 2) )

    题目大意:给你两个数l,r(l<r),求一个数是大于等于l且小于等于r的数中二进制数的1的个数最多,如果1的个数相同则取最小的那个(翻译渣,请见谅!) 思路:把左区间L化为二进制,再把左区间的二 ...

  6. codeforces 484a//Bits// Codeforces Round #276(Div. 1)

    题意:给出区间[ll,rr],求中间一个数二进制表示时一的个数最多. 写出ll和rr的二进制,设出现第一个不同的位置为pos(从高位到低位),找的数为x,那么为了使x在[ll,rr]内,前pos-1个 ...

  7. Codeforces Round #382 (Div. 2)B. Urbanization 贪心

    B. Urbanization 题目链接 http://codeforces.com/contest/735/problem/B 题面 Local authorities have heard a l ...

  8. Codeforces Round #180 (Div. 2) B. Sail 贪心

    B. Sail 题目连接: http://www.codeforces.com/contest/298/problem/B Description The polar bears are going ...

  9. Codeforces Round #374 (Div. 2) B. Passwords 贪心

    B. Passwords 题目连接: http://codeforces.com/contest/721/problem/B Description Vanya is managed to enter ...

随机推荐

  1. git的使用注意事项

    这里仅记录了下自己在初次使用git来管理项目的时候遇到的一些注意事项,记录下来备忘以下,以免下次又在这里花太多时间. 1. centos下面已经支持yum install git.来安装. 2. 安装 ...

  2. inner join / left join / right join

    left join(左联接) 返回包括左表中的所有记录和右表中联结字段相等的记录 right join(右联接) 返回包括右表中的所有记录和左表中联结字段相等的记录inner join(等值连接) 只 ...

  3. Android RecyclerView遇到notifyDataSetChanged无效时的解决方案

    一.简述 不管AbsListView(ListView.GridView)或是新出的RecyclerView,在使用notifyDataSetChanged方法更新列表数据时,一定要保证数据为同个对象 ...

  4. js 翻页

    翻页功能是js很基础的一个算法,且用得很多,所以必须掌握此项技能. 我们要想清楚在实现翻页的过程中需要哪几个步骤: 1.我们首先需要的变量有哪些,必须的有一个存放当前页码的变量nowPage.一个存放 ...

  5. 常用MySQL语句整合

    常用MySQL语句整合 1. MySQL服务的配置和使用 修改MySQL管理员的口令:mysqladmin –u root password 密码字符串 如:mysqldmin –u root pas ...

  6. 【技术累积】【线】【java】【2】AOP

    思维导图 基础概念 翻译:面向切面编程,或面向方面编程: 是OOP的重要补充: 切面:传统的OOP构建的是对象之间的关系,是一种垂直的关系:假设,OOP程序是一个圆筒,那么与业务或逻辑无关的东西,比如 ...

  7. B+树知识点

    B+树介绍   目录 B+树 B+树的插入操作 B+树的删除操作 回到顶部 B+树 B+树和二叉树.平衡二叉树一样,都是经典的数据结构.B+树由B树和索引顺序访问方法(ISAM,是不是很熟悉?对,这也 ...

  8. 第十二节:pandas缺失数据处理

    1.isnull():检查是否含有确实数据 2.fillna():填充缺失数据 3.dropna() :删除缺失值 4.replace():替换值

  9. c#获取文字全拼音

    class Program { /// <summary> /// 获得拼音 /// </summary> /// <param name="str_Spell ...

  10. 【codeforces 761E】Dasha and Puzzle

    [题目链接]:http://codeforces.com/contest/761/problem/E [题意] 给你一棵树,让你在平面上选定n个坐标; 使得这棵树的连接关系以二维坐标的形式展现出来; ...