Claris and XOR

Problem Description

Claris loves bitwise operations very much, especially XOR, because it has many beautiful features. He gets four positive integers a,b,c,da,b,c,d that satisfies a\leq bab and c\leq dcd. He wants to choose two integers x,yx,y that satisfies a\leq x\leq baxb and c\leq y\leq dcyd, and maximize the value of x~XOR~yx XOR y. But he doesn't know how to do it, so please tell him the maximum value of x~XOR~yx XOR y.

Input

The first line contains an integer T\left(1\leq T\leq10,000\right)T(1T10,000)——The number of the test cases. For each test case, the only line contains four integers a,b,c,d\left(1\leq a,b,c,d\leq10^{18}\right)a,b,c,d(1a,b,c,d1018​​). Between each two adjacent integers there is a white space separated.

Output

For each test case, the only line contains a integer that is the maximum value of x~XOR~yx XOR y.

Sample Input
2
1 2 3 4
5 7 13 15
Sample Output
Copy
6
11
Hint
In the first test case, when and only when x=2,y=4x=2,y=4, the value of x~XOR~yx XOR y is the maximum. In the second test case, when and only when x=5,y=14x=5,y=14 or x=6,y=13x=6,y=13, the value of x~XOR~yx XOR y is the maximum.
 
 
 
这游戏真难,之前写过一道类似的,就直接敲了...o(n)...超时....
 
 
TLE代码:
 1 #include <vector>
 2 #include <map>
 3 #include <set>
 4 #include <algorithm>
 5 #include <iostream>
 6 #include <cstdio>
 7 #include <cmath>
 8 #include <cstdlib>
 9 #include <string>
10 #include <cstring>
11 #include <queue>
12 using namespace std;
13 #define INF 0x3f3f3f3f
14 #define ll long long
15 
16 int const MAX = 100000005;
17 int n;
18 
19 struct Trie
20 {
21     int root, tot, next[MAX][2], end[MAX];
22     inline int node()
23     {
24         memset(next[tot], -1, sizeof(next[tot]));
25         end[tot] = 0;
26         return tot ++;
27     }
28 
29     inline void Init()
30     {
31         tot = 0;
32         root = node();
33     }
34 
35     inline void insert(ll x)
36     {
37         int p = root;
38         for(int i = 31; i >= 0; i--)
39         {
40             int ID = ((1 << i) & x) ? 1 : 0;
41             if(next[p][ID] == -1)
42                 next[p][ID] = node();
43             p = next[p][ID];
44         }
45         end[p] = x;
46     }
47 
48     inline int search(int x)
49     {
50         int p = root;
51         for(int i = 31; i >= 0; i--)
52         {
53             int ID = ((1 << i) & x) ? 1 : 0;
54             if(ID == 0)
55                 p = next[p][1] != -1 ? next[p][1] : next[p][0];
56             else
57                 p = next[p][0] != -1 ? next[p][0] : next[p][1];
58         }
59         return x ^ end[p];
60     }
61 
62 }trie;
63 
64 int a[2],b[2];
65 int main()
66 {
67     int t;
68     scanf("%d",&t);
69     while(t--)
70     {
71         int n=4;
72         int WTF = 0,ans=0, x;
73         trie.Init();
74         for(int i = 0; i < 2; i++)
75         {
76             scanf("%d", &a[i]);
77         }
78         for(int i = 0; i < 2; i++)
79         {
80             scanf("%d", &b[i]);
81         }
82         for(int i=a[0]; i<=a[1]; i++){
83             //WTF=0;
84             //trie.insert(1);
85             //WTF = WTFx(WTF, trie.search(1));
86             for(int j=b[0]; j<=b[1]; j++){
87                 trie.Init();
88                 trie.insert(i);
89                 WTF = max(WTF, trie.search(i));
90                 trie.insert(j);
91                 WTF = max(WTF, trie.search(j));
92                 ans=max(WTF,ans);
93             }
94         }    
95         printf("%d\n", WTF);
96     }
97 }

 

 

转载于:https://www.cnblogs.com/wangmengmeng/p/5372629.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值