Advertisement
hung_mine

ORDSET

Oct 11th, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.62 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. /// from HUNG MINE with love <3
  4. int t[1000001][3], cc = 1, fin[1000001], par[1000001], cnt[1000001], q, x;
  5. const int add = 1e9;
  6. void ins (int x) {
  7.       int r = 1;
  8.       for (int k = 30; k >= 0; -- k) {
  9.             int i = ((x >> k) & 1);
  10.             if (t[r][i] == 0) {
  11.                   ++ cc;
  12.                   t[r][i] = cc;
  13.                   par[cc] = r;
  14.             }
  15.             r = t[r][i];
  16.       }
  17.       if (fin[r] == 1) return;
  18.       fin[r] = 1;
  19.       ++ cnt[r];
  20.       while (r != 1) {
  21.             r = par[r];
  22.             ++ cnt[r];
  23.       }
  24. }
  25. void del (int x) {
  26.       int r = 1;
  27.       for (int k = 30; k >= 0; -- k) {
  28.             int i = (x >> k) & 1;
  29.             if (t[r][i] == 0) return;
  30.             r = t[r][i];
  31.       }
  32.       if (fin[r] == 0) return;
  33.       fin[r] = 0;
  34.       -- cnt[r];
  35.       while (r != 1) {
  36.             r = par[r];
  37.             -- cnt[r];
  38.       }
  39. }
  40. int pth (int p) {
  41.       int r = 1;
  42.       if (cnt[r] < p) return -1e9;
  43.       int x = 0;
  44.       for (int k = 30; k >= 0; -- k) {
  45.             int i = t[r][0];
  46.             if (p > cnt[i]) {
  47.                   x |= (1 << k);
  48.                   p -= cnt[i];
  49.                   r = t[r][1];
  50.             }
  51.             else {
  52.                   r = t[r][0];
  53.             }
  54.       }
  55.       return x;
  56. }
  57. int cou (int x) {
  58.       int r = 1;
  59.       int res = 0;
  60.       for (int k = 30; k >= 0; -- k) {
  61.             int i = ((x >> k) & 1);
  62.             if (i == 1) res += cnt[t[r][0]];
  63.             if (t[r][i] == 0) return res;
  64.             r = t[r][i];
  65.       }
  66.       return res;
  67. }
  68. int main () {
  69.       if (fopen ("test.inp", "r")) {
  70.             freopen ("test.inp", "r", stdin);
  71.       }
  72. //      else {
  73. //            freopen ("ORDSET.inp", "r", stdin);
  74. //            freopen ("ORDSET.out", "w", stdout);
  75. //      }
  76.  
  77.       ios_base :: sync_with_stdio (0);
  78.       cin.tie (0);
  79.       cout.tie (0);
  80.       cin >> q;
  81.       while (q --) {
  82.             char c;
  83.             cin >> c;
  84.             if (c == 'I') {
  85.                   cin >> x;
  86.                   ins (x + add);
  87.             }
  88.             else if (c == 'D') {
  89.                   cin >> x;
  90.                   del (x + add);
  91.             }
  92.             else if (c == 'K') {
  93.                   cin >> x;
  94.                   if (pth (x) == -1e9) {
  95.                         cout << "invalid\n";
  96.                   }
  97.                   else {
  98.                         cout << pth (x) - add<< "\n";
  99.                   }
  100.             }
  101.             else {
  102.                   cin >> x;
  103.                   cout << cou (x + add) << "\n";
  104.             }
  105.       }
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement