题目链接:http://codeforces.com/contest/706/problem/D

题意很简单不多说。

把一个数当作二进制插入字典树中,按照xor的性质,1找0,0找1,贪心即可。

注意的是一开始集合中就有0,所以一开始'?'查询时输出他本身。

 //#pragma comment(linker, "/STACK:102400000, 102400000")
#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <vector>
#include <cmath>
#include <ctime>
#include <list>
#include <set>
#include <map>
using namespace std;
typedef long long LL;
typedef pair <int, int> P;
struct trie {
int cnt;
trie *next[];
trie() {
cnt = ;
for(int i = ; i < ; ++i)
next[i] = NULL;
}
}; void insert(int num[], trie *root) {
trie *p = root;
for(int i = ; i >= ; --i) {
if(p->next[num[i]] == NULL) {
p->next[num[i]] = new trie();
}
p = p->next[num[i]];
p->cnt++;
}
} void del(int num[], trie *root) {
trie *p = root;
for(int i = ; i >= ; --i) {
p = p->next[num[i]];
p->cnt--;
}
} int search(int num[], trie *root, int val) {
int res = ;
trie *p = root;
for(int i = ; i >= ; --i) {
if(p->next[num[i]^] != NULL && p->next[num[i]^]->cnt > ) {
p = p->next[num[i]^];
res += ( << i);
}
else if(p->next[num[i]] != NULL && p->next[num[i]]->cnt > ) {
p = p->next[num[i]];
}
else {
res = ;
break;
}
}
return max(val, res);
} void destory(trie *root) {
if(root->next[] != NULL)
destory(root->next[]);
if(root->next[] != NULL)
destory(root->next[]);
delete(root);
} int main()
{
trie *root = new trie();
int n, val, num[];
char op[];
scanf("%d", &n);
while(n--) {
scanf("%s %d", op, &val);
int f = , temp = val;
do {
num[f++] = val % ;
val /= ;
}while(val);
while(f < ) {
num[f++] = ;
}
if(op[] == '+') {
insert(num, root);
}
else if(op[] == '-') {
del(num, root);
}
else {
printf("%d\n", root->next[] != NULL || root->next[] != NULL ? search(num, root, temp) : temp);
}
}
destory(root);
return ;
}

Codeforces 706 D. Vasiliy's Multiset (字典树贪心)的更多相关文章

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

  2. CodeForces 706D Vasiliy's Multiset (字典树查询+贪心)

    题意:最开始的时候有一个集合,集合里面只有一个元素0,现在有q次操作,操作分为3种: + x: 表示向集合中添加一个元素x - x:表示删除集合中值为x的一个元素 ? x:表示查询集合中与x异或的最大 ...

  3. codeforces 706D D. Vasiliy's Multiset(trie树)

    题目链接: D. Vasiliy's Multiset time limit per test 4 seconds memory limit per test 256 megabytes input ...

  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 455B A Lot of Games(字典树+博弈)

    题目连接: Codeforces 455B A Lot of Games 题目大意:给定n.表示字符串集合. 给定k,表示进行了k次游戏,然后是n个字符串.每局開始.字符串为空串,然后两人轮流在末尾追 ...

  6. Educational Codeforces Round 12 E. Beautiful Subarrays 字典树

    E. Beautiful Subarrays 题目连接: http://www.codeforces.com/contest/665/problem/E Description One day, ZS ...

  7. 【codeforces 514C】Watto and Mechanism(字典树做法)

    [题目链接]:http://codeforces.com/contest/514/problem/C [题意] 给你n个字符串; 然后给你m个询问;->m个字符串 对于每一个询问字符串 你需要在 ...

  8. codeforces 1285D. Dr. Evil Underscores(字典树)

    链接:https://codeforces.com/problemset/problem/1285/D 题意:给n个数a1,a2,a3.....an,找到一个数X,使得X 异或所有的ai ,得到的ma ...

  9. Codeforces 633 C Spy Syndrome 2 字典树

    题意:还是比较好理解 分析:把每个单词反转,建字典树,然后暴力匹配加密串 注:然后我就是特别不理解,上面那种能过,而且时间很短,但是我想反之亦然啊 我一开始写的是,把加密串进行反转,然后单词正着建字典 ...

随机推荐

  1. 戏(细)说Executor框架线程池任务执行全过程(下)

    上一篇文章中通过引入的一个例子介绍了在Executor框架下,提交一个任务的过程,这个过程就像我们老大的老大要找个老大来执行一个任务那样简单.并通过剖析ExecutorService的一种经典实现Th ...

  2. 自己实现内存操作函数memset(),memcmp(),memcpy(),memmove()

    1.memset()内存设置函数(初始化) void *my_memset(void* dest, int c, size_t count) { assert(dest != NULL); char  ...

  3. BZOJ 4690 Never Wait for Weights

    带权并查集23333333 注意dis[x]+=dis[fath[x]. #include<iostream> #include<cstdio> #include<cst ...

  4. 决定undo表空间的大小

    1.查询每秒最高需要的undo的数据块[每个块8k大小] sys)) from v$undostat; )) --------------------------------------------- ...

  5. csu 1326 The contest

    裸的   并查集  +  分组背包: #include<iostream> #include<cstring> #include<algorithm> #inclu ...

  6. 转深入学习heritrix---体系结构(Overview of the crawler)

    Heritrix采用了模块化的设计,它由一些核心类(core classes)和可插件模块(pluggable modules)构成.核心类可以配置,但不能被覆盖,插件模块可以被由第三方模块取代. ( ...

  7. 七:zookeeper与paxos的分析

    zookeeper是什么 官方说辞:Zookeeper 分布式服务框架是Apache Hadoop 的一个子项目,它主要是用来解决分布式应用中经常遇到的一些数据管理问题,如:统一命名服务.状态同步服务 ...

  8. 两天三场Java实习生面试总结

    Java 关键字(如abstract)[详解] String[相关面试题] String.StringBuffer.StringBuilder区别 String中有没有使一个字符串反转的方法 线程的实 ...

  9. css3 :nth-child 常用用法

    前端的哥们想必都接触过css中一个神奇的玩意,可以轻松选取你想要的标签并给与修改添加样式,是不是很给力,它就是“:nth-child”. 下面我将用几个典型的实例来给大家讲解:nth-child的实际 ...

  10. 定制Bootstrap遇到无法下载的解决——Blob下载注意事项

    今天定制bootstrap(在这里),全部的勾都选过了,于是兴高采烈地点击“编译并下载”.等了一会儿,迅雷7跳出来了“新建下载任务”,但是它居然说这个url不合法! url像这样: blob:http ...