-
Notifications
You must be signed in to change notification settings - Fork 0
/
XOR_948D.cpp
102 lines (94 loc) · 1.51 KB
/
XOR_948D.cpp
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
102
#include <bits/stdc++.h>
using namespace std;
#define MOD 1000000007
#define N 300005
#define M 30
#define ll long long
#define ld long double
#define pb push_back
#define ff first
#define ss second
struct trie{
int zero, one, l, r;
};
trie t[N*M];
int a[N], p[N];
int cur = 2, n;
void add(int x){
int b, node = 1;
for(b=M; b>=0; b--){
if(x & (1<<b)){
if(!t[node].one) t[node].one = cur++;
t[node].r++;
node = t[node].one;
}
else{
if(!t[node].zero) t[node].zero = cur++;
t[node].l++;
node = t[node].zero;
}
}
}
void del(int x){
int b, node = 1;
for(b=M; b>=0; b--){
if(x & (1<<b)){
t[node].r--;
if(t[node].r == 0){
t[node].one = 0; break;
}
node = t[node].one;
}
else{
t[node].l--;
if(t[node].l == 0){
t[node].zero = 0; break;
}
node = t[node].zero;
}
}
}
int get(int x){
int b, node = 1, po, ans;
ans = 0;
for(b=M; b>=0; b--){
po = (1<<b);
if(x & po){
if(!t[node].one){
ans = ans + po;
node = t[node].zero;
}
else node = t[node].one;
}
else{
if(!t[node].zero){
ans = ans + po;
node = t[node].one;
}
else node = t[node].zero;
}
}
return ans;
}
int main(){
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
cin>>n;
int i, ans;
for(i=0; i<n; i++){
cin>>a[i];
}
for(i=0; i<n; i++){
cin>>p[i];
add(p[i]);
}
for(i=0; i<n; i++){
ans = get(a[i]);
cout<<ans<<" ";
del(a[i] ^ ans);
}
return 0;
}