Guest User

1846. GCD 2010

a guest
Feb 14th, 2016
430
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.74 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. #define ll int
  3. using namespace std;
  4. #define MAX 100005
  5.  
  6. ll tree[400005];
  7.  
  8. void update(ll node,ll b,ll e,ll i, ll val)
  9. {
  10.     if(i<b || i>e)
  11.     {
  12.         return;
  13.     }
  14.     if(b==e)
  15.     {
  16.         tree[node]=val;
  17.         return;
  18.     }
  19.  
  20.     ll left=0,right=0,mid=0;
  21.     left=2*node;
  22.     right=2*node+1;
  23.     mid=(b+e)/2;
  24.  
  25.     update(left,b,mid,i,val);
  26.     update(right,mid+1,e,i,val);
  27.  
  28.     tree[node]=__gcd(tree[left],tree[right]);
  29.     return;
  30. }
  31.  
  32. int main()
  33. {
  34.     ll n,m,i,j,a;
  35.     char ch;
  36.     map<ll,ll>mp,mp1;
  37.     while(scanf("%d",&n)!=EOF)
  38.     {
  39.         memset(tree,0,sizeof tree);
  40.         mp.clear();
  41.         mp1.clear();
  42.         j=1;
  43.         for(i=1;i<=MAX;i++)
  44.             update(1,1,MAX,i,0);
  45.  
  46.         for(i=1; i<=n; i++)
  47.         {
  48. //            for(ll k=1;k<=15;k++)
  49. //                cout<<tree[j]<<ends;
  50. //            cout<<endl;
  51.             getchar();
  52.             scanf("%c %d",&ch,&m);
  53.  
  54.             if(ch=='+')
  55.             {
  56.                 if(mp.count(m)>0)
  57.                 {
  58.                     mp[m]++;
  59.                 }
  60.                 else
  61.                 {
  62.                     update(1,1,MAX,j,m);
  63.                     mp[m]++;
  64.                     mp1[m]=j;
  65.                     j++;
  66.                 }
  67.                 printf("%d\n",max(1,tree[1]));
  68.             }
  69.             if(ch=='-')
  70.             {
  71.                 if(mp[m]>1)
  72.                 {
  73.                     mp[m]--;
  74.                 }
  75.                 else if(mp[m]==1)
  76.                 {
  77.                     mp[m]--;
  78.                     a=mp1[m];
  79.                     update(1,1,MAX,a,0);
  80.                 }
  81.                 printf("%d\n",max(1,tree[1]));
  82.             }
  83.         }
  84.     }
  85.     return 0;
  86. }
Advertisement
Add Comment
Please, Sign In to add comment