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

题意:q次操作,可以向多重集中增添,删除,询问异或最大值。

思路:转化为二进制用字典树存储,数字从高位开始,并全部固定位30位。

#include<bits/stdc++.h>
using namespace std;
const int N=2e5 + 5;
int now = 1 ,Trie[N<<5][2] ,num[N<<5];
void Insert(int x)
{
for(int i = 29 ,cur = 1 ; i >= 0 ;i--)
{
int tmp=(x >> i) & 1;
if(!Trie[cur][tmp])
Trie[cur][tmp] = ++now;
cur = Trie[cur][tmp];
num[cur]++;
}
}
void Delete(int x)
{
for(int i = 29 ,cur = 1 ;i >= 0 ;i--)
{
cur = Trie[cur][(x>>i)&1];
num[cur]--;
}
}
int query(int x)
{
int ans=0;
for(int i = 29 ,cur = 1 ;i >= 0 ;i--)
{
int tmp = (x >> i) & 1;
if(num[Trie[cur][tmp^1]])
{
ans += (1<<i);
cur = Trie[cur][tmp^1];
}
else
cur = Trie[cur][tmp];
}
return ans;
}
int main()
{
int q;
scanf("%d",&q);
Insert(0);
char c;
while(q--)
{
int x;
scanf(" %c %d" ,&c ,&x);
if(c == '+')
Insert(x);
if(c == '-')
Delete(x);
if(c == '?')
printf("%d\n" ,query(x));
}
return 0;
}

关于字典树:http://blog.csdn.net/u011787119/article/details/46991691

codeforces 706D (字典树)的更多相关文章

  1. E - Petya and Exam CodeForces - 832B 字典树+搜索

    E - Petya and Exam CodeForces - 832B 这个题目其实可以不用字典树写,但是因为之前写过poj的一个题目,意思和这个差不多,所以就用字典树写了一遍. 代码还是很好理解的 ...

  2. Watto and Mechanism CodeForces - 514C (字典树,哈希)

    大意: 给定字符串集$S$, 每次询问给出字符串$a$, 求$S$中是否存在一个字符串恰好与$a$相差一个字符. 直接建字典树暴力复杂度是$O(n\sqrt{n})$, 也可以用set维护所有哈希值, ...

  3. Vitya and Strange Lesson CodeForces - 842D 字典树+交换节点

    题意: Today at the lesson Vitya learned a very interesting function - mex. Mex of a sequence of number ...

  4. 【字典树】【贪心】Codeforces 706D Vasiliy's Multiset

    题目链接: http://codeforces.com/contest/706/problem/D 题目大意: 三种操作,1.添加一个数,2.删除一个数,3.查询现有数中与x异或最大值.(可重复) 题 ...

  5. Codeforces 706D Vasiliy's Multiset(可持久化字典树)

    [题目链接] http://codeforces.com/problemset/problem/706/D [题目大意] 要求实现一个集合中的三个操作,1:在集合中加入一个元素x,2:从集合中删除一个 ...

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

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

  7. Codeforces Round #311 (Div. 2) E. Ann and Half-Palindrome 字典树/半回文串

    E. Ann and Half-Palindrome Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contes ...

  8. Codeforces Round #311 (Div. 2) E - Ann and Half-Palindrome(字典树+dp)

    E. Ann and Half-Palindrome time limit per test 1.5 seconds memory limit per test 512 megabytes input ...

  9. Codeforces 455B A Lot of Games(字典树+博弈)

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

随机推荐

  1. “stdafx.h”: No such file or directory

    “stdafx.h”: No such file or directory 一般原因是建工程的时候选择了空工程,然后添加现有源文件(含stdafx.cpp) 或者 修改了已有的stdafx.cpp 或 ...

  2. C++文本的读取和写入

    #include <iostream> #include <sstream> #include <fstream> #include <string> ...

  3. TortoiseGit 连接每次都要输入用户名和密码

    当你配置好git后,在C:\Documents and Settings\Administrator\ 或者  C:\Users\Administrator 目录下有一个 .gitconfig 的文件 ...

  4. R之data.table速查手册

    R语言data.table速查手册 介绍 R中的data.table包提供了一个data.frame的高级版本,让你的程序做数据整型的运算速度大大的增加.data.table已经在金融,基因工程学等领 ...

  5. HDU-4526 威威猫系列故事——拼车记 动态规划

    分析:该题有2个地方要注意:所有的车要么不坐要么就坐满,这个贪心策略很容易证明是正确的,还有一点就是最后一辆车除外. #include <cstdlib> #include <cst ...

  6. 机器学习职位 & 面经

    http://www.cnblogs.com/peileyuan/p/4775453.html <浅谈机器学习的职业发展方向> http://blog.sina.com.cn/s/blog ...

  7. bootstrap selectpicker

    mark 一下使用 bootstrap selectpicker 遇到的一个小issue,作为下次查错使用 $('.selectpicker').selectpicker('val', 'Mustar ...

  8. TextView 行数控制

    控制textview只显示 2 行文本,超过 2 行显示展开/折叠按钮(mShowImage),isShow=true时,是展开状态,isShow=false时,是折叠状态 方法一: int line ...

  9. ASP.NET 配置KindEditor文本编辑器

    ASP.NET 配置KindEditor文本编辑器 跟着这篇博客做了两个小时,只搞出了下面这么个东西. 时间浪费在了原博客里这样的一句话:将kindeditor/asp.net/bin/LitJSON ...

  10. BBC.万物与虚无.Everything.and.Nothing

    这么有意思的纪录片怎么能错过 待续~