Advertisement
MinhNGUYEN2k4

Untitled

Aug 1st, 2020
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.13 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define int long long
  3. #define FOR(g,z,t) for(int g=z; g<=t; ++g)
  4. using namespace std;
  5. const int N = (3*1e5+1)*32;
  6. int n;
  7. int child[N][2],cnt=0,num[N];
  8.  
  9. int Getbit(int val, int pos) {
  10.  return 1 & (val >> (pos-1));
  11. }
  12.  
  13. void ADD(int x) {
  14.     int node =0;
  15.     for(int i=31; i>=1; --i) {
  16.         int k = Getbit(x,i);
  17.         if (!child[node][k]) child[node][k]=++cnt;
  18.         ++num[child[node][k]];
  19.         node=child[node][k];
  20.     }
  21. }
  22.  
  23. int Query(int k) {
  24.     int ans = 0;
  25.     int node = 0;
  26.     for(int i=31; i>=1; --i) {
  27.         if (k <= num[child[node][0]]) node = child[node][0];
  28.         else {
  29.             k -= num[child[node][0]];
  30.             ans += 1 << (i-1);
  31.             node = child[node][1];
  32.         }
  33.     }
  34.     return ans;
  35. }
  36.  
  37. signed main()
  38. {
  39.     ios_base::sync_with_stdio(false);
  40.     cin.tie(0);cout.tie(0);
  41.     freopen("RANDNUM.INP","r",stdin);
  42.     cin >> n;
  43.     while (n--) {
  44.         int type,val;
  45.         cin >> type >> val;
  46.         if (type == 1) {
  47.             ADD(val);
  48.         }
  49.         else  {
  50.             cout << Query(val) << endl;
  51.         }
  52.     }
  53.     return 0;
  54. }
  55.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement