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
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 .

题意:n个操作,+  x表示在一个Multiset中添加一个x的数;

        -  x表示在一个Multiset中删除一个x的数;

        ? x表示输出x与Multiset中一个最大的异或值

思路:trie数,找x的二进制为相反,相反的位置最大结果越优;

#include<bits/stdc++.h>
using namespace std;
#define ll __int64
#define mod 1000000007
#define pi (4*atan(1.0))
const int N=1e5+,M=4e6+,inf=1e9+;
int a[M][],sum[M],len;
void init()
{
memset(a,,sizeof(a));
memset(sum,,sizeof(sum));
len=;
}
void insertt(int x)
{
int num[];
memset(num,,sizeof(num));
int flag=;
while(x)
{
num[flag++]=x%;
x/=;
}
int u=,n=;
for(int i=n; i>=; i--)
{
if(!a[u][num[i]])
{
a[u][num[i]]=len++;
}
u=a[u][num[i]];
sum[u]++;
}
}
void del(int x)
{
int num[];
memset(num,,sizeof(num));
int flag=;
while(x)
{
num[flag++]=x%;
x/=;
}
int u=,n=;
for(int i=n; i>=; i--)
{
int v=a[u][num[i]];
sum[v]--;
if(!sum[v])
a[u][num[i]]=;
u=v;
}
}
int getans(int x)
{
int num[];
memset(num,,sizeof(num));
int flag=;
while(x)
{
num[flag++]=x%;
x/=;
}
for(int i=; i<=; i++)
num[i]=num[i]?:;
int u=,n=,v,w;
int ans=;
for(int i=n; i>=; i--)
{
if(num[i])
{
v=;
w=;
}
else
{
w=;
v=;
}
if(a[u][v])
{
u=a[u][v];
ans+=(<<i);
}
else
u=a[u][w];
}
return ans;
}
char ch[];
int main()
{
int x,y,z,i,t;
int T,cas;
init();
insertt();
scanf("%d",&T);
for(cas=; cas<=T; cas++)
{
scanf("%s %d",ch,&x);
if(ch[]=='+')
insertt(x);
else if(ch[]=='-')
del(x);
else
printf("%d\n",getans(x));
}
return ;
}

Codeforces Round #367 (Div. 2) D. Vasiliy's Multiset trie树的更多相关文章

  1. Codeforces Round #367 (Div. 2) D. Vasiliy's Multiset Trie

    题目链接: http://codeforces.com/contest/706/problem/D D. Vasiliy's Multiset time limit per test:4 second ...

  2. Codeforces Round #367 (Div. 2)D. Vasiliy's Multiset (字典树)

    D. Vasiliy's Multiset time limit per test 4 seconds memory limit per test 256 megabytes input standa ...

  3. Codeforces Round #367 (Div. 2) D. Vasiliy's Multiset

    题目链接:Codeforces Round #367 (Div. 2) D. Vasiliy's Multiset 题意: 给你一些操作,往一个集合插入和删除一些数,然后?x让你找出与x异或后的最大值 ...

  4. Codeforces Round #367 (Div. 2) D. Vasiliy's Multiset (0/1-Trie树)

    Vasiliy's Multiset 题目链接: http://codeforces.com/contest/706/problem/D Description Author has gone out ...

  5. Codeforces Round #367 (Div. 2) D. Vasiliy's Multiset(可持久化Trie)

    D. Vasiliy's Multiset time limit per test 4 seconds memory limit per test 256 megabytes input standa ...

  6. Codeforces Round #367 (Div. 2) D. Vasiliy's Multiset(01字典树求最大异或值)

    http://codeforces.com/contest/706/problem/D 题意:有多种操作,操作1为在字典中加入x这个数,操作2为从字典中删除x这个数,操作3为从字典中找出一个数使得与给 ...

  7. Codeforces Round #367 (Div. 2)---水题 | dp | 01字典树

    A.Beru-taxi 水题:有一个人站在(sx,sy)的位置,有n辆出租车,正向这个人匀速赶来,每个出租车的位置是(xi, yi) 速度是 Vi;求人最少需要等的时间: 单间循环即可: #inclu ...

  8. Codeforces Round #333 (Div. 1) D. Acyclic Organic Compounds trie树合并

    D. Acyclic Organic Compounds   You are given a tree T with n vertices (numbered 1 through n) and a l ...

  9. Codeforces Round #367 (Div. 2) C. Hard problem(DP)

    Hard problem 题目链接: http://codeforces.com/contest/706/problem/C Description Vasiliy is fond of solvin ...

随机推荐

  1. UVA Dividing coins

    题目链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_proble ...

  2. 从jarray中删除指定元素的问题

    string jsonText = "[{\"a\": \"aaa\",\"b\": \"bbb\",\&qu ...

  3. Ubuntu 系统下可以做什么?

    ubuntu和windows到底有什么不同呢?从大的方面讲,它们的设计理念不同.借用一位知乎前辈说的“windows为不知道自己正在做什么的人设计,linux为知道自己要做什么,正在做什么的人设计”. ...

  4. JavaScript数据结构与算法-数组练习

    一. 创建一个记录学生成绩的对象,提供一个添加成绩的方法,以及一个显示学生平均成绩的方法. // 创建一个记录学生成绩的对象 const Students = function Students () ...

  5. Testlink安装访问提示“应用程序DEFAULT WEB SITE”中的服务器错误

    错误摘要:HTTP错误403.14 - ForbiddenWeb服务器被配置为不列出此目录的内容.

  6. ubuntu安装deb文件

    install the deb-package, e.g. using the Terminal command$ sudo apt install <path-to-smartgit-deb- ...

  7. android开发 软键盘出现后 防止EditText控件遮挡 总体平移UI

    在EditText控件接近底部的情况下 软键盘弹出后会把获得焦点的EditText控件遮挡 无法看到输入信息  防止这种情况发生 就须要设置AndroidManifest.xml的属性 前面的xml信 ...

  8. http的keep-alive和tcp的keepalive区别

    原文地址:http://blog.csdn.net/oceanperfect/article/details/51064574 1.HTTP Keep-Alive在http早期,每个http请求都要求 ...

  9. gradle build 找不到tools.jar 解决方法

    import javax.tools.ToolProvider compile(files(((URLClassLoader) ToolProvider.getSystemToolClassLoade ...

  10. 联合文件系统 unionfs