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

时间:2023-03-09 06:40:10
codeforces 484a//Bits// Codeforces Round #276(Div. 1)

题意:给出区间[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 ;
}