leoanjos

Splay Tree

Apr 12th, 2023
982
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.66 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. #define llong long long int
  6.  
  7. template<typename T>
  8. struct RandomNumberGenerator {
  9. private:
  10.     int cnt;
  11.     T S, A, C, last;
  12.  
  13. public:
  14.     RandomNumberGenerator(T S, T A, T C): S(S), A(A), C(C), cnt(0) {}
  15.  
  16.     T next() {
  17.         if (cnt++ == 0) return last = S;
  18.         return last = A * last + C;
  19.     }
  20. };
  21.  
  22. template<typename T>
  23. struct SplayTree {
  24. private:
  25.     struct Node {
  26.         T v;
  27.         Node *l, *r, *p;
  28.  
  29.         Node(T v, Node *l = NULL, Node *r = NULL, Node *p = NULL): v(v), l(l), r(r), p(p) {}
  30.     };
  31.  
  32.     Node *root;
  33.  
  34. public:
  35.     SplayTree(): root(NULL) {}
  36.  
  37.     int insert(T v) {
  38.         int depth = 0;
  39.         root = insert(root, v, depth);
  40.         return depth;
  41.     }
  42.  
  43.     int query(T v) {
  44.         int depth = 0;
  45.         root = splay(query(root, v, depth));
  46.         return depth;
  47.     }
  48.  
  49. private:
  50.     Node* insert(Node *node, T v, int &depth) {
  51.         if (node == NULL) return new Node(v);
  52.         if (node->v == v) {
  53.             depth = -1;
  54.             return node;
  55.         }
  56.  
  57.         depth++;
  58.         if (v < node->v) {
  59.             node->l = insert(node->l, v, depth);
  60.             node->l->p = node;
  61.         } else {
  62.             node->r = insert(node->r, v, depth);
  63.             node->r->p = node;
  64.         }
  65.  
  66.         return node;
  67.     }
  68.  
  69.     Node *query(Node *node, T v, int &depth) {
  70.         if (node == NULL) {
  71.             depth = -1;
  72.             return NULL;
  73.         }
  74.  
  75.         if (node->v == v) return node;
  76.  
  77.         depth++;
  78.         if (v < node->v) return query(node->l, v, depth);
  79.         return query(node->r, v, depth);
  80.     }
  81.  
  82.     void rotate_left(Node *node) {
  83.         node->p->r = node->l;
  84.         if (node->l != NULL)
  85.             node->l->p = node->p;
  86.  
  87.         node->l = node->p;
  88.         if (node->p->p != NULL) {
  89.             if (node->p->p->l == node->p) node->p->p->l = node;
  90.             else node->p->p->r = node;
  91.         }
  92.  
  93.         node->p = node->p->p;
  94.         node->l->p = node;
  95.     }
  96.  
  97.     void rotate_right(Node *node) {
  98.         node->p->l = node->r;
  99.         if (node->r != NULL)
  100.             node->r->p = node->p;
  101.  
  102.         node->r = node->p;
  103.         if (node->p->p != NULL) {
  104.             if (node->p->p->l == node->p) node->p->p->l = node;
  105.             else node->p->p->r = node;
  106.         }
  107.  
  108.         node->p = node->p->p;
  109.         node->r->p = node;
  110.     }
  111.  
  112.     void zig(Node *node) {
  113.         if (node->p->l == node) rotate_right(node);
  114.         else rotate_left(node);
  115.     }
  116.  
  117.     Node* splay(Node *node) {
  118.         if (node == NULL) return root;
  119.  
  120.         while (node->p != NULL && node->p->p != NULL) {
  121.             if ((node->p->l == node && node->p->p->l == node->p) || (node->p->r == node && node->p->p->r == node->p)) {
  122.                 zig(node->p);
  123.                 zig(node);
  124.             } else {
  125.                 zig(node);
  126.                 zig(node);
  127.             }
  128.         }
  129.  
  130.         while (node->p != NULL)
  131.             zig(node);
  132.  
  133.         return node;
  134.     }
  135. };
  136.  
  137. int main() {
  138.     ios_base::sync_with_stdio(false);
  139.     cin.tie(NULL);
  140.  
  141.     const uint32_t A = 1664525;
  142.     const uint32_t C = 1013904223;
  143.  
  144.     uint32_t S;
  145.     int U, B, N, I, Q, P;
  146.     cin >> S >> U >> B >> N >> I >> Q >> P;
  147.  
  148.     RandomNumberGenerator<uint32_t> rng(S, A, C);
  149.  
  150.     SplayTree<uint32_t> T;
  151.     while (B--)
  152.         T.insert(rng.next() % U);
  153.  
  154.     for (int i = 0; i < N; i++) {
  155.         uint32_t X = rng.next();
  156.         uint32_t K = rng.next() % U;
  157.  
  158.         bool ins = X % (I + Q) < I;
  159.         int D = ins ? T.insert(K) : T.query(K);
  160.         if (i % P == 0)
  161.             cout << (ins ? "I" : "Q") << " " << K << " " << D << "\n";
  162.     }
  163. }
Advertisement
Add Comment
Please, Sign In to add comment