neohacker's blog

By neohacker, 6 years ago, In Russian
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
int bor[2][3000002];
int cnt[2][3000002];
int T = 1;
int el = 0;
void add(int a)
{
	int cur = 0;
	for(int i = 30;i>=0;i--)
	{
	 	bool tmp = (((1<<i)&a)>>i);
	 	if(bor[tmp][cur] == 0)
		 	bor[tmp][cur] = T++;
	 	cnt[tmp][cur]++;
	 	cur = bor[tmp][cur];
	}
}
int k_el(int a)
{
	int cur = 0;
	int ans = 0;
	int tmp = 30;
	while(tmp >= 0)
	{
		if(cnt[0][cur] >= a)
		{
			cnt[0][cur]--; 	
			cur = bor[0][cur];
	 	}
	 	else
	 	{
	 		a -= cnt[0][cur];
	 	 	ans += (1<<tmp);
	 	 	cnt[1][cur]--;
	 	 	cur = bor[1][cur];
	 	}
	 	tmp--;
 	}
 	return ans;
}
void go()
{
	for(int i = 0;i<el;i++)
	{
	 	cout << k_el(1) << " ";
	}
}
int main()
{       
        ios::sync_with_stdio(0);  
        #ifndef LOCAL
	freopen("machine.in","r",stdin);
	freopen("machine.out","w",stdout);	
	#endif            
        int n;
        cin >> n;
        for(int i = 0;i<n;i++)
        {
                int a;
                cin >> a;
                if(a > 0)
                {
                        add(a);
                        el++;
                }
                else if(a < 0)
                {
			cout << k_el(-a) << endl;
			el--;
                }
                else
                {                	
                	go();
                	el = 0;      
                }
        }
}
  • Vote: I like it
  • -39
  • Vote: I do not like it