您的位置:首页 > 其它

Codeforces Round #367 (Div. 2) 字典树-xor

2016-08-13 10:31 441 查看
                                                                                                            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 integerx from multiset
A. It's guaranteed that at least onex 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 integery 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 setA.

Output
For each query of the type '?' print one integer — the maximum value of bitwise exclusive OR (XOR) of integerxi
and some integer from the multisetA.

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 integers0,
8, 9,
11, 6 and 1.

The answer for the sixth query is integer

 — maximum among integers

,

,

,


and

.

题意:进行n次操作  0 在集合内

+  表示将 x加入集合内 

-   表示将x删除集合内

? 表示查询x与集合内的数的  异或的最大值

思路: xor数 --- 存起来当模板

ACcode:

#include<iostream>
#include<cstdio>
using namespace std;
#define maxn  500000 * 2 * 20
int ch[maxn][2];
int cnt[maxn];
int clk = 1;
void insert(int x)
{
int root = 1;
cnt[root]++;
for (int i = 30; i >= 0; i--)
{
int s;
if (x & (1 << i)) s = 1;
else s = 0;
if (!ch[root][s])
ch[root][s] = ++clk;
root = ch[root][s];
cnt[root]++;
}
}
void remove(int x)
{
int root = 1;
cnt[root]--;
for (int i = 30; i >= 0; i--)
{
int s;
if (x & (1 << i)) s = 1;
else s = 0;
root = ch[root][s];
cnt[root]--;
}
}
int query(int x)
{
int rt = 1;
int ret = 0;
for (int i = 30; i >= 0; i--)
{
int s;
if (x & (1 << i)) s = 1;
else s = 0;
if (cnt[ch[rt][!s]])
{
ret |= 1 << i;
rt = ch[rt][!s];
}
else rt = ch[rt][s];
}
return ret;
}
int main()
{
int n;
scanf("%d", &n);
insert(0);
while (n--)
{
char s[10];
scanf("%s", s);
int x;
scanf("%d", &x);
if (s[0] == '+') insert(x);
else if (s[0] == '-') remove(x);
else printf("%d\n", query(x));
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐