Advertisement
Guest User

Untitled

a guest
Jun 26th, 2014
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.21 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <math.h>
  3. #include <string.h>
  4. #include <stdlib.h>
  5. #include <assert.h>
  6. #include <algorithm>
  7. #include <vector>
  8. #include <stack>
  9. #include <queue>
  10. #include <list>
  11. #include <set>
  12. #include <map>
  13. #include <string>
  14. #include <iostream>
  15. using namespace std;
  16.  
  17. int gcd(int a, int b) {
  18.     return b ? gcd(b, a % b) : a;
  19. }
  20.  
  21. struct TN {
  22.     int k, p, d;
  23.     TN *l, *r;
  24.     TN(int K) : k(K), p(rand()), d(K), l(0), r(0) {}
  25. };
  26.  
  27. class Treap {
  28.     TN *t;
  29.     int getD(TN *n) {
  30.         return n ? n->d : 0;
  31.     }
  32.     void update(TN *n) {
  33.         if (n)
  34.             n->d = gcd(n->k, gcd(getD(n->l), getD(n->r)));
  35.     }
  36.     TN *merge(TN *a, TN *b) {
  37.         if (!a || !b)
  38.             return a ? a : b;
  39.         if (a->p > b->p) {
  40.             a->r = merge(a->r, b);
  41.             update(a);
  42.             return a;
  43.         } else {
  44.             b->l = merge(a, b->l);
  45.             update(b);
  46.             return b;
  47.         }
  48.     }
  49.     void split(TN *t, int k, TN *&a, TN *&b) {
  50.         if (!t) {
  51.             a = b = 0;
  52.             return;
  53.         }
  54.         if (t->k > k) {
  55.             split(t->l, k, a, t->l);
  56.             b = t;
  57.         } else {
  58.             split(t->r, k, t->r, b);
  59.             a = t;
  60.         }
  61.         update(a);
  62.         update(b);
  63.     }
  64.     void rAdd(TN *&n, TN *nn) {
  65.         if (!n) {
  66.             n = nn;
  67.             return;
  68.         }
  69.         if (n->p < nn->p) {
  70.             split(n, nn->k, nn->l, nn->r);
  71.             n = nn;
  72.         } else {
  73.             rAdd(nn->k < n->k ? n->l : n->r, nn);
  74.         }
  75.         update(n);
  76.     }
  77.     void rDel(TN *&n, int x) {
  78.         if (n->k == x)
  79.             n = merge(n->l, n->r);
  80.         else
  81.             rDel(x < n->k ? n->l : n->r, x);
  82.         update(n);
  83.     }
  84. public:
  85.     Treap() : t(0) {}
  86.     void add(int x) {
  87.         rAdd(t, new TN(x));
  88.     }
  89.     void del(int x) {
  90.         rDel(t, x);
  91.     }
  92.     int getGcd() {
  93.         return t ? t->d : 1;
  94.     }
  95. } t;
  96.  
  97. int n, x;
  98. char c;
  99.  
  100. int main() {
  101.     scanf("%d", &n);
  102.     for (int i = 0; i < n; i++) {
  103.         scanf(" %c%d", &c, &x);
  104.         if (c == '+')
  105.             t.add(x);
  106.         else
  107.             t.del(x);
  108.         printf("%d\n", t.getGcd());
  109.     }
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement