Advertisement
Guest User

Untitled

a guest
May 21st, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.25 KB | None | 0 0
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <random>
  4. #include <time.h>
  5.  
  6. using namespace std;
  7.  
  8. inline int gcd(int a, int b) {
  9.     while (b != 0) {
  10.         a %= b;
  11.         swap(a, b);
  12.     }
  13.     return a;
  14. }
  15.  
  16. inline int superRand() {
  17.     return (rand() << 15) + rand();
  18. }
  19.  
  20. struct Node {
  21.     int key, prior;
  22.     int g, sz;
  23.     Node * left, *right;
  24.     Node() : left(nullptr), right(nullptr), g(1), sz(0) {};
  25.     Node(int key1) : key(key1), prior(superRand()), left(nullptr), right(nullptr), g(key1), sz(1) {};
  26. };
  27.  
  28. Node * root = nullptr;
  29.  
  30. inline int get_g(Node * &root) {
  31.     if (!root) {
  32.         return 0;
  33.     }
  34.     return root->g;
  35. }
  36. inline int get_sz(Node * &root) {
  37.     if (!root) {
  38.         return 0;
  39.     }
  40.     return root->sz;
  41. }
  42.  
  43. inline void update(Node * &root) {
  44.     if (!root)
  45.         return;
  46.     root->g = gcd(root->key, gcd(get_g(root->left), get_g(root->right)));
  47.     root->sz = get_sz(root->left) + get_sz(root->right) + 1;
  48. }
  49.  
  50. void split(Node * root, Node*&left, Node *&right, int x) {
  51.     if (!root) {
  52.         left = nullptr;
  53.         right = nullptr;
  54.         return;
  55.     }
  56.     if (root->key < x) {
  57.         split(root->right, root->right, right, x);
  58.         left = root;
  59.         update(left);
  60.         update(root);
  61.         return;
  62.     }
  63.     split(root->left, left, root->left, x);
  64.     right = root;
  65.     update(right);
  66.     update(root);
  67. }
  68.  
  69. void mmerge(Node * &root, Node * left, Node * right) {
  70.     if (!left) {
  71.         root = right;
  72.         return;
  73.     }
  74.     if (!right) {
  75.         root = left;
  76.         return;
  77.     }
  78.     if (left->prior > right->prior) {
  79.         mmerge(left->right, left->right, right);
  80.         root = left;
  81.     }
  82.     else {
  83.         mmerge(right->left, left, right->left);
  84.         root = right;
  85.     }
  86.     update(root);
  87. }
  88.  
  89. void minsert(Node * &root, Node * x) {
  90.     if (!root) {
  91.         root = x;
  92.         return;
  93.     }
  94.     if (x->prior > root->prior) {
  95.         split(root, x->left, x->right, x->key);
  96.         root = x;
  97.     }
  98.     else
  99.         minsert(root->key > x->key ? root->left : root->right, x);
  100.     update(root);
  101. }
  102.  
  103. void merase(Node * &root, int x) {
  104.     if (!root) {
  105.         return;
  106.     }
  107.     if (root->key == x) {
  108.         mmerge(root, root->left, root->right);
  109.     }
  110.     else
  111.         merase(root->key > x ? root->left : root->right, x);
  112.     update(root);
  113. }
  114.  
  115. bool mfind(Node * root, int x) {
  116.     if (!root)
  117.         return false;
  118.     if (root->key == x)
  119.         return true;
  120.     mfind(root->key > x ? root->left : root->right, x);
  121. }
  122.  
  123. void Print(Node * root) {
  124.     if (!root)
  125.         return;
  126.     Print(root->left);
  127.     cout << root->key << " ";
  128.     Print(root->right);
  129. }
  130.  
  131. void print(Node * root) {
  132.     Print(root);
  133.     cout << endl;
  134. }
  135.  
  136.  
  137. int main()
  138. {
  139.     ios_base::sync_with_stdio(0);
  140.     cin.tie(0);
  141.     int q;
  142.     cin >> q;
  143.     for (int i = 0; i<q; ++i) {
  144.         char type;
  145.         int x;
  146.         cin >> type >> x;
  147.         if (type == '+') {
  148.             minsert(root, new Node(x));
  149.             cout << get_g(root) << endl;
  150.         }
  151.         else {
  152.             merase(root, x);
  153.             if (get_sz(root) == 0)
  154.                 cout << 1 << endl;
  155.             else
  156.                 cout << get_g(root) << endl;
  157.         }
  158.     }
  159.     return 0;
  160. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement