CF#367(Div2)D. Vasiliy's Multiset (字典树)

来源:互联网 发布:高校大数据实验室 编辑:程序博客网 时间:2024/05/28 23:20

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:

  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
11101413
Note

After first five operations multiset A contains integers 089116 and 1.

The answer for the sixth query is integer  — maximum among integers  and .




题目大意:要你模拟mutiset的操作,? x时要求x与mutiset里元素异或的最大值。


解题思路:字典树tire,只有0和1的分枝,然后从高位到低位枚举即可。


/* ***********************************************┆  ┏┓   ┏┓ ┆┆┏┛┻━━━┛┻┓ ┆┆┃       ┃ ┆┆┃   ━   ┃ ┆┆┃ ┳┛ ┗┳ ┃ ┆┆┃       ┃ ┆┆┃   ┻   ┃ ┆┆┗━┓ 马 ┏━┛ ┆┆  ┃ 勒 ┃  ┆      ┆  ┃ 戈 ┗━━━┓ ┆┆  ┃ 壁     ┣┓┆┆  ┃ 的草泥马  ┏┛┆┆  ┗┓┓┏━┳┓┏┛ ┆┆   ┃┫┫ ┃┫┫ ┆┆   ┗┻┛ ┗┻┛ ┆************************************************ */#include <stdio.h>#include <string.h>#include <iostream>#include <algorithm>#include <vector>#include <queue>#include <stack>#include <set>#include <map>#include <string>#include <math.h>#include <stdlib.h>using namespace std;#define rep(i,a,b) for (int i=(a),_ed=(b);i<=_ed;i++)#define per(i,a,b) for (int i=(b),_ed=(a);i>=_ed;i--)#define pb push_back#define mp make_pairconst int inf_int = 2e9;const long long inf_ll = 2e18;#define inf_add 0x3f3f3f3f#define mod 1000000007#define LL long long#define ULL unsigned long long#define MS0(X) memset((X), 0, sizeof((X)))#define SelfType intSelfType Gcd(SelfType p,SelfType q){return q==0?p:Gcd(q,p%q);}SelfType Pow(SelfType p,SelfType q){SelfType ans=1;while(q){if(q&1)ans=ans*p;p=p*p;q>>=1;}return ans;}#define Sd(X) int (X); scanf("%d", &X)#define Sdd(X, Y) int X, Y; scanf("%d%d", &X, &Y)#define Sddd(X, Y, Z) int X, Y, Z; scanf("%d%d%d", &X, &Y, &Z)inline int read(){int ra,fh;char rx;rx=getchar(),ra=0,fh=1;while((rx<'0'||rx>'9')&&rx!='-')rx=getchar();if(rx=='-')fh=-1,rx=getchar();while(rx>='0'&&rx<='9')ra*=10,ra+=rx-48,rx=getchar();return ra*fh;}//#pragma comment(linker, "/STACK:102400000,102400000")struct Tire{    int nx[2],cnt;    void newtire()    {        MS0(nx);        cnt = 0;    }}tire[200005*32];int root,tot;void update(int x,int val){    int rt = root;    for(int i=31;i>=0;i--)    {        int bit = x >> i & 1;        if(!tire[rt].nx[bit])        {            tire[++tot].newtire();            tire[rt].nx[bit] = tot;        }        rt = tire[rt].nx[bit];        tire[rt].cnt += val;    }}int query(int x){    int res = 0;    int rt = root;    for(int i=31;i>=0;i--)    {        int bit = x >> i & 1;        if(!tire[rt].nx[bit^1] || !tire[tire[rt].nx[bit^1]].cnt)rt = tire[rt].nx[bit];        else        {            rt = tire[rt].nx[bit^1];            res |= 1 << i;        }    }    return res;}int main(){    //freopen("in.txt","r",stdin);    //freopen("out.txt","w",stdout);    int q;    root = 1,tot = 1;    tire[tot++].newtire();    update(0,1);    q = read();    while(q--)    {        char c[2];        int x;        scanf("%s%d",c,&x);        {            if(c[0]=='?')printf("%d\n",query(x));            else            {                update(x,c[0]=='+' ? 1 : -1);            }        }    }    return 0;}



0 0