Advertisement
Dang_Quan_10_Tin

XQUERY Anh Thái 11.1.2022 VOI22

Jan 11th, 2022
801
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.48 KB | None | 0 0
  1. #define task ""
  2.  
  3. #include <iostream>
  4. #include <cstdio>
  5. #include <cstring>
  6.  
  7. using namespace std;
  8.  
  9. using ll = long long;
  10. using ld = long double;
  11.  
  12. constexpr int N = 1e5 + 5;
  13. int q;
  14.  
  15. struct node
  16. {
  17.     int cnt[18], sum;
  18.     node *child[2];
  19.  
  20.     node()
  21.     {
  22.         memset(cnt, 0, sizeof cnt);
  23.         sum = 0;
  24.         child[0] = child[1] = NULL;
  25.     }
  26. };
  27.  
  28. struct Trie
  29. {
  30.     const int maxbit = 17;
  31.     node root;
  32.     int _xor;
  33.  
  34. #define bit(i, x) (((x) >> (i)) & 1)
  35.  
  36.     Trie()
  37.     {
  38.         _xor = 0;
  39.     }
  40.  
  41.     void Add(int x)
  42.     {
  43.         int id = maxbit;
  44.         node *a = &root;
  45.  
  46.         x ^= _xor;
  47.  
  48.         while (1)
  49.         {
  50.             for (int i = 17; ~i; --i)
  51.                 if (bit(i, x))
  52.                     ++a->cnt[i];
  53.             ++a->sum;
  54.  
  55.             if (id == -1)
  56.                 return;
  57.  
  58.             if (!(a->child[bit(id, x)]))
  59.                 a->child[bit(id, x)] = new node;
  60.  
  61.             a = a->child[bit(id, x)];
  62.             --id;
  63.         }
  64.     }
  65.  
  66.     bool Del(node *a, int x, int id)
  67.     {
  68.         if (id == -1)
  69.         {
  70.             if (a->sum)
  71.             {
  72.                 for (int i = 17; ~i; --i)
  73.                     if (bit(i, x))
  74.                         --a->cnt[i];
  75.                 --a->sum;
  76.                 return true;
  77.             }
  78.             else
  79.                 return false;
  80.         }
  81.  
  82.         if ((a->child[bit(id, x)]) && Del(a->child[bit(id, x)], x, id - 1))
  83.         {
  84.             for (int i = 17; ~i; --i)
  85.                 if (bit(i, x))
  86.                     --a->cnt[i];
  87.             --a->sum;
  88.             return true;
  89.         }
  90.  
  91.         return false;
  92.     }
  93.  
  94.     void Delete(int x)
  95.     {
  96.         x ^= _xor;
  97.         Del(&root, x, maxbit);
  98.     }
  99.  
  100.     ll Get(int k)
  101.     {
  102.         int id = maxbit;
  103.         node *a = &root;
  104.         ll ans(0);
  105.  
  106.         while (1)
  107.         {
  108.             if (id == -1)
  109.             {
  110.                 for (int i = 17; ~i; --i)
  111.                     if (bit(i, _xor))
  112.                         ans += (1ll << i) * min(k, a->sum - a->cnt[i]);
  113.                     else
  114.                         ans += (1ll << i) * min(k, a->cnt[i]);
  115.                 break;
  116.             }
  117.  
  118.             int x = 0, y = 1;
  119.  
  120.             if (bit(id, _xor))
  121.                 swap(x, y);
  122.  
  123.             if ((a->child[x]) && a->child[x]->sum >= k)
  124.                 a = a->child[x];
  125.             else
  126.             {
  127.                 if (a->child[x])
  128.                 {
  129.                     k -= a->child[x]->sum;
  130.  
  131.                     for (int i = 17; ~i; --i)
  132.                         if (bit(i, _xor))
  133.                             ans += (1ll << i) * (a->child[x]->sum - a->child[x]->cnt[i]);
  134.                         else
  135.                             ans += (1ll << i) * a->child[x]->cnt[i];
  136.                 }
  137.  
  138.                 a = a->child[y];
  139.             }
  140.  
  141.             --id;
  142.         }
  143.         return ans;
  144.     }
  145. } f;
  146.  
  147. void Read()
  148. {
  149.     cin >> q;
  150. }
  151.  
  152. void Solve()
  153. {
  154.     while (q--)
  155.     {
  156.         int t, x;
  157.         cin >> t >> x;
  158.         if (t == 0)
  159.             f.Add(x);
  160.         else if (t == 1)
  161.             f.Delete(x);
  162.         else if (t == 2)
  163.             f._xor ^= x;
  164.         else
  165.             cout << f.Get(x) << "\n";
  166.     }
  167. }
  168.  
  169. int32_t main()
  170. {
  171.     ios::sync_with_stdio(0);
  172.     cin.tie(0);
  173.     cout.tie(0);
  174.     if (fopen(task ".INP", "r"))
  175.     {
  176.         freopen(task ".INP", "r", stdin);
  177.         freopen(task ".OUT", "w", stdout);
  178.     }
  179.  
  180.     Read();
  181.     Solve();
  182. }
  183.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement