1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
|
#include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <algorithm> #include <stack> #include <map> #include <set> #include <vector> #include <queue> #include <string> #include <cmath> using namespace std; #define ll long long #define rep(i,n) for(int i =0; i < n; ++i) #define CLR(x) memset(x,0,sizeof x) #define MEM(a,b) memset(a,b,sizeof a) #define pr(x) cout << #x << " = " << x << " "; #define prln(x) cout << #x << " = " << x << endl; const int maxn = 4e6+100; const int INF = 0x3f3f3f3f; int code[40]; struct Node{ int l, r; }node[maxn]; int ans, sz; void build(int pos, int id, int x){ if(pos == -1) return; int c = x & (1<<pos); if(c) { if(node[id].l == -1) { node[id].l = sz++; } build(pos-1, node[id].l, x); } else { if(node[id].r == -1){ node[id].r = sz++; } build(pos-1, node[id].r, x); } } void dfs(int pos, int id, int x){ if(pos == -1) { if(x > ans) ans = x; return; } int c = x & (1<<pos); if(c) { if(node[id].r != -1){ dfs(pos-1, node[id].r, x); } else { if((ans&(1<<pos)) && ans >= x) return; dfs(pos-1, node[id].l, x^(1<<pos)); } } else { if(node[id].l != -1){ dfs(pos-1, node[id].l, x^(1<<pos)); } else { if((ans&(1<<pos)) && ans >= x) return; dfs(pos-1, node[id].r, x); } } } int a[maxn], b[maxn]; int main(){ #ifdef LOCAL freopen("/home/zeroxf/桌面/in.txt","r",stdin); #endif int n, m, t; sz = maxn; scanf("%d", &t); while(t--){ for(int i = 0; i < sz; ++i){ node[i].l = node[i].r = -1; } sz = 1; ans = 0; int x; scanf("%d%d", &n, &m); for(int i = 0; i < n; ++i){ scanf("%d", &x); build(30, 0, x); } for(int i = 0; i < m; ++i){ scanf("%d", &x); dfs(30, 0, x); } printf("%d\n", ans); } return 0; }
|