题目链接: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. JuQueen(线段树 lazy)

    JuQueen Time Limit: 5 Sec  Memory Limit: 512 MB Description Input Output Sample Input 10 10 5 state ...

  2. query判断值是否为空,针对前台提交数据的校验

    1.<input type="hidden" id="key" name="key" value="123"> ...

  3. [渣译文] 使用 MVC 5 的 EF6 Code First 入门 系列:实现基本的CRUD功能

    英文渣水平,大伙凑合着看吧…… 这是微软官方SignalR 2.0教程Getting Started with Entity Framework 6 Code First using MVC 5 系列 ...

  4. Jmockit使用

    引用单元测试中mock的使用及mock神器jmockit实践中的java单元测试中各种Mock框架对比,就能明白JMockit有多么强大: JMockit是基于JavaSE5中的java.lang.i ...

  5. js之oop <二> 对象属性

    js中对象属性可以动态添加和删除.删除对象属性用delete关键字. function obj(){ } var oo = new obj(); oo.a = "a"; oo.b ...

  6. vs 2005 使用 boost regex

    第一步: Boost 入门及其VS2005下编译boost库  boost.regex库安装指南  深入浅出之正则表达式(一)  C++中三种正则表达式比较(C regex,C ++regex,boo ...

  7. python之Excel操作

    #coding:utf-8 __author__ = 'similarface' import xlrd book=xlrd.open_workbook('/Users/similarface/Dow ...

  8. [讨论] 全新首发WIN7 32&64 WINSXS进一步精简批处理公测

    恶魔浮雕 发表于 2016-1-8 15:01:27 |https://www.itsk.com/forum.php?mod=viewthread&tid=362278&highlig ...

  9. C#日常总结1

    Rows:行的集合: Columns:列的集合: Gridview:用来显示数据的表格{ //设置 AutoGenerateColumns="false":表示不允许自动产生列,列 ...

  10. Supercell only provide the best games for players

    Supercell only provide the best games for players Supercell start to change all, Supercell's first t ...