题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=5661
题意:给你两个区间
[a,b]
,
[c,d]
,找两个数
x,y
,
(a≤x≤b)
,
(c≤y≤d)
,使得
x⊕y
最大(
⊕
是异或)
题解:这题可能很多人一上来觉得是数位dp,反正我是不知道数位dp怎么写,估计也贼烦,其实正解应该是贪心,从大到小枚举位数,然后肯定是这一位能取1就取1,然后根据你的取值的情况,对上下界进行更新
具体怎么实现呢,我们把
aa,bb,cc,dd
认为是第
i
位上
1.
bb=1,aa=0
,此时肯定答案中这一位能取到1,如果
cc,dd
同为1,
c,d
的范围就不能修改,那么前者必须取0,即为此时取的是下界
a
,所以
2.
aa=bb
的情况,和上面讨论的类似,一样更新区间就行了
代码:
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <string>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <sstream>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
#define MAX 100005
#define MAXN 6005
#define maxnode 15
#define sigma_size 30
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define lrt rt<<1
#define rrt rt<<1|1
#define middle int m=(r+l)>>1
#define LL long long
#define ull unsigned long long
#define mem(x,v) memset(x,v,sizeof(x))
#define lowbit(x) (x&-x)
#define pii pair<int,int>
#define bits(a) __builtin_popcount(a)
#define mk make_pair
#define limit 10000
//const int prime = 999983;
const int INF = 0x3f3f3f3f;
const LL INFF = 0x3f3f;
const double pi = acos(-1.0);
//const double inf = 1e18;
const double eps = 1e-8;
const LL mod = 1e9+7;
const ull mx = 133333331;
/*****************************************************/
inline void RI(int &x) {
char c;
while((c=getchar())<'0' || c>'9');
x=c-'0';
while((c=getchar())>='0' && c<='9') x=(x<<3)+(x<<1)+c-'0';
}
/*****************************************************/
int main(){
int t;
cin>>t;
while(t--){
LL a,b,c,d;
scanf("%I64d%I64d%I64d%I64d",&a,&b,&c,&d);
LL ans=0;
for(int i=62;i>=0;i--){
int aa=((a>>i)&1);
int bb=((b>>i)&1);
int cc=((c>>i)&1);
int dd=((d>>i)&1);
if(aa!=bb){
if(cc!=dd){
ans+=(1LL<<i)+(1LL<<i)-1;
break;
}
else if(cc==1){
ans+=(1LL<<i);
b=(1LL<<i)-1;
}
else if(cc==0){
ans+=(1LL<<i);
a=0;
}
}
else if(aa==0){
if(cc!=dd){
ans+=(1LL<<i);
c=0;
}
else if(cc==1) ans+=(1LL<<i);
}
else if(aa==1){
if(cc!=dd){
ans+=(1LL<<i);
d=(1LL<<i)-1;
}
else if(cc==0) ans+=(1LL<<i);
}
}
cout<<ans<<endl;
}
return 0;
}