C. Bits

time limit per test1 second

memory limit per test256 megabytes

inputstandard input

outputstandard output

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.

Examples

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


题意:求l和r之间二进制1的个数最多的数字x


首先证明x一定在l的基础上

然后从低位贪心把0变成1

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <queue>
using namespace std;
typedef long long ll;
inline ll read(){
char c=getchar();ll x=,f=;
while(c<''||c>''){if(c=='-')f=-;c=getchar();}
while(c>=''&&c<=''){x=x*+c-'';c=getchar();}
return x*f;
}
ll T,l,r;
int main(int argc, const char * argv[]) {
T=read();
while(T--){
l=read();r=read();
ll t=;
while(l<r){
if((l|t)>r) break;
l|=t;
t<<=;
}
printf("%I64d\n",l);
}
return ;
}

CodeForces 485C Bits[贪心 二进制]的更多相关文章

  1. CodeForces484A Bits(贪心)

    CodeForces484A Bits(贪心) CodeForces484A 题目大意:给出范围[A.B].期望你给出某个数X满足X属于[A,B],而且X转成二进制的1的个数最多.假设有多个给出最小的 ...

  2. CodeForces - 158B.Taxi (贪心)

    CodeForces - 158B.Taxi (贪心) 题意分析 首先对1234的个数分别统计,4人组的直接加上即可.然后让1和3成对处理,只有2种情况,第一种是1多,就让剩下的1和2组队处理,另外一 ...

  3. codeforces 484A A. Bits(贪心)

    题目链接: A. Bits time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  4. Codeforces Round #276 (Div. 1) A. Bits 贪心

    A. Bits   Let's denote as  the number of bits set ('1' bits) in the binary representation of the non ...

  5. Codeforces 1208F Bits And Pieces 位运算 + 贪心 + dp

    题意:给你一个序列a, 问a[i] ^ (a[j] & a[k])的最大值,其中i < j < k. 思路:我们考虑对于每个a[i]求出它的最优解.因为是异或运算,所以我们从高位向 ...

  6. Codeforces 484A - Bits 二进制找1

    这题可以根据l, r 在二进制下的长度进行分类. l  的长度小于 r 的时候,有两种可能,一种是r 在二进制下是 1* 这种样子,故答案取 r : 一种是取答案为  (1LL << (r ...

  7. codeforces 724D(贪心)

    题目链接:http://codeforces.com/contest/724/problem/D 题意:给定一个字符串和一个数字m,选取一个一个子序列s,使得对于字符串中任意长度为m的子序列都至少含有 ...

  8. Codeforces 626G Raffles(贪心+线段树)

    G. Raffles time limit per test:5 seconds memory limit per test:256 megabytes input:standard input ou ...

  9. 51nod 1596 搬货物【贪心/二进制】

    1596 搬货物 题目来源: CodeForces 基准时间限制:1 秒 空间限制:131072 KB 分值: 10 难度:2级算法题  收藏  取消关注 现在有n个货物,第i个货物的重量是 2wi  ...

随机推荐

  1. MySQL使用if判断

    select *,if(sva=1,"男","女") as ssva from taname where sva<>"" 12. ...

  2. SqlServer-无限递归树状图结构设计和查询

    在现实生活中,公司的部门设计会涉及到很多子部门,然后子部门下面又存在子部门,形成类似判断的树状结构,比如说评论楼中楼的评论树状图,职位管理的树状图结构等等,实现类似的树状图数据结构是在开发中经常出现的 ...

  3. 十一个行为模式之模板方法模式(Template Method Pattern)

    定义: 定义一个操作中算法的框架,并且将一部分操作延迟到子类当中.使得子类可以不改变一个算法的结构即可重新定义算法步骤. 结构图: AbstractClass:抽象方法类,定义了一些基本操作,这些操作 ...

  4. 十一个行为模式之策略模式(Strategy Pattern)

    定义: 定义一系列的算法,将每一个算法封装起来,并使它们之间可以相互替换,让算法具有可扩展性和对立性. 结构图: Context:环境类,算法的使用者.对外提供了算法使用的接口,并且持有一个抽象算法类 ...

  5. iOS 真机测试 App installation failed

    真机测试的过程中,出现这种Bug This application's application-identifier entitlement does not match that of the in ...

  6. Apache日志按天切割

    Linux系统配置方法: 将其改为 ErrorLog "| /usr/local/apache/bin/rotatelogs /home/logs/www/error_%Y%m%d.log ...

  7. iOS开发-UI 从入门到精通(五)

    近日在做项目的时候,为了快捷适配屏幕采用了Storyboard,添加约束以后运行后发现一个问题(下面将以普通案例展示该问题);在4.7 甚至更大的屏幕下是没有问题的,如下图(4.7屏幕): 但是放到更 ...

  8. iOS UIMenuController菜单

    //1:普通 ////  ViewController.m//  DemoTest#import "ViewController.h"@interface ViewControll ...

  9. 安卓学习----使用okHttp(get方式)---下载图片

    一首先下载Jar包 https://github.com/square/okhttp 如果使用android studio只需要加入依赖 compile 'com.squareup.okhttp3:o ...

  10. Mac使用极简教程

    最近领导让我写一篇关于Mac的使用教程,因为使用人群未知,所以尽量写的通俗易懂,可谓是关于Mac电脑使用的精简教程吧,在此发表出来以供参考. Mac因为安全性而闻名,我们拥有了一部Mac,那么我们来了 ...