109962024-05-29 21:37:10szilEsoteric Bovinescpp17Accepted 100/100782ms286148 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;
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted2ms356 KiB
subtask210/10
2Accepted3ms676 KiB
3Accepted6ms2056 KiB
4Accepted4ms740 KiB
5Accepted4ms1040 KiB
6Accepted6ms2148 KiB
7Accepted4ms1124 KiB
8Accepted6ms2276 KiB
9Accepted4ms996 KiB
10Accepted6ms1892 KiB
11Accepted4ms1296 KiB
12Accepted4ms1048 KiB
13Accepted6ms1764 KiB
14Accepted4ms1788 KiB
subtask315/15
15Accepted3ms624 KiB
16Accepted4ms892 KiB
17Accepted46ms8248 KiB
18Accepted259ms26024 KiB
19Accepted231ms25828 KiB
20Accepted231ms26024 KiB
21Accepted259ms25956 KiB
22Accepted236ms25956 KiB
23Accepted216ms26084 KiB
subtask430/30
24Accepted23ms908 KiB
25Accepted35ms4708 KiB
26Accepted54ms12260 KiB
27Accepted48ms9380 KiB
28Accepted37ms6692 KiB
29Accepted50ms8568 KiB
30Accepted57ms9956 KiB
31Accepted46ms8420 KiB
32Accepted52ms8644 KiB
33Accepted46ms8164 KiB
34Accepted52ms9572 KiB
subtask545/45
35Accepted135ms40720 KiB
36Accepted333ms117860 KiB
37Accepted617ms285964 KiB
38Accepted3ms612 KiB
39Accepted6ms2020 KiB
40Accepted13ms4836 KiB
41Accepted19ms7012 KiB
42Accepted35ms15736 KiB
43Accepted35ms16228 KiB
44Accepted23ms868 KiB
45Accepted34ms4584 KiB
46Accepted56ms12196 KiB
47Accepted65ms13464 KiB
48Accepted103ms18428 KiB
49Accepted128ms22904 KiB
50Accepted115ms18532 KiB
51Accepted116ms16108 KiB
52Accepted224ms26104 KiB
53Accepted264ms26084 KiB
54Accepted263ms26004 KiB
55Accepted722ms286148 KiB
56Accepted671ms280592 KiB
57Accepted261ms26020 KiB
58Accepted782ms275940 KiB
59Accepted694ms275940 KiB
60Accepted771ms275960 KiB
61Accepted694ms275944 KiB
62Accepted694ms275940 KiB