Guest User

van Emde Boas tree

a guest
Jul 2nd, 2014
520
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.16 KB | None | 0 0
  1. #include <cstdio>
  2. #include <ctime>
  3.  
  4. #include <iostream>
  5. #include <vector>
  6. #include <set>
  7.  
  8. using namespace std;
  9.  
  10. const int Infinity = 1000000000;
  11.  
  12. class EmdeBoasTree
  13. {
  14.     private:
  15.         int keySize, min, max;
  16.         EmdeBoasTree **ch, *aux;
  17.  
  18.     public:
  19.         EmdeBoasTree(int nSize)
  20.         {
  21.             keySize = nSize;
  22.             min = Infinity, max = -Infinity;
  23.             ch = 0;
  24.             if (keySize != 1)
  25.             {
  26.                 ch = new EmdeBoasTree* [1 << (keySize >> 1)];
  27.                 fill(ch, ch + (1 << (keySize >> 1)), nullptr);
  28.             }
  29.             aux = 0;
  30.         }
  31.         ~EmdeBoasTree()
  32.         {
  33.             delete aux;
  34.             for (size_t i = 0; i < (1 << (keySize >> 1)); i++)
  35.                 if (ch[i]) delete ch[i];
  36.             delete ch;
  37.         }
  38.  
  39.         bool empty()
  40.         {
  41.             return min > max;
  42.         }
  43.  
  44.         void insert(int x)
  45.         {
  46.             if (empty()) return void(min = max = x);
  47.             if (min == max)
  48.             {
  49.                 if (x < min) min = x;
  50.                 if (x > max) max = x;
  51.                 return;
  52.             }
  53.             if (x < min) swap(x, min);
  54.             if (x > max) swap(x, max);
  55.             int hi = x >> keySize / 2, lo = x - (hi << keySize / 2);
  56.             if (!ch[hi]) ch[hi] = new EmdeBoasTree(keySize / 2);
  57.             ch[hi]->insert(lo);
  58.             if (ch[hi]->min == ch[hi]->max)
  59.             {
  60.                 if (!aux) aux = new EmdeBoasTree(keySize / 2);
  61.                 aux->insert(hi);
  62.             }
  63.         }
  64.  
  65.         void erase(int x)
  66.         {
  67.             if (empty()) return;
  68.             if (min == max && min == x)
  69.             {
  70.                 min = Infinity, max = -Infinity;
  71.                 return;
  72.             }
  73.             if (min == x)
  74.             {
  75.                 if (!aux || aux->empty())
  76.                     return void(min = max);
  77.                 else
  78.                     x = ch[aux->min]->min, min = x;
  79.             }
  80.             if (max == x)
  81.             {
  82.                 if (!aux || aux->empty())
  83.                     return void(max = min);
  84.                 else
  85.                     x = ch[aux->max]->max, max = x;
  86.             }
  87.             if (!aux || aux->empty()) return;
  88.             int hi = x >> keySize / 2, lo = x - (hi << keySize / 2);
  89.             if (!ch[hi]) ch[hi] = new EmdeBoasTree(keySize / 2);
  90.             ch[hi]->erase(lo);
  91.             if (ch[hi]->empty()) aux->erase(hi);
  92.         }
  93.  
  94.         bool contain(int x)
  95.         {
  96.             if (empty()) return false;
  97.             if (x == min || x == max) return true;
  98.             int hi = x >> keySize / 2, lo = x - (hi << keySize / 2);
  99.             if (!ch[hi]) return false;
  100.             return ch[hi]->contain(lo);
  101.         }
  102. };
  103.  
  104. int main()
  105. {
  106.     freopen("emdeboas.in", "r", stdin);
  107. //     freopen("emdeboas.out", "w", stdout);
  108.     int m = 20000000;
  109. #ifdef EMDE_BOAS
  110.     EmdeBoasTree *t = new EmdeBoasTree(32);
  111.     for (int i = 0; i < m;
  112.     t->insert(i ^ 981838913);
  113. #else
  114.     set<int> s;
  115.     for (int i = 0; i < m; i++)
  116.         s.insert(i ^ 981838913);
  117. #endif
  118.     cout << "Time: " << clock() / 1000 << " ms." << endl;
  119.     return 0;
  120. }
Advertisement
Add Comment
Please, Sign In to add comment