knightL

newF

Sep 18th, 2011
292
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.97 KB | None | 0 0
  1. #include <cstdio>
  2. #include <algorithm>
  3. #include <vector>
  4. using namespace std;
  5.  
  6. typedef int query;
  7.  
  8. vector<query> q;
  9. vector<int> dict;
  10.  
  11. vector<int> tree;
  12. int ds;
  13.  
  14. int getds(int n)
  15. {
  16.     n--;
  17.     n|=n>>1;
  18.     n|=n>>2;
  19.     n|=n>>4;
  20.     n|=n>>8;
  21.     n|=n>>16;
  22.     return n+1;
  23. }
  24.  
  25. int gcd(int a,int b)
  26. {
  27.     return b? gcd(b,a%b):a;
  28. }
  29.  
  30. void change(int i,int delta)
  31. {
  32.     i+=ds;
  33.     tree[i]+=delta;
  34.     tree[i/2]=gcd(tree[i]?dict[i-ds]:0,tree[i^1]?dict[(i^1)-ds]:0);
  35.     i/=4;
  36.     while(i)
  37.     {
  38.         tree[i]=gcd(tree[2*i],tree[2*i+1]);
  39.         i/=2;
  40.     }
  41. }
  42.  
  43. int main()
  44. {
  45.     int n;
  46.     char op[2];
  47.     int x;
  48.     scanf("%d",&n);
  49.     for(int i=0;i<n;i++)
  50.     {
  51.         scanf("%s%d",op,&x);
  52.         q.push_back(x*((op[0]=='+')*2-1));
  53.         dict.push_back(x);
  54.     }
  55.     sort(dict.begin(),dict.end());
  56.     ds=getds(max(n,16));
  57.     tree.resize(ds*2);
  58.     for(int i=ds-1;i>0;i--)
  59.         tree[i]=0;
  60.     for(int i=0;i<n;i++)
  61.     {
  62.         change(lower_bound(dict.begin(),dict.end(),abs(q[i]))-dict.begin(),q[i]/abs(q[i]));
  63.         printf("%d\n",tree[1]?tree[1]:1);
  64.     }
  65. }
Advertisement
Add Comment
Please, Sign In to add comment