Vasiliy's Multiset

题目链接:

http://codeforces.com/contest/706/problem/D

Description


```
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 integer x from multiset A. It's guaranteed that at least one x 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 integer y from the multiset A.

Multiset is a set, where equal elements are allowed.

</big>

##Input
<big>

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.

</big>

##Output
<big>

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.

</big>

##Examples
<big>

input

10

  • 8
  • 9
  • 11
  • 6
  • 1

    ? 3
  • 8

    ? 3

    ? 8

    ? 11

    output

    11

    10

    14

    13
</big>

##Source
<big>
Codeforces Round #367 (Div. 2)
</big> <br/>
##题意:
<big>
维护一个multiset:
操作1:插入一个数字
操作2:删除指定数字
操作3:给出一个x,求集合中的一个元素y,使得 x XOR y 最大.
</big> <br/>
##题解:
<big>
由于异或前后的值不能保持单调性,所以排序和线段树都不好处理.
这里需要用一个 0/1-Trie树 来维护.
对于集合中的数用二叉树中的结点来表示其二进制位.
①插入:开辟一些新的树结点.
②删除:找到对应结点赋成NULL. 删除时有几点需要注意:
集合允许重复,所以可以记录每个数出现的次数,当集合中仅有一个x时才删除.
把最底端的叶节点删除后,要递归更新其父结点的状态,否则在匹配时可能会进入不存在的结点.
③匹配:先把x按位取反,从高位到低位在二叉树中匹配x,按能匹配时尽量匹配的原则往下走. 这样可以保证结果从高位到低位有尽量多的位不相同,即异或结果最大.
注意:初始时集合中有个0,一开始忘了这个,RE了2发.
</big> <br/>
##代码:
``` cpp
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <stack>
#include <vector>
#include <list>
#define LL long long
#define eps 1e-8
#define maxn 101000
#define mod 100000007
#define inf 0x3f3f3f3f
#define mid(a,b) ((a+b)>>1)
#define IN freopen("in.txt","r",stdin);
using namespace std; class Trie
{
public:
Trie *next[2];
Trie() {
memset(next,NULL,sizeof(next));
}
}; Trie *root; void Insert(LL n)
{
Trie *p = root;
for(int i=31;i>=0;i--)
{
int id = (n >> i) & 1;
if(p->next[id] == NULL)
p->next[id] = new Trie();
p = p->next[id];
}
} int Delete(Trie *p, LL n, int i)
{
if(i < 0) return 1;
int id = (n >> i) & 1;
int ret = Delete(p->next[id], n, i-1);
if(ret) p->next[id] = NULL;
if(p->next[0] == NULL && p->next[1] == NULL)
return 1;
return 0;
} LL Match(LL m)
{
m = ~m;
LL ans = 0;
Trie *p = root;
for(int i=31;i>=0;i--) {
ans <<= 1;
int bit = (m >> i) & 1;
if(bit) {
if(p->next[1]) {
p = p->next[1];
ans++;
}
else {
p = p->next[0];
}
}
else {
if(p->next[0]) {
p = p->next[0];
}
else {
p = p->next[1];
ans++;
}
}
}
return ans;
} map<LL, int> occ; int main(int argc, char const *argv[])
{
//IN; int q;
while(scanf("%d", &q) != EOF)
{
root = new Trie();
occ.clear();
Insert(0LL);
occ[0] = 1;
while(q--)
{
getchar();
char op; LL val;
op = getchar();
scanf("%I64d", &val); if(op == '+') {
if(!occ[val]) {
occ[val] = 1;
Insert(val);
}
else occ[val]++;
}
else if(op == '-') {
if(occ[val] == 1) Delete(root, val, 31);
occ[val]--;
} else {
printf("%I64d\n", Match(val)^val);
}
}
} return 0;
}

Codeforces Round #367 (Div. 2) D. Vasiliy's Multiset (0/1-Trie树)的更多相关文章

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

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

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

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

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

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

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

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

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

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

  8. Codeforces Round #367 (Div. 2) B. Interesting drink (模拟)

    Interesting drink 题目链接: http://codeforces.com/contest/706/problem/B Description Vasiliy likes to res ...

  9. Codeforces Round #367 (Div. 2) A. Beru-taxi (水题)

    Beru-taxi 题目链接: http://codeforces.com/contest/706/problem/A Description Vasiliy lives at point (a, b ...

随机推荐

  1. R语言iris数据集的层次聚类

    data=iris[,-5]dist.e=dist(data,method='euclidean')model1=hclust(dist.e,method='ward') #分3类result=cut ...

  2. bzoj2428: [HAOI2006]均分数据

    模拟退火.挺好理解的.然后res打成了ans一直WA一直WA...!!!一定要注意嗷嗷嗷一定要注意嗷嗷嗷一定要注意嗷嗷嗷. 然后我就一直卡一直卡...发现最少1800次的时候就可以出解了.然后我就去调 ...

  3. Strongly connected 挺简单的tarjan

    题意:给你一个连通图,问你最多加多少条边,还能保证该图不是强连通图. 对整个图求强连通分量,然后对图缩点,记录一下缩点之后每隔点包含的原来的点的个数,找出最少的那个点,然后对这个点建成完全图,对另外的 ...

  4. 另类方法解决设计Web页面出现:Error Creating Control

    在B/S开发的过程中,经常会遇到这样的提示:Error Creating Control ,而这些页面明明之前是可以打开的,但还是出现如下图所示: 网上找到的方法是把控件初始化放在OnInit里去写, ...

  5. SQL语句方法语法总结(二)

    1.给表插入数据. (1)INSERT INTO TBL_NAME VALUES (VALUE_1,VALUE_2,...) (2)INSERT INTO TBL_NAME (COL_1,COL_2, ...

  6. 在页面中使用js

    JavaScript:用来在页面编写特效的,和HTML\CSS一样当都是由浏览器解析 JavaScript语言 一.JS如何运行(JavaScript,jscript,VbScript,applet ...

  7. 想找个计算器当本命?来试试UWP应用《纸书科学计算器》

    久违了.上次在博客园发文还是4年前,正是高中参加NOIP的时候.这4年里发生了很多事,乃至再次看到过去的文章时,仿佛看到了自己也不熟悉的风景.最近很想把我的博客重新拾起来,慢慢灌溉,写一些微不足道的技 ...

  8. erl0008 - unicode 和 utf-8之间的关系

    转载:http://blog.jobbole.com/84903/ 原文出处: 卢钧轶   欢迎分享原创到伯乐头条 本文将简述字符集,字符编码的概念.以及在遭遇乱码时的一些常用诊断技巧. 背景:字符集 ...

  9. The Robust Fuzzy C-means

    摘要: 基于FCM的在图像处理方面对噪声敏感的不足,本文通过引入空间模型建立空间模糊C均值聚类提高算法的鲁棒性,在此基础上,结合抑制式对算法进一步优化.最后,给图像加不同程度的噪声,通过MATLAB编 ...

  10. vs2008破解方法

    前提条件:测试操作系统WIN7 1,解压缩镜像文件 2,找到文件:Setup\setup.sdb,用记事本打开: 3,找到以下项: [Product Key]XPWKC7X98VKQDGM3QWYVG ...