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

题意:

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

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

从高位i开始枚举,

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

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

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

#include<cstdio>
#include<iostream> using namespace std; typedef long long LL; LL bit[]; void read(LL &x)
{
x=; char c=getchar();
while(!isdigit(c)) c=getchar();
while(isdigit(c)) { x=x*+c-''; c=getchar(); }
} int main()
{
bit[]=;
for(int i=;i<=;++i) bit[i]=bit[i-]<<;
int n;
scanf("%d",&n);
LL a,b;
LL ans;
while(n--)
{
read(a); read(b);
ans=(1LL<<)-;
for(int i=;i>=;--i)
{
if(ans>b && ans-bit[i]>=a) ans-=bit[i];
if(ans>=a && ans<=b) break;
}
cout<<ans<<'\n';
}
}
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
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

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. 调用wx.request接口时需要注意的几个问题

    写在前面 之前写了一篇<微信小程序实现各种特效实例>,上次的小程序的项目我负责大部分前端后台接口的对接,然后学长帮我改了一些问题.总的来说,收获了不少吧! 现在项目已经完成,还是要陆陆续续 ...

  2. effective c++ 笔记 (30-31)

    //---------------------------15/04/17---------------------------- //#30   透彻了解inlineing的里里外外 { /* 1: ...

  3. html元素双击事件触发机制猜想及疑惑

    今天有个同事遇到一个奇怪的问题,我照着他的代码做了一些简化写了这个demo <!DOCTYPE html> <html> <head> <style type ...

  4. 【Unity Shader】(三) ------ 光照模型原理及漫反射和高光反射的实现

    [Unity Shader](三) ---------------- 光照模型原理及漫反射和高光反射的实现 [Unity Shader](四) ------ 纹理之法线纹理.单张纹理及遮罩纹理的实现 ...

  5. PAT甲题题解-1034. Head of a Gang (30)-并查集

    给出n和k接下来n行,每行给出a,b,c,表示a和b之间的关系度,表明他们属于同一个帮派一个帮派由>2个人组成,且总关系度必须大于k.帮派的头目为帮派里关系度最高的人.(注意,这里关系度是看帮派 ...

  6. Final发布——视频博客

    1.视频链接 视频上传至优酷自频道,地址链接:http://v.youku.com/v_show/id_XMzk1OTIwNTUwMA==.html?spm=a2h0j.11185381.listit ...

  7. LeetCode 633. 平方数之和

    题目: 给定一个非负整数 c ,你要判断是否存在两个整数 a 和 b,使得 a2 + b2 = c.     示例1: 输入: 5 输出: True 解释: 1 * 1 + 2 * 2 = 5 示例2 ...

  8. Daily Scrum - 12/07

    Meeting Minutes 确认基本完成了UI组件的基本功能的动画实现: 准备开始实行UI组件的合并: 讨论了长期计划算法的难点,以及简单版本的实现方案. 督促大家更新TFS: Burndown ...

  9. 第十一周(11.24-12.01)----ptim测试程序运行速度

    我在dos下用ptime指令对分数运算(http://www.cnblogs.com/YangXiaomoo/p/6095583.html)的运行时间进行了测试.测试结果一般都在0.212-0.217 ...

  10. d3 数学方法(伪随机数生成器 )

    一.正态(高斯)分布(normal (Gaussian) distribution)的随机数 /* var nomarlRandmo = d3.random.normal(); console.log ...