D. Vasiliy’s Multiset
time limit per test
4 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
Author has gone out of the stories about Vasiliy, so here is just a formal task description.
You are given q queries and a multiset A, initially containing only integer 0. There are three types of queries:
"+ x" — add integer x to multiset A.
"- x" — erase one occurrence of integer x from multiset A. It's guaranteed that at least one x is present in the multiset A before this query.
"? x" — you are given integer x and need to compute the value , i.e. the maximum value of bitwise exclusive OR (also know as XOR) of integer x and some integer y from the multiset A.
Multiset is a set, where equal elements are allowed.
Input
The first line of the input contains a single integer q (1 ≤ q ≤ 200 000) — the number of queries Vasiliy has to perform.
Each of the following q lines of the input contains one of three characters ‘+’, ‘-’ or ‘?’ and an integer xi (1 ≤ xi ≤ 109). It’s guaranteed that there is at least one query of the third type.
Note, that the integer 0 will always be present in the set A.
Output
For each query of the type ‘?’ print one integer — the maximum value of bitwise exclusive OR (XOR) of integer xi and some integer from the multiset A.
Example
Input
10
+ 8
+ 9
+ 11
+ 6
+ 1
? 3
- 8
? 3
? 8
? 11
Output
11
10
14
13
Note
After first five operations multiset A contains integers 0, 8, 9, 11, 6 and 1.
The answer for the sixth query is integer — maximum among integers , , , and .
题意:有三种操作
+
x
:添加元素
−
思路:用字典树按高位存储数字,然后从最高位开始搜索,注意数字
x
也可以不异或,所以说,每次要先添加一个元素
ac代码:
/* ***********************************************
Author : AnICoo1
Created Time : 2016-08-19-21.51 Friday
File Name : D:\MyCode\2016-8月\2016-8-19.cpp
LANGUAGE : C++
Copyright 2016 clh All Rights Reserved
************************************************ */
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<stack>
#include<set>
#include<map>
#include<queue>
#include<vector>
#include<iostream>
#include<algorithm>
#define MAXN 1010000
#define LL long long
#define ll __int64
#define INF 0xfffffff
#define mem(x,y) memset(x,(y),sizeof(x))
#define PI acos(-1)
#define gn (sqrt(5.0)+1)/2
#define eps 1e-8
using namespace std;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
ll powmod(ll a,ll b,ll MOD){ll ans=1;while(b){if(b%2)ans=ans*a%MOD;a=a*a%MOD;b/=2;}return ans;}
double dpow(double a,ll b){double ans=1.0;while(b){if(b%2)ans=ans*a;a=a*a;b/=2;}return ans;}
//head
struct s
{
int num;
s *next[2];
};
s *root;
void create(int x)
{
s *p=root,*q;
for(int i=30;i>=0;i--)
{
int id=(x>>i)&1;
if(p->next[id]==NULL)
{
q=(s *)malloc(sizeof(s));
q->num=1;
for(int j=0;j<2;j++)
q->next[j]=NULL;
p->next[id]=q;
p=p->next[id];
}
else
{
p->next[id]->num++;
p=p->next[id];
}
}
}
void Delete(int x)
{
s *p=root,*q;
for(int i=30;i>=0;i--)
{
int id=(x>>i)&1;
p=p->next[id];
p->num--;
}
}
int Find(int x)
{
int ans=0;
s *p=root;
create(0);
for(int i=30;i>=0;i--)
{
int id=(x>>i)&1;
int to=id?0:1;
if(p->next[to]!=NULL&&p->next[to]->num>0)
{
p=p->next[to];
ans=ans*2+to;
}
else
{
p=p->next[id];
ans=ans*2+id;
}
}
return ans;
}
void Begin()
{
for(int i=0;i<2;i++)
root->next[i]=NULL;
root->num=0;
}
void freetree(s *t)
{
if(t==NULL)
return;
for(int i=0;i<2;i++)
{
if(t->next[i]!=NULL)
freetree(t->next[i]);
}
free(t);
return;
}
int main()
{
int n;scanf("%d",&n);
root=(s *)malloc(sizeof(s));
Begin();
for(int i=1;i<=n;i++)
{
char ch[2];int x;scanf("%s%d",ch,&x);
if(ch[0]=='+')
create(x);
else if(ch[0]=='-')
Delete(x);
else if(ch[0]=='?')
{
int y=Find(x);
printf("%d\n",x^y);
}
}
freetree(root);
return 0;
}