Advertisement
Guest User

Untitled

a guest
Jun 26th, 2014
377
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.05 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <vector>
  4. #include <set>
  5. #include <map>
  6. #include <cmath>
  7. #include <sstream>
  8.  
  9. using namespace std;
  10.  
  11. #define F first
  12. #define S second
  13. #define MP make_pair
  14.  
  15. vector <pair <int, bool> > zapr;
  16. int n, j, b[100000], c[100000], block[100000];
  17. char ss[1000100], rd;
  18. int sz = 0;
  19.  
  20. void print_int(int n) {
  21.     if (!n)
  22.         return;
  23.  
  24.     print_int(n / 10);
  25.     ss[sz++] = n % 10 + '0';
  26. }
  27.  
  28. int gcd(int a, int b) {
  29.     if (!a || !b)
  30.         return a + b;
  31.  
  32.     return gcd(b % a, a);
  33. }
  34.  
  35. int read_int() {
  36.     char c;
  37.  
  38.     do {
  39.         c = getchar();
  40.     } while (c < '0' || c > '9');
  41.  
  42.     int res = 0;
  43.  
  44.     do {
  45.         res = res * 10 + c - '0';
  46.         c = getchar();
  47.     } while (c >= '0' && c <= '9');
  48.  
  49.     return res;
  50. }
  51.  
  52. char read_char() {
  53.     char c;
  54.  
  55.     do {
  56.         c = getchar();
  57.     } while (c != '+' && c != '-');
  58.  
  59.     return c;
  60. }
  61.  
  62. int fastMin(int x, int y) { return (((y-x)>>(32-1))&(x^y))^x; }
  63.  
  64. int main() {
  65.     #ifndef ONLINE_JUDGE
  66.         freopen("in", "r", stdin);
  67.     #endif
  68.  
  69.     n = read_int();
  70.  
  71.     zapr.resize(n);
  72.  
  73.     for (int i = 0; i < n; i++) {
  74.         zapr[i].S = (read_char() - '-');
  75.         zapr[i].F = read_int();
  76.     }
  77.  
  78.     map <int, bool> Map(zapr.begin(), zapr.end());
  79.  
  80.     vector <int> a;
  81.     a.reserve(Map.size());
  82.  
  83.     for (map <int, bool> :: iterator it = Map.begin(); it != Map.end(); it++)
  84.         a.push_back(it -> F);
  85.  
  86.     int len = a.size();
  87.     int l = sqrt(len + .0) + 1;
  88.  
  89.     int cnt = 0, cnt1 = 0;
  90.  
  91.     for (int i = 0; i < len; i++) {
  92.         if (cnt1 == l) {
  93.             cnt++;
  94.             cnt1 = 0;
  95.         }
  96.         block[i] = cnt;
  97.         cnt1++;
  98.     }  
  99.        
  100.  
  101.     for (int i = 0; i < n; i++) {
  102.         int pos = lower_bound(a.begin(), a.end(), zapr[i].F) - a.begin();
  103.  
  104.         c[pos] += (zapr[i].S ? 1 : -1);
  105.  
  106.         j = block[pos];
  107.  
  108.         b[j] = 0;
  109.  
  110.         for (int k = j * l; k < fastMin((j + 1) * l, len) && (b[j] ^ 1); k++)
  111.             if (c[k])
  112.                 b[j] = gcd(b[j], a[k]);
  113.  
  114.         int ans = 0;
  115.  
  116.         for (j = 0; j < l + 1 && (ans ^ 1); j++)
  117.             ans = gcd(ans, b[j]);
  118.  
  119.         print_int(ans ? ans : 1);
  120.         ss[sz++] = '\n';
  121.     }
  122.  
  123.     printf("%s", ss);
  124.  
  125.     return 0;                  
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement