Advertisement
i_love_rao_khushboo

D. Vasiliy's Multiset

Oct 19th, 2021
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.71 KB | None | 0 0
  1. // Problem: C. Vasiliy's Multiset
  2. // Contest: Codeforces - Training Contest #033
  3. // URL: https://codeforces.com/gym/349701/problem/C
  4. // Memory Limit: 256 MB
  5. // Time Limit: 4000 ms
  6. // Parsed on: 18-10-2021 15:19:17 IST (UTC+05:30)
  7. // Author: Kapil Choudhary
  8. // ********************************************************************
  9. // कर्मण्येवाधिकारस्ते मा फलेषु कदाचन |
  10. // मा कर्मफलहेतुर्भूर्मा ते सङ्गोऽस्त्वकर्मणि || १.४७ ||
  11. // ********************************************************************
  12.  
  13. #include<bits/stdc++.h>
  14. using namespace std;
  15.  
  16. #define ll long long
  17. #define ld long double
  18. #define ull unsigned long long
  19. #define pb push_back
  20. #define ppb pop_back
  21. #define pf push_front
  22. #define ppf pop_front
  23. #define mp make_pair
  24. #define F first
  25. #define S second
  26. #define PI 3.1415926535897932384626
  27. #define sz(x) ((int)(x).size())
  28. #define vset(v, n, val) v.clear(); v.resize(n, val)
  29.  
  30. typedef pair<int, int> pii;
  31. typedef pair<ll, ll> pll;
  32. typedef vector<int> vi;
  33. typedef vector<ll> vll;
  34. typedef vector<ull> vull;
  35. typedef vector<bool> vb;
  36. typedef vector<char> vc;
  37. typedef vector<string> vs;
  38. typedef vector<pii> vpii;
  39. typedef vector<pll> vpll;
  40. typedef vector<vi> vvi;
  41. typedef vector<vll> vvll;
  42. typedef vector<vull> vvull;
  43. typedef vector<vb> vvb;
  44. typedef vector<vc> vvc;
  45. typedef vector<vs> vvs;
  46.  
  47. /************************************************** DEBUGGER *******************************************************************************************************/
  48.  
  49. #ifndef ONLINE_JUDGE
  50. #define debug(x) cerr << #x <<" "; _print(x); cerr << endl;
  51. #else
  52. #define debug(x)
  53. #endif
  54.  
  55. void _print(ll t) { cerr << t; }
  56. void _print(int t) { cerr << t; }
  57. void _print(string t) { cerr << t; }
  58. void _print(char t) { cerr << t; }
  59. void _print(ld t) { cerr << t; }
  60. void _print(double t) { cerr << t; }
  61. void _print(ull t) { cerr << t; }
  62.  
  63. template <class T, class V> void _print(pair <T, V> p);
  64. template <class T> void _print(vector <T> v);
  65. template <class T> void _print(vector <vector<T>> v);
  66. template <class T> void _print(set <T> v);
  67. template <class T, class V> void _print(map <T, V> v);
  68. template <class T> void _print(multiset <T> v);
  69. template <class T, class V> void _print(multimap <T, V> v);
  70. template <class T> void _print(queue <T> v);
  71. template <class T> void _print(priority_queue <T> v);
  72. template <class T> void _print(stack <T> s);
  73.  
  74. // modify it's definition below as per need such as it can be used for STL containers with custom args passed
  75. template <class T> void _print(T v);
  76.  
  77. template <class T, class V> void _print(pair <T, V> p) { cerr << "{"; _print(p.F); cerr << ","; _print(p.S); cerr << "}"; }
  78. template <class T> void _print(vector <T> v) { cerr << "[ "; for (T i : v) {_print(i); cerr << " "; } cerr << "]"; }
  79. template <class T> void _print(vector <vector<T>> v) { cerr << "==>" << endl; for (vector<T> vec : v) { for(T i : vec) {_print(i); cerr << " "; } cerr << endl; } }
  80. template <class T> void _print(set <T> v) { cerr << "[ "; for (T i : v) {_print(i); cerr << " "; } cerr << "]"; }
  81. template <class T, class V> void _print(map <T, V> v) { cerr << "[ "; for (auto i : v) {_print(i); cerr << " "; } cerr << "]"; }
  82. template <class T> void _print(multiset <T> v) { cerr << "[ "; for (T i : v) {_print(i); cerr << " "; } cerr << "]"; }
  83. template <class T, class V> void _print(multimap <T, V> v) { cerr << "[ "; for (auto i : v) {_print(i); cerr << " "; } cerr << "]"; }
  84. template <class T> void _print(queue <T> v) { cerr << "[ "; while(!v.empty()) {_print(v.front()); v.pop(); cerr << " "; } cerr << "]"; }
  85. template <class T> void _print(priority_queue <T> v) { cerr << "[ "; while(!v.empty()) {_print(v.top()); v.pop(); cerr << " "; } cerr << "]"; }
  86. template <class T> void _print(stack <T> v) { cerr << "[ "; while(!v.empty()) {_print(v.top()); v.pop(); cerr << " "; } cerr << "]"; }
  87. template <class T> void _print(T v) {  }
  88.  
  89. /*******************************************************************************************************************************************************************/
  90.  
  91. mt19937_64 rang(chrono::high_resolution_clock::now().time_since_epoch().count());
  92. int rng(int lim) {
  93.     uniform_int_distribution<int> uid(0,lim-1);
  94.     return uid(rang);
  95. }
  96.  
  97. const int INF = 0x3f3f3f3f;
  98. const int mod = 1e9+7;
  99.  
  100. ll mod_exp(ll a, ll b) { a %= mod; if(a == 0) return 0LL; ll res = 1LL;
  101.                          while(b > 0) { if(b & 1) res = (res * a) % mod; a = (a * a) % mod; b >>= 1; } return res; }
  102.                          
  103. ll mod_inv(ll a) { return mod_exp(a, mod - 2); } // works only for prime value of "mod"
  104. ll GCD(ll a, ll b) { return (b == 0) ? a : GCD(b, a % b); }
  105.  
  106. /******************************************************************************************************************************/
  107.  
  108. struct Trie {
  109.     // lf represents 0, rg represents 1
  110.     Trie *lf, *rg;
  111.    
  112.     Trie() {
  113.         lf = rg = NULL;
  114.     }
  115. };
  116.  
  117. // assuming numbers to be 64 bit integers
  118. void trie_insert(Trie *root, ll val) {
  119.     Trie *cur = root;
  120.    
  121.     for(ll i = 63; i >= 0; i--) {
  122.         ll bit = (val >> i) & 1;
  123.        
  124.         if(bit == 0) {
  125.             if(cur->lf == NULL) cur->lf = new Trie();
  126.             cur = cur->lf;
  127.         }
  128.        
  129.         else {
  130.             if(cur->rg == NULL) cur->rg = new Trie();
  131.             cur = cur->rg;
  132.         }
  133.     }
  134. }
  135.  
  136. ll max_xor_util(Trie *root, ll val) {
  137.     Trie *cur = root;
  138.  
  139.     // to store the max XOR which number 'val' can form with any
  140.     // number present in trie
  141.     ll ans = 0LL;
  142.    
  143.     for(ll i = 63; i >= 0; i--) {
  144.         // finding whether the ith bit is set or not
  145.         ll bit = (val >> i) & 1;
  146.        
  147.         if(bit == 0) {
  148.             // check for complementary of 0 i.e. 1 as it will increment our answer
  149.             if(cur->rg) {
  150.                 // include it in ans
  151.                 ans += (1LL << i);
  152.                 cur = cur->rg;
  153.             }
  154.            
  155.             else cur = cur->lf;
  156.         }
  157.        
  158.         else {
  159.             // check for complementary of 1 i.e. 0 as it will increment our answer
  160.             if(cur->lf) {
  161.                 // include it in ans
  162.                 ans += (1LL << i);
  163.                 cur = cur->lf;
  164.             }
  165.            
  166.             else cur = cur->rg;
  167.         }
  168.     }
  169.    
  170.     return ans;
  171. }
  172.  
  173. // when calling this function pass cur as root & dep = 0;
  174. Trie* trie_delete(Trie* cur, vll &v, ll dep) {
  175.     // base case
  176.     if(cur == NULL) return cur;
  177.    
  178.     if(dep == 64) {
  179.         delete (cur);
  180.         cur = NULL;
  181.         return cur;
  182.     }
  183.        
  184.     if(v[dep] == 0) cur->lf = trie_delete(cur->lf, v, dep + 1);
  185.     else cur->rg = trie_delete(cur->rg, v, dep + 1);
  186.        
  187.     if(cur->lf == NULL and cur->rg == NULL) {
  188.         delete (cur);
  189.         cur = NULL;
  190.     }
  191.        
  192.     return cur;
  193. }
  194.  
  195. Trie* trie_init() {
  196.     Trie *root = new Trie();
  197.     return root;
  198. }
  199.  
  200. void solve()
  201. {
  202.     Trie *root = trie_init();
  203.     trie_insert(root, 0LL);
  204.    
  205.     map<ll, ll> cnt;
  206.    
  207.     int q; cin >> q;
  208.     while(q--) {
  209.         char tp; ll x; cin >> tp >> x;
  210.        
  211.         if(tp == '+') {
  212.             if(cnt.find(x) == cnt.end()) trie_insert(root, x);
  213.             cnt[x] += 1;
  214.         }
  215.        
  216.         else if(tp == '-') {
  217.             cnt[x] -= 1;
  218.             if(cnt[x] == 0) {
  219.                 cnt.erase(x);
  220.                
  221.                 vll v;
  222.                 for(ll i = 63; i >= 0; i--) {
  223.                     v.pb((x >> i) & 1);
  224.                 }
  225.                
  226.                 trie_delete(root, v, 0);
  227.             }
  228.         }
  229.        
  230.         else {
  231.             cout << max_xor_util(root, x) << "\n";
  232.         }
  233.     }
  234. }
  235.  
  236. int main()
  237. {
  238.     ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
  239.     srand(chrono::high_resolution_clock::now().time_since_epoch().count());
  240.  
  241.     // #ifndef ONLINE_JUDGE
  242.     //     freopen("input.txt", "r", stdin);
  243.     //     freopen("output.txt", "w", stdout);
  244.     // #endif
  245.    
  246.     // #ifndef ONLINE_JUDGE
  247.     //      freopen("error.txt", "w", stderr);
  248.     // #endif
  249.    
  250.     int t = 1;
  251.     // int test = 1;
  252.     // cin >> t;
  253.     while(t--) {
  254.         // cout << "Case #" << test++ << ": ";
  255.         solve();
  256.     }
  257.  
  258.     return 0;
  259. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement