题意:
给定两个区间,在两个区间任意选择两个数,求最大的异或值?
思路:
贪心
从高位开始枚举,优先选择放置01或者10,每次判断这次的选择是否合法,然后就没了!
代码:
#include <iostream>
#include <cstdio>
#define PB push_back
#define FT first
#define SD second
#define MP make_pair
#define INF 0x3f3f3f3f
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> P;
const int N = 233,MOD = 7+1e9;
LL a, b, c, d;
bool isok(LL x, LL y, int p)
{
LL xr = x + (1LL << p) - 1;
LL yr = y + (1LL << p) - 1;
if(x > b || xr < a) return 0;
if(y > d || yr < c) return 0;
return 1;
}
int main()
{
int T;
scanf("%d", &T);
while(T --) {
scanf("%I64d%I64d%I64d%I64d", &a, &b, &c, &d);
LL x = 0, y = 0, ans = 0;
for(int i = 61;i >= 0;i --) {
if(isok(x, y + (1LL << i), i)) {
y += (1LL << i);
}
else if(isok(x + (1LL << i), y, i)) {
x += (1LL << i);
}
else if(isok(x + (1LL << i), y + (1LL << i), i)) {
y += (1LL << i);
x += (1LL << i);
}
else ;
}
printf("%I64d\n", x ^ y);
}
return 0;
}