Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <iostream>
- #include <vector>
- #include <cmath>
- #include <algorithm>
- #include <memory.h>
- #include <map>
- #include <set>
- #include <queue>
- #include <list>
- #include <sstream>
- #include <cstring>
- #include <cassert>
- #include <climits>
- using namespace std;
- const int N = 350;
- const int blocksize = 400;
- const int SZ = 5e7;
- char buf[SZ];
- int pos;
- void * operator new (size_t x){
- pos += x;
- if (pos > SZ)
- throw 42;
- return buf + pos - x;
- }
- void operator delete(void *x) { }
- inline int gcd(int a, int b) {
- while (b) {
- a %= b;
- swap(a, b);
- }
- return a;
- }
- map<int, vector<int>> ind;
- int block[N];
- multiset<int> inblock[N];
- int id = 0;
- int maxblock = 0;
- int query() {
- int res = -1;
- for (int i = 0; i <= maxblock; i++) {
- if (block[i] == -1) continue;
- res = res == -1 ? block[i] : gcd(res, block[i]);
- }
- return res;
- }
- void add(int x) {
- while (inblock[id].size() >= blocksize) {
- id++;
- maxblock = max(maxblock, id);
- }
- inblock[id].insert(x);
- int cur = -1;
- for (auto v : inblock[id]) {
- cur = cur == -1 ? v : gcd(cur, v);
- }
- ind[x].push_back(id);
- block[id] = cur;
- }
- void del(int x) {
- int index = ind[x][ind[x].size() - 1];
- ind[x].pop_back();
- inblock[index].erase(inblock[index].find(x));
- int cur = -1;
- for (auto v : inblock[index]) {
- cur = cur != -1 ? gcd(cur, v) : v;
- }
- block[index] = cur;
- id = index;
- }
- char c;
- int main() {
- //freopen("input.txt", "r", stdin);
- //freopen("output.txt", "w", stdout);
- int n;
- scanf("%d", &n);
- for (int i = 0; i < N; i++)
- block[i] = -1;
- for (int i = 0; i < n; i++) {
- int x;
- scanf("%s%d", &c, &x);
- if (c == '+') {
- add(x);
- }
- else {
- del(x);
- }
- int ans = query();
- printf("%d\n", ans != -1 ? ans : 1);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement