Advertisement
slash0t

binary bor

Apr 17th, 2025
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.74 KB | None | 0 0
  1. struct bor {
  2.     const static int al = 2;
  3.     const static int a = 0;
  4.  
  5.     struct node {
  6.         node* to[al];
  7.         int visited;
  8.  
  9.         node() {
  10.             memset(to, 0, sizeof(to));
  11.             visited = 0;
  12.         }
  13.  
  14.         node* go(int goal) {
  15.             if (this->to[goal] == nullptr) {
  16.                 this->to[goal] = new node();
  17.             }
  18.             return this->to[goal];
  19.         }
  20.     };
  21.  
  22.     node* root;
  23.  
  24.     bor() {
  25.         root = new node();
  26.     }
  27.  
  28.     node* add(int num, int index) {
  29.         node* now = root;
  30.         for (int i = sz - 1; i >= 0; i--) {
  31.             int bit = (num >> i) & 1;
  32.             now = now->go(bit);
  33.             now->visited = index;
  34.         }
  35.         return now;
  36.     }
  37. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement