Submission #1365968

#TimeUsernameProblemLanguageResultExecution timeMemory
1365968saywooKirameki of Revue (KAISTRUN26SPRING_E)C++20
100 / 100
568 ms68872 KiB
#include <bits/stdc++.h>
using namespace std;

using ll = long long int;

struct BinaryTrie {
    int cnt;
    bool finish;
    BinaryTrie* next[2];

    const int DEPTH = 30; // 2^DEPTH

    BinaryTrie() { finish = 0; cnt = 0; next[0] = next[1] = NULL; }
    ~BinaryTrie() {
        if (next[0]) delete next[0];
        if (next[1]) delete next[1];
    }
    void Insert(const ll x) {
        BinaryTrie* ptr = this;
        for (int y = DEPTH; y >= 0; y--) {
            ptr->cnt++;
            if (!ptr->next[(x>>y)&1]) ptr->next[(x>>y)&1] = new BinaryTrie;
            ptr = ptr->next[(x>>y)&1];
        }
        ptr->finish = true;
        ptr->cnt++;
    }
    bool Find(const ll x) {
        BinaryTrie* ptr = this;
        for (int y = DEPTH; y >= 0; y--) {
            if (!ptr->next[(x>>y)&1]) return false;
            ptr = ptr->next[(x>>y)&1];
        }
        return ptr->finish;
    }
    bool Delete(const ll x, const int y) {
        if (y < 0) finish = false;
        else {
            if (!next[(x>>y)&1]->Delete(x, y - 1)) {
                delete next[(x>>y)&1]; next[(x>>y)&1] = nullptr;
            }
        }
        return --cnt;
    }
};

ll n, k;
ll a[101010];

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);

    cin >> n >> k;
    BinaryTrie* trie = new BinaryTrie;
    for (int i = 1; i <= n; i++) {
        cin >> a[i]; trie->Insert(a[i]);
    }

    ll l = -1; ll r = INT_MAX;

    while (l + 1 < r) {
        ll m = (l + r) >> 1;

        ll cnt = 0;

        for (int i = 1; i <= n; i++) {
            BinaryTrie* ptr = trie;
            ll tmp = m;
            for (int j = 30; j >= 0; j--) {
                int now = (a[i] & (1 << j)) > 0;

                if (tmp >= (1 << j)) {
                    if (ptr->next[now]) cnt += ptr->next[now]->cnt;
                    now ^= 1;
                    tmp ^= (1 << j);
                }
                if (!ptr->next[now]) break;
                ptr = ptr->next[now];
            }
        }

        cnt = (cnt - n) / 2;

        if (cnt < k) l = m;
        else r = m;
    }

    cout << r - 1 << '\n';

    return 0;
}
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...