题目链接: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. C语言之移位操作

    C语言很多操作都是以字节为单位进行的,但有时为了节约空间,很多系统程序中要求在比特位级别进行运算处理.C语言一同提供了六种位运算的运算符,分别为&(按位与),|(按位或),~(按位取反),^( ...

  2. BZOJ 3631 松鼠的新家

    链剖. #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> ...

  3. UVa725 - Division

    #include<cstdio> #include<cstring> #include<algorithm> using namespace std; ; int ...

  4. Android Studio 学习 - HelloWorld

    今天是学习Android Studio的第2天,加油! 1. 首先要记录下使用Android Studio的一个代码自动完成的功能.平常基本上用Delphi,乍一换工具,各种不习惯,或者说不熟悉.按照 ...

  5. 【转】session setup failed: NT_STATUS_LOGON_FAILURE -- 不错

    原文网址:http://blog.sina.com.cn/s/blog_5cdb72780100l26f.html samba服务器出现“session setup failed: NT_STATUS ...

  6. 【转】在Eclipse中使用PyDev进行Python开发

    原文网址:http://www.crifan.com/eclipse_use_pydev_develop_python/ 在折腾: [记录]使用Python的IDE:Eclipse+PyDev 的过程 ...

  7. 安卓 Input Events(输入事件)

    在安卓中,有不止一种方法从你的应用截取用户交互事件.在你的用户界面中考虑事件,途径就是从用户界面中的一个指定的view对象中捕获事件.该view提供了这样做的方法. 在你用来组成你布局的不同的view ...

  8. Hadoop1.1.2伪分布式安装笔记

    一.设置Linux的静态IP 修改桌面图标修改,或者修改配置文件修改 1.先执行ifconfig,得到网络设备的名称eth0 2.编辑/etc/sysconfig/network-scripts/if ...

  9. ubuntu中flash的中文乱码解决方法

    ubuntu装好之后, 为浏览器firefox安装flash插件, 后来发现中文会变成方框. 如何解决? 输入:cd /etc/fonts/conf.d/ 为了安全,备份一下: sudo cp 49- ...

  10. 10分钟搞定react-router

    1.路由的安装: $ npm install -S react-router 2.引入路由文件 import {Router, Route, browserHistory} from 'react-r ...