题意:给出区间[ll,rr],求中间一个数二进制表示时一的个数最多。

写出ll和rr的二进制,设出现第一个不同的位置为pos(从高位到低位),找的数为x,那么为了使x在[ll,rr]内,前pos-1个位必须也相同。而rr在pos和pos后如果都为1,那么pos和pos后都取1,否则pos取0,pos后取1。

乱码:

//#pragma comment(linker,"/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
#include <stack>
#include <list>
using namespace std;
const int SZ=,INF=0x7FFFFFFF;
typedef long long lon;
const double EPS=1e-;
const lon one=; lon geth(lon x)
{
lon res=;
for(lon i=;i<;++i)
{
if(x&(one<<i))
{
res=max(res,i);
}
}
return res;
} int main()
{
std::ios::sync_with_stdio();
//freopen("d:\\1.txt","r",stdin);
lon n;
cin>>n;
for(int i=;i<n;++i)
{
lon ll,rr;
cin>>ll>>rr;
lon bit=geth(rr);
lon res=;
for(lon j=bit;j>=;--j)
{
lon b1=rr&(one<<j);
lon b2=ll&(one<<j);
if(b1^b2)
{
lon cand=;
for(lon k=j;k>=;--k)
{
if(rr&(one<<k))++cand;
}
res+=(one<<max(j,cand))-;
break;
}
else res+=((one<<j)&rr);
}
cout<<res<<endl;
} return ;
}

最新文章

  1. Fragment的startActivityForResult和Activity的startActivityForResult的区别
  2. ros下多机器人系统(1)
  3. ubuntu中安装VMWare tools
  4. [日常训练]curves
  5. JAVA作业02
  6. 软件工程 speedsnail 第二次冲刺8
  7. PHP错误The server encountered an internal error or misconfiguration and was unable to complete your re
  8. Sqli-labs less 29
  9. Imageview使用记录
  10. Apache 目录权限
  11. SQL Server 表字段值转换成字段名称(二)
  12. mybatis关联查询,查询结果多条,却只返回一条记录
  13. VS Code 的常用快捷键
  14. ABP官方文档翻译 4.2 数据传输对象
  15. 搭建iSCSI文件服务器故障转移群集
  16. int 4 bytes
  17. pycharm换行
  18. python数据分析Titanic_Survived预测
  19. 什么是安全散列算法SHA256?
  20. Java中的volatile关键字为什么不是不具有原子性

热门文章

  1. VS2010的快捷键;VS2012变化的快捷键
  2. Java线程基础知识(状态、共享与协作)
  3. pycharm中内看内建函数的定义
  4. Linux基础命令---e2image
  5. Unity VR编辑器――如上帝般创建VR内容,Project Soli google用雷达识别手势体积相当于一张 Mini SD 内存卡
  6. Kali连接不上ssh
  7. dba和运维专家们说有丰富的大型分布式系统架构设计经验纯属扯淡
  8. troubleshooting-执行导数shell脚本抛异常error=2, No such file or directory
  9. 20145221《网络对抗》PC平台逆向破解
  10. Win32 实现 MFC CFileDialog 对话框