http://codeforces.com/contest/484/problem/A

题意:

询问[a,b]中二进制位1最多且最小的数

贪心,假设开始每一位都是1

从高位i开始枚举,

如果当前数>b,且减去1<<i后仍>=a,就减1<<i

当当前数在[a,b]之间时,输出

因为从高位开始减,所以保证当前数是最小的

  1. #include<cstdio>
  2. #include<iostream>
  3.  
  4. using namespace std;
  5.  
  6. typedef long long LL;
  7.  
  8. LL bit[];
  9.  
  10. void read(LL &x)
  11. {
  12. x=; char c=getchar();
  13. while(!isdigit(c)) c=getchar();
  14. while(isdigit(c)) { x=x*+c-''; c=getchar(); }
  15. }
  16.  
  17. int main()
  18. {
  19. bit[]=;
  20. for(int i=;i<=;++i) bit[i]=bit[i-]<<;
  21. int n;
  22. scanf("%d",&n);
  23. LL a,b;
  24. LL ans;
  25. while(n--)
  26. {
  27. read(a); read(b);
  28. ans=(1LL<<)-;
  29. for(int i=;i>=;--i)
  30. {
  31. if(ans>b && ans-bit[i]>=a) ans-=bit[i];
  32. if(ans>=a && ans<=b) break;
  33. }
  34. cout<<ans<<'\n';
  35. }
  36. }
A. Bits
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard 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
  1. 3
    1 2
    2 4
    1 10
output
  1. 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

CF&&CC百套计划4 Codeforces Round #276 (Div. 1) A. Bits的更多相关文章

  1. CF&&CC百套计划4 Codeforces Round #276 (Div. 1) E. Sign on Fence

    http://codeforces.com/contest/484/problem/E 题意: 给出n个数,查询最大的在区间[l,r]内,长为w的子区间的最小值 第i棵线段树表示>=i的数 维护 ...

  2. CF&&CC百套计划3 Codeforces Round #204 (Div. 1) A. Jeff and Rounding

    http://codeforces.com/problemset/problem/351/A 题意: 2*n个数,选n个数上取整,n个数下取整 最小化 abs(取整之后数的和-原来数的和) 先使所有的 ...

  3. CF&&CC百套计划3 Codeforces Round #204 (Div. 1) D. Jeff and Removing Periods

    http://codeforces.com/problemset/problem/351/D 题意: n个数的一个序列,m个操作 给出操作区间[l,r], 首先可以删除下标为等差数列且数值相等的一些数 ...

  4. CF&&CC百套计划3 Codeforces Round #204 (Div. 1) E. Jeff and Permutation

    http://codeforces.com/contest/351/problem/E 题意: 给出一些数,可以改变任意数的正负,使序列的逆序对数量最少 因为可以任意加负号,所以可以先把所有数看作正数 ...

  5. CF&&CC百套计划3 Codeforces Round #204 (Div. 1) B. Jeff and Furik

    http://codeforces.com/contest/351/problem/B 题意: 给出一个n的排列 第一个人任选两个相邻数交换位置 第二个人有一半的概率交换相邻的第一个数>第二个数 ...

  6. CF&&CC百套计划1 Codeforces Round #449 C. Willem, Chtholly and Seniorious (Old Driver Tree)

    http://codeforces.com/problemset/problem/896/C 题意: 对于一个随机序列,执行以下操作: 区间赋值 区间加 区间求第k小 区间求k次幂的和 对于随机序列, ...

  7. CF&&CC百套计划1 Codeforces Round #449 B. Ithea Plays With Chtholly

    http://codeforces.com/contest/896/problem/B 题意: 交互题 n张卡片填m个1到c之间的数,1<=n*ceil(c/2)<=m 最后填出一个单调非 ...

  8. CF&&CC百套计划1 Codeforces Round #449 A. Nephren gives a riddle

    http://codeforces.com/contest/896/problem/A 第i个字符串嵌套第i-1个字符串 求第n个字符串的第k个字母 dfs #include<map> # ...

  9. CF&&CC百套计划2 CodeChef December Challenge 2017 Chef And Easy Xor Queries

    https://www.codechef.com/DEC17/problems/CHEFEXQ 题意: 位置i的数改为k 询问区间[1,i]内有多少个前缀的异或和为k 分块 sum[i][j] 表示第 ...

随机推荐

  1. WebSplider在线爬虫

    WebSplider是什么? WebSplider在线爬虫是一个结合Web技术与爬虫技术的项目. WebSplider支持Web页面进行爬虫配置,提交配置到服务器后,服务器端爬虫程序进行数据抓取,最后 ...

  2. jumpserver部署

    1.部署环境.安装依赖包 # yum install git python-pip mysql-devel gcc automake autoconf python-devel vim sshpass ...

  3. 华为云Istio服务网格,让应用治理智能化、可视化

  4. 实战重现隐藏在A标签_blank下的危险漏洞,简略说明noopener的作用

    前几日,在看阮一峰老师的博客文章中,发现了这么一篇 .标题为 <target = "_blank" 的危险性(英文)>.这篇文章同事看过之后因为不理解其中的危险之处,念 ...

  5. 一张图理解Git

    更详细的git介绍:Git操作指南

  6. “Linux内核分析”实验三报告

    构造一个简单的Linux系统 张文俊+原创作品转载请注明出处+<Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-10000290 ...

  7. java第二次实验报告

    课程:Java实验   班级:201352     姓名:池彬宁  学号:20135212 成绩:             指导教师:娄佳鹏   实验日期:15.05.05 实验密级:         ...

  8. 20172308 实验五《Java面向对象程序设计 》实验报告

    20172308 2017-2018-2 实验五 <网络编程与安全>报告 课程:<程序设计与数据结构> 班级: 1723 姓名: 周亚杰 学号:20172308 实验教师:王志 ...

  9. JAVA面对对象(一)——封装

    1.封装思想:将对象的属性和行为封装起来的载体是类,类通常对客户隐藏其实现的细节 2.封装就是将属性私有化(private),并提供公共的方法(public)访问私有属性 3.通过封装,实现对属性数据 ...

  10. TAC队--团队选题报告

    The Art of Code--团队选题报告 一个APP项目能否被大众所接受,能否拥有受众,the most important是它的idea. 一个好的idea可以为项目带来巨大的改变 一个 独特 ...