Advertisement
Guest User

Untitled

a guest
Jun 26th, 2014
1,308
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.40 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <ctime>
  4. #include <cstdlib>
  5.  
  6. using namespace std;
  7.  
  8. typedef struct Node* PNode;
  9.  
  10. struct Node {
  11.     int key, prior, g;
  12.  
  13.     PNode l, r;
  14.  
  15.     Node() : key(0), prior(0), g(1), l(NULL), r(NULL) {}
  16.  
  17.     Node(int key) : key(key), prior((rand() << 16) + rand()), g(key), l(NULL), r(NULL) {}
  18. };
  19.  
  20. int gcd(int a, int b) {
  21.     if (!a || !b)
  22.         return a + b;
  23.  
  24.     return gcd(b, a % b);
  25. }
  26.  
  27. int node_gcd(PNode t) {
  28.     return (t ? t -> g : 0);
  29. }
  30.  
  31. void upd_gcd(PNode t) {
  32.     if (t) {
  33.         t -> g = gcd(node_gcd(t -> l), node_gcd(t -> r));
  34.         t -> g = gcd(t -> g, t -> key);
  35.     }
  36. }
  37.  
  38. void output(PNode t) {
  39.     if (!t)
  40.         return;
  41.  
  42.     output(t -> l);
  43.     cout << t -> key << ' ' << t -> g << ' ' << t -> prior << endl;
  44.     output(t -> r);
  45. }
  46.  
  47. void split(PNode t, PNode& l, PNode& r, int key) {
  48.     if (!t) {
  49.         l = r = NULL;
  50.         return;
  51.     }
  52.  
  53.     if (t -> key <= key) {
  54.         split(t -> r, t -> r, r, key);
  55.         l = t;
  56.     } else {
  57.         split(t -> l, l, t -> l, key);
  58.         r = t;
  59.     }
  60.  
  61.     upd_gcd(l);
  62.     upd_gcd(r);
  63. }
  64.  
  65. PNode merge(PNode l, PNode r) {
  66.     if (!l || !r)
  67.         return l ? l : r;
  68.  
  69.     PNode ans;
  70.  
  71.     if (l -> prior > r -> prior) {
  72.         l -> r = merge(l -> r, r);
  73.         ans = l;
  74.     } else {
  75.         r -> l = merge(l, r -> l);
  76.         ans = r;
  77.     }
  78.  
  79.     upd_gcd(l);
  80.     upd_gcd(r);
  81.  
  82.     return ans;
  83. }
  84.  
  85. void erase(PNode &t, int key) {
  86.     if (!t)
  87.         return;
  88.  
  89.     if (t -> key == key) {
  90.         PNode tmp = merge(t -> l, t -> r);
  91.         delete t;
  92.         t = tmp;
  93.         upd_gcd(t);
  94.         return;
  95.     }
  96.  
  97.     if (key < t -> key)
  98.         erase(t -> l, key);
  99.     else
  100.         erase(t -> r, key);
  101.  
  102.     upd_gcd(t);
  103. }
  104.  
  105. void insert(PNode& t, PNode it) {
  106.     if (!t) {
  107.         t = it;
  108.         return;
  109.     }
  110.  
  111.     if (it -> prior > t -> prior) {
  112.         split(t, it -> l, it -> r, it -> key);
  113.         t = it;
  114.         upd_gcd(t);
  115.         return;
  116.     }
  117.  
  118.     if (it -> key < t -> key)
  119.         insert(t -> l, it);
  120.     else
  121.         insert(t -> r, it);
  122.  
  123.     upd_gcd(t);
  124. }
  125.  
  126. PNode root = NULL;
  127. int n, key;
  128. char c;
  129.  
  130. int main() {
  131.     #ifndef ONLINE_JUDGE
  132.         freopen("in", "r", stdin);
  133.     #endif
  134.  
  135.     scanf("%d", &n);
  136.  
  137.     srand(time(NULL));
  138.  
  139.     for (int i = 0; i < n; i++) {
  140.         scanf(" \n%c%d", &c, &key);
  141.         switch (c) {
  142.             case '-':
  143.                 erase(root, key);
  144.                 break;
  145.             case '+':
  146.                 PNode tmp = new Node(key);
  147.                 insert(root, tmp);
  148.                 break;
  149.         }
  150.  
  151.         //output(root);
  152.  
  153.         int ans = node_gcd(root);
  154.  
  155.         printf("%d\n", ans ? ans : 1);
  156.     }
  157.  
  158.     return 0;
  159. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement