Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Make CSP great again
- //You're as beautiful as the day I lost you
- #include <bits/stdc++.h>
- #define TASK "TESTCODE"
- #define Log2(x) 31 - __builtin_clz(x)
- using namespace std;
- struct BinaryTrie
- {
- struct node
- {
- int child[2], cnt;
- node()
- {
- memset(child, -1, sizeof(child));
- cnt = 0;
- }
- };
- vector<node> Tree;
- void new_node()
- {
- node tmp;
- Tree.push_back(tmp);
- }
- void add(long long& num)
- {
- int id = 0;
- for (int i = 60; i >= 0; --i)
- {
- int c = (num >> i) & 1;
- if (Tree[id].child[c] == -1)
- {
- Tree[id].child[c] = Tree.size();
- new_node();
- }
- id = Tree[id].child[c];
- ++Tree[id].cnt;
- //cerr << c;
- }
- }
- long long Find(int k)
- {
- long long res = 0;
- int id = 0;
- for (int i = 60; i >= 0; -- i)
- {
- if (Tree[id].child[0] == -1)
- {
- id = Tree[id].child[1];
- res |= 1LL << i;
- continue;
- }
- else if (Tree[id].child[1] == -1)
- {
- id = Tree[id].child[0];
- continue;
- }
- if (Tree[Tree[id].child[0]].cnt >= k)
- {
- id = Tree[id].child[0];
- }
- else
- {
- k -= Tree[Tree[id].child[0]].cnt;
- res |= 1LL << i;
- id = Tree[id].child[1];
- }
- }
- return res;
- }
- } Tree;
- int n;
- void read()
- {
- Tree.new_node();
- cin >> n;
- for (int i = 1; i <= n; ++ i)
- {
- long long a;
- cin >> a;
- Tree.add(a);
- }
- }
- void solve()
- {
- int q;
- cin >> q;
- while(q--)
- {
- int k;
- cin >> k;
- cout << Tree.Find(k) << '\n';
- }
- }
- int main()
- {
- ios_base::sync_with_stdio(false);
- cin.tie(nullptr);
- if (fopen(TASK".INP", "r"))
- {
- freopen(TASK".INP", "r", stdin);
- //freopen(TASK".OUT", "w", stdout);
- }
- int t = 1;
- bool typetest = false;
- if (typetest)
- {
- cin >> t;
- }
- for (int __ = 1; __ <= t; ++ __)
- {
- //cout << "Case " << __ << ": ";
- read();
- solve();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement