Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <math.h>
- #include <string.h>
- #include <stdlib.h>
- #include <assert.h>
- #include <algorithm>
- #include <vector>
- #include <stack>
- #include <queue>
- #include <list>
- #include <set>
- #include <map>
- #include <string>
- #include <iostream>
- using namespace std;
- int gcd(int a, int b) {
- return b ? gcd(b, a % b) : a;
- }
- struct TN {
- int k, p, d;
- TN *l, *r;
- TN(int K) : k(K), p(rand()), d(K), l(0), r(0) {}
- };
- class Treap {
- TN *t;
- int getD(TN *n) {
- return n ? n->d : 0;
- }
- void update(TN *n) {
- if (n)
- n->d = gcd(n->k, gcd(getD(n->l), getD(n->r)));
- }
- TN *merge(TN *a, TN *b) {
- if (!a || !b)
- return a ? a : b;
- if (a->p > b->p) {
- a->r = merge(a->r, b);
- update(a);
- return a;
- } else {
- b->l = merge(a, b->l);
- update(b);
- return b;
- }
- }
- void split(TN *t, int k, TN *&a, TN *&b) {
- if (!t) {
- a = b = 0;
- return;
- }
- if (t->k > k) {
- split(t->l, k, a, t->l);
- b = t;
- } else {
- split(t->r, k, t->r, b);
- a = t;
- }
- update(a);
- update(b);
- }
- void rAdd(TN *&n, TN *nn) {
- if (!n) {
- n = nn;
- return;
- }
- if (n->p < nn->p) {
- split(n, nn->k, nn->l, nn->r);
- n = nn;
- } else {
- rAdd(nn->k < n->k ? n->l : n->r, nn);
- }
- update(n);
- }
- void rDel(TN *&n, int x) {
- if (n->k == x)
- n = merge(n->l, n->r);
- else
- rDel(x < n->k ? n->l : n->r, x);
- update(n);
- }
- public:
- Treap() : t(0) {}
- void add(int x) {
- rAdd(t, new TN(x));
- }
- void del(int x) {
- rDel(t, x);
- }
- int getGcd() {
- return t ? t->d : 1;
- }
- } t;
- int n, x;
- char c;
- int main() {
- scanf("%d", &n);
- for (int i = 0; i < n; i++) {
- scanf(" %c%d", &c, &x);
- if (c == '+')
- t.add(x);
- else
- t.del(x);
- printf("%d\n", t.getGcd());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement