10996 | 2024-05-29 21:37:10 | szil | Esoteric Bovines | cpp17 | Accepted 100/100 | 782ms | 286148 KiB |
#include <bits/stdc++.h>
using ll = long long;
using namespace std;
const int MAXN = 200'001;
struct Node {
Node *nxt[2];
int siz;
Node() {
nxt[0] = nxt[1] = nullptr;
siz = 0;
}
};
void add(Node *root, ll val) {
root->siz++;
for (int i = 60; i >= 0; i--) {
int bit = (val >> i) & 1;
if (!root->nxt[bit]) root->nxt[bit] = new Node();
root = root->nxt[bit];
root->siz++;
}
}
void solve() {
ll n, k; cin >> n >> k;
Node *root = new Node();
vector<ll> a(n + 1);
for(int i = 1; i <= n; i++) cin >> a[i];
vector<Node *> pt(n + 1, root);
for(int i = 1; i <= n; i++)
{
ll x;
cin >> x;
add(root, x);
}
ll ans = 0;
for(int j = 60; j >= 0; j--)
{
ll x = 0;
for(int i = 1; i <= n; i++) {
if(pt[i] && pt[i]->nxt[(a[i] >> j) & 1]) x += pt[i]->nxt[(a[i] >> j) & 1]->siz;
}
if(x < k)
{
ans |= 1LL<<j;
k -= x;
for(int i = 1; i <= n; i++)
{
if (!pt[i]) continue;
int dir = !((a[i] >> j) & 1);
if(!pt[i]->nxt[dir]) pt[i] = nullptr;
else pt[i] = pt[i]->nxt[dir];
}
} else {
for (int i = 1; i <= n; i++) {
if (!pt[i]) continue;
int dir = (a[i] >> j) & 1;
if(!pt[i]->nxt[dir]) pt[i] = nullptr;
else pt[i] = pt[i]->nxt[dir];
}
}
}
cout << ans << "\n";
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
int t = 1;
cin >> t;
while (t--) {
solve();
}
return 0;
}
Subtask | Sum | Test | Verdict | Time | Memory | ||
---|---|---|---|---|---|---|---|
subtask1 | 0/0 | ||||||
1 | Accepted | 2ms | 356 KiB | ||||
subtask2 | 10/10 | ||||||
2 | Accepted | 3ms | 676 KiB | ||||
3 | Accepted | 6ms | 2056 KiB | ||||
4 | Accepted | 4ms | 740 KiB | ||||
5 | Accepted | 4ms | 1040 KiB | ||||
6 | Accepted | 6ms | 2148 KiB | ||||
7 | Accepted | 4ms | 1124 KiB | ||||
8 | Accepted | 6ms | 2276 KiB | ||||
9 | Accepted | 4ms | 996 KiB | ||||
10 | Accepted | 6ms | 1892 KiB | ||||
11 | Accepted | 4ms | 1296 KiB | ||||
12 | Accepted | 4ms | 1048 KiB | ||||
13 | Accepted | 6ms | 1764 KiB | ||||
14 | Accepted | 4ms | 1788 KiB | ||||
subtask3 | 15/15 | ||||||
15 | Accepted | 3ms | 624 KiB | ||||
16 | Accepted | 4ms | 892 KiB | ||||
17 | Accepted | 46ms | 8248 KiB | ||||
18 | Accepted | 259ms | 26024 KiB | ||||
19 | Accepted | 231ms | 25828 KiB | ||||
20 | Accepted | 231ms | 26024 KiB | ||||
21 | Accepted | 259ms | 25956 KiB | ||||
22 | Accepted | 236ms | 25956 KiB | ||||
23 | Accepted | 216ms | 26084 KiB | ||||
subtask4 | 30/30 | ||||||
24 | Accepted | 23ms | 908 KiB | ||||
25 | Accepted | 35ms | 4708 KiB | ||||
26 | Accepted | 54ms | 12260 KiB | ||||
27 | Accepted | 48ms | 9380 KiB | ||||
28 | Accepted | 37ms | 6692 KiB | ||||
29 | Accepted | 50ms | 8568 KiB | ||||
30 | Accepted | 57ms | 9956 KiB | ||||
31 | Accepted | 46ms | 8420 KiB | ||||
32 | Accepted | 52ms | 8644 KiB | ||||
33 | Accepted | 46ms | 8164 KiB | ||||
34 | Accepted | 52ms | 9572 KiB | ||||
subtask5 | 45/45 | ||||||
35 | Accepted | 135ms | 40720 KiB | ||||
36 | Accepted | 333ms | 117860 KiB | ||||
37 | Accepted | 617ms | 285964 KiB | ||||
38 | Accepted | 3ms | 612 KiB | ||||
39 | Accepted | 6ms | 2020 KiB | ||||
40 | Accepted | 13ms | 4836 KiB | ||||
41 | Accepted | 19ms | 7012 KiB | ||||
42 | Accepted | 35ms | 15736 KiB | ||||
43 | Accepted | 35ms | 16228 KiB | ||||
44 | Accepted | 23ms | 868 KiB | ||||
45 | Accepted | 34ms | 4584 KiB | ||||
46 | Accepted | 56ms | 12196 KiB | ||||
47 | Accepted | 65ms | 13464 KiB | ||||
48 | Accepted | 103ms | 18428 KiB | ||||
49 | Accepted | 128ms | 22904 KiB | ||||
50 | Accepted | 115ms | 18532 KiB | ||||
51 | Accepted | 116ms | 16108 KiB | ||||
52 | Accepted | 224ms | 26104 KiB | ||||
53 | Accepted | 264ms | 26084 KiB | ||||
54 | Accepted | 263ms | 26004 KiB | ||||
55 | Accepted | 722ms | 286148 KiB | ||||
56 | Accepted | 671ms | 280592 KiB | ||||
57 | Accepted | 261ms | 26020 KiB | ||||
58 | Accepted | 782ms | 275940 KiB | ||||
59 | Accepted | 694ms | 275940 KiB | ||||
60 | Accepted | 771ms | 275960 KiB | ||||
61 | Accepted | 694ms | 275944 KiB | ||||
62 | Accepted | 694ms | 275940 KiB |