4 seconds
256 megabytes
standard input
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.
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.
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.
10
+ 8
+ 9
+ 11
+ 6
+ 1
? 3
- 8
? 3
? 8
? 11
11
10
14
13
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 .
分析:01字典树;
代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#define rep(i,m,n) for(i=m;i<=n;i++)
#define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
#define mod 1000000007
#define inf 0x3f3f3f3f
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define pi acos(-1.0)
const int maxn=1e7+;
const int dis[][]={{,},{-,},{,-},{,}};
using namespace std;
ll gcd(ll p,ll q){return q==?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=;while(q){if(q&)f=f*p;p=p*p;q>>=;}return f;}
int n,m,k,t,q,id;
char a[];
struct node
{
int next[],num,cnt;
}p[maxn];
void insert(int n)
{
int now=;
for(int i=;i>=;i--)
{
if(!p[now].next[n>>i&])p[now].next[n>>i&]=++id;
now=p[now].next[n>>i&],p[now].cnt++;
}
p[now].num=n;
}
void del(int n)
{
int now=;
for(int i=;i>=;i--)
{
now=p[now].next[n>>i&],p[now].cnt--;
}
}
int query(int n)
{
int now=;
for(int i=;i>=;i--)
{
if(p[p[now].next[n>>i&^]].cnt)now=p[now].next[n>>i&^];
else now=p[now].next[n>>i&];
}
return n^p[now].num;
}
int main()
{
int i,j;
insert();
scanf("%d",&q);
while(q--)
{
scanf("%s %d",a,&m);
if(a[]=='+')
{
insert(m);
}
else if(a[]=='-')
{
del(m);
}
else
{
printf("%d\n",query(m));
}
}
//system("pause");
return ;
}