Vasiliy's Multiset

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:

  1. "+ x" — add integer x to multiset A.
  2. "- 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.
  3. "? 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 .

分析: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+10;
const int dis[4][2]={{0,1},{-1,0},{0,-1},{1,0}};
using namespace std;
ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p;p=p*p;q>>=1;}return f;}
int n,m,k,t,q,id;
char a[10];
struct node
{
    int next[2],num,cnt;
}p[maxn];
void insert(int n)
{
    int now=0;
    for(int i=31;i>=0;i--)
    {
        if(!p[now].next[n>>i&1])p[now].next[n>>i&1]=++id;
        now=p[now].next[n>>i&1],p[now].cnt++;
    }
    p[now].num=n;
}
void del(int n)
{
    int now=0;
    for(int i=31;i>=0;i--)
    {
        now=p[now].next[n>>i&1],p[now].cnt--;
    }
}
int query(int n)
{
    int now=0;
    for(int i=31;i>=0;i--)
    {
        if(p[p[now].next[n>>i&1^1]].cnt)now=p[now].next[n>>i&1^1];
        else now=p[now].next[n>>i&1];
    }
    return n^p[now].num;
}
int main()
{
    int i,j;
    insert(0);
    scanf("%d",&q);
    while(q--)
    {
        scanf("%s %d",a,&m);
        if(a[0]=='+')
        {
            insert(m);
        }
        else if(a[0]=='-')
        {
            del(m);
        }
        else
        {
            printf("%d\n",query(m));
        }
    }
    //system("pause");
    return 0;
}
posted @ 2016-08-12 11:27  mxzf0213  阅读(340)  评论(0编辑  收藏  举报