Advertisement
tien_noob

BinaryTrie - Random Idea

Dec 16th, 2021
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.39 KB | None | 0 0
  1. //Make CSP great again
  2. //You're as beautiful as the day I lost you
  3. #include <bits/stdc++.h>
  4. #define TASK "TESTCODE"
  5. #define Log2(x) 31 - __builtin_clz(x)
  6. using namespace std;
  7. struct BinaryTrie
  8. {
  9.     struct node
  10.     {
  11.         int child[2], cnt;
  12.         node()
  13.         {
  14.             memset(child, -1, sizeof(child));
  15.             cnt = 0;
  16.         }
  17.     };
  18.     vector<node> Tree;
  19.     void new_node()
  20.     {
  21.         node tmp;
  22.         Tree.push_back(tmp);
  23.     }
  24.     void add(long long& num)
  25.     {
  26.         int id = 0;
  27.         for (int i = 60; i >= 0; --i)
  28.         {
  29.             int c = (num >> i) & 1;
  30.             if (Tree[id].child[c] == -1)
  31.             {
  32.                 Tree[id].child[c] = Tree.size();
  33.                 new_node();
  34.             }
  35.             id = Tree[id].child[c];
  36.             ++Tree[id].cnt;
  37.             //cerr << c;
  38.         }
  39.     }
  40.     long long Find(int k)
  41.     {
  42.         long long res = 0;
  43.         int id = 0;
  44.         for (int i = 60; i >= 0; -- i)
  45.         {
  46.             if (Tree[id].child[0] == -1)
  47.             {
  48.                 id = Tree[id].child[1];
  49.                 res |= 1LL << i;
  50.                 continue;
  51.             }
  52.             else if (Tree[id].child[1] == -1)
  53.             {
  54.                 id = Tree[id].child[0];
  55.                 continue;
  56.             }
  57.             if (Tree[Tree[id].child[0]].cnt >= k)
  58.             {
  59.                 id = Tree[id].child[0];
  60.             }
  61.             else
  62.             {
  63.                 k -= Tree[Tree[id].child[0]].cnt;
  64.                 res |= 1LL << i;
  65.                 id = Tree[id].child[1];
  66.             }
  67.         }
  68.         return res;
  69.     }
  70. } Tree;
  71. int n;
  72. void read()
  73. {
  74.     Tree.new_node();
  75.     cin >> n;
  76.     for (int i = 1; i <= n; ++ i)
  77.     {
  78.         long long a;
  79.         cin >> a;
  80.         Tree.add(a);
  81.     }
  82. }
  83. void solve()
  84. {
  85.     int q;
  86.     cin >> q;
  87.     while(q--)
  88.     {
  89.         int k;
  90.         cin >> k;
  91.         cout << Tree.Find(k) << '\n';
  92.     }
  93. }
  94. int main()
  95. {
  96.     ios_base::sync_with_stdio(false);
  97.     cin.tie(nullptr);
  98.     if (fopen(TASK".INP", "r"))
  99.     {
  100.         freopen(TASK".INP", "r", stdin);
  101.         //freopen(TASK".OUT", "w", stdout);
  102.     }
  103.     int t = 1;
  104.     bool typetest = false;
  105.     if (typetest)
  106.     {
  107.         cin >> t;
  108.     }
  109.     for (int __ = 1; __ <= t; ++ __)
  110.     {
  111.         //cout << "Case " << __ << ": ";
  112.         read();
  113.         solve();
  114.     }
  115. }
  116.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement