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
![](http://codeforces.com/predownloaded/af/1b/af1b4d5401ef843e35de22bbd1367fa78816de31.png)
, 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
Output
Note
After first five operations multiset A contains integers0,
8, 9,
11, 6 and 1.
The answer for the sixth query is integer
![](http://codeforces.com/predownloaded/f1/be/f1be3f0eb002b70f2885010a36a4ee3437035f76.png)
— maximum among integers
![](http://codeforces.com/predownloaded/37/b9/37b978f8f58974079cd14c236f215799c4498de1.png)
,
![](http://codeforces.com/predownloaded/5d/c1/5dc10ff70d05fbc23f9ca460fcdaabd323da2000.png)
,
![](http://codeforces.com/predownloaded/25/ea/25eadc76e65c2dcc9e605d7cbcb8b85c432154b6.png)
,
![](http://codeforces.com/predownloaded/4e/9f/4e9f7b2b7c9c3c4a682f5417c5422483016ee463.png)
and
![](http://codeforces.com/predownloaded/6f/0d/6f0d4181cb4df733db930b2d79c66c1f83fafd42.png)
.
题意:进行n次操作 0 在集合内
+ 表示将 x加入集合内
- 表示将x删除集合内
? 表示查询x与集合内的数的 异或的最大值
思路: xor数 --- 存起来当模板
ACcode:
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
![](http://codeforces.com/predownloaded/af/1b/af1b4d5401ef843e35de22bbd1367fa78816de31.png)
, 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
![](http://codeforces.com/predownloaded/f1/be/f1be3f0eb002b70f2885010a36a4ee3437035f76.png)
— maximum among integers
![](http://codeforces.com/predownloaded/37/b9/37b978f8f58974079cd14c236f215799c4498de1.png)
,
![](http://codeforces.com/predownloaded/5d/c1/5dc10ff70d05fbc23f9ca460fcdaabd323da2000.png)
,
![](http://codeforces.com/predownloaded/25/ea/25eadc76e65c2dcc9e605d7cbcb8b85c432154b6.png)
,
![](http://codeforces.com/predownloaded/4e/9f/4e9f7b2b7c9c3c4a682f5417c5422483016ee463.png)
and
![](http://codeforces.com/predownloaded/6f/0d/6f0d4181cb4df733db930b2d79c66c1f83fafd42.png)
.
题意:进行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; }
相关文章推荐
- Codeforces Round #367 (Div. 2) [D] Vasiliy's Multiset(01字典树模板)
- 【Codeforces Round 367 (Div 2) D】【字典树典型题】Vasiliy's Multiset
- Codeforces Round #172 (Div. 2) D. Maximum Xor Secondary 单调栈应用
- Codeforces Round #430 (Div. 2) D.Vitya and Strange Lesson 异或 01字典树补集最小
- Codeforces Round #177 (Div. 1) C. Polo the Penguin and XOR operation(贪心)
- 【Codeforces Round 367 (Div 2) B】【二分查找】Interesting drink
- Codeforces Round #430 (Div. 2) D.Vitya and Strange Lesson 异或 01字典树补集最小
- 【Codeforces Round 367 (Div 2) C】【简单DP】Hard problem
- Codeforces Round #430 (Div. 2) D.Vitya and Strange Lesson 异或 01字典树补集最小
- Codeforces Round #440 (Div. 2, based on Technocup 2018 Elimination Round 2) D. Something with XOR Queries
- Codeforces Round #430 (Div. 2) D.Vitya and Strange Lesson 异或 01字典树补集最小
- Codeforces Round #367 (Div. 2)-Vasiliy's Multiset-最大异或和-trie
- codeforces round 396 div2 E Mahmoud and a xor trip 按位操作+dp子树内外
- Codeforces Round #430 (Div. 2) D.Vitya and Strange Lesson 异或 01字典树补集最小
- Codeforces Round #396 (Div. 2) E. Mahmoud and a xor trip
- Codeforces Round #367 (Div. 2)-C - Hard problem-DP
- Codeforces Round #367 (Div. 2) B. Interesting drink(二分)
- Codeforces Round #430 (Div. 2) D.Vitya and Strange Lesson 异或 01字典树补集最小
- Codeforces Round #340 (Div. 2) E. XOR and Favorite Number 莫队算法
- 【Codeforces Round 367 (Div 2) E】【十字链表 边框维护】Working routine nm矩形交换q次子矩形的最终矩形