Dang_Quan_10_Tin

COW ON TRIE

Jul 28th, 2020
303
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.86 KB | None | 0 0
  1. #define NguyenDangQuan the_author
  2. #define my_heart love_you_to_the_moon_and_back
  3.  
  4. #include <bits/stdc++.h>
  5. using namespace std;
  6. using ll = long long;
  7. using ld = long double;
  8. using ull = unsigned long long;
  9.  
  10. bool typetest;
  11. inline void fastIOfileinput(){
  12.     ios::sync_with_stdio(0);
  13.     cin.tie(0);
  14.     cout.tie(0);
  15.     typetest = 0;
  16. }
  17.  
  18. /// I HATE MY BRAIN
  19.  
  20. const int N = 2e5 + 1;
  21. int n, q;
  22. int have(0);
  23.  
  24. struct Trie{
  25.     Trie *child[2];
  26.     int v = 0;
  27.     Trie() {child[0] = child[1] = NULL;}
  28.     void upd(int id, int lim, int &x, int val){
  29.         if(id == lim){
  30.             v += val;
  31.             return;
  32.         }
  33.         if(!child[(x >> id) & 1])
  34.             child[(x >> id) & 1] = new Trie;
  35.         child[(x >> id) & 1] -> upd(id - 1, lim, x, val);
  36.         if(child[(x >> id) & 1] -> v == 0)
  37.             delete child[(x >> id) & 1],
  38.             child[(x >> id) & 1] = NULL;
  39.         v += val;
  40.     }
  41.     int found(int id, int lim, int &k, int now){
  42.         if(id == lim)
  43.             return now;
  44.         if(child[0] && child[0] -> v + have >= k)
  45.             return child[0] -> found(id - 1, lim, k, now);
  46.         if(child[0])
  47.             have += child[0] ->v;
  48.         now |= 1 << id;
  49.         return child[1] -> found(id - 1, lim, k, now);
  50.     }
  51. };
  52. Trie beg;
  53.  
  54. void Read(){
  55.     cin >> n;
  56.     for(int i = 1; i <= n; ++i){
  57.         int v;
  58.         cin >> v;
  59.         beg.upd(30, -1, v, 1);
  60.     }
  61. }
  62.  
  63. void Solve(){
  64.     cin >> q;
  65.     while(q--){
  66.         int type;
  67.         cin >> type;
  68.         if(type == 1){
  69.             int k;
  70.             cin >> k;
  71.             if(k > n){
  72.                 cout << -1 << "\n";
  73.                 continue;
  74.             }
  75.             have = 0;
  76.             cout << beg.found(30, -1, k, 0) << "\n";
  77.         }
  78.         else if(type == 2){
  79.             int k;
  80.             cin >> k;
  81.             if(k > n)
  82.                 continue;
  83.             have = 0;
  84.             int v = beg.found(30, -1, k, 0);
  85.             beg.upd(30, -1, v, -1);
  86.             --n;
  87.         }
  88.         else{
  89.             int k;
  90.             cin >> k;
  91.             beg.upd(30, -1, k, 1);
  92.             ++n;
  93.         }
  94.     }
  95. }
  96.  
  97. main(){
  98.     fastIOfileinput();
  99.     if(typetest){
  100.         int t;
  101.         cin >> t;
  102.         while(t--){
  103.             Read();
  104.             Solve();
  105.         }
  106.     }
  107.     else{
  108.         Read();
  109.         Solve();
  110.     }
  111. }
Advertisement
Add Comment
Please, Sign In to add comment