题目链接: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. 使用GIt向github上传代码

    github是一个基于git的代码托管平台,付费用户可以建私人仓库,我们一般的免费用户只能使用公共仓库,也就是代码要公开.这对于一般人来说公共仓库就已经足够了. 1.注册账户以及创建仓库     要想 ...

  2. 【Unity】常用代码

    //父子节点相关的: parent 变量表示Transform的父节点 root 表示它的根节点,如果没有父节点,它会返回自己 //根据名字查找子节点 Transform Find(string na ...

  3. GridControl

    隐藏Drag a column header here to group by that column https://www.devexpress.com/Support/Center/Questi ...

  4. 提取data.frame中的部分数据(不含列标题和行标题)

    ?unlist     Given a list structure x, unlist simplifies it to produce a vector which contains all th ...

  5. linux下启动AP热点时出错

    1.启动hostapd,在终端下输入sudo ./hostapd hostapd.conf (注意:使用到的hostapd和hostapd.conf都处在当前工作目录下) 1.2.在执行1之后会出现以 ...

  6. php : 匿名函数(闭包) [二]

    摘自: http://www.cnblogs.com/yjf512/archive/2012/10/29/2744702.html php的闭包(Closure)也就是匿名函数.是PHP5.3引入的. ...

  7. javascript 函数声明和函数表达式的区别(学习笔记)

    javascript中声明函数的方法有两种:函数声明式和函数表达式. 区别如下: 1).以函数声明的方法定义的函数,函数名是必须的,而函数表达式的函数名是可选的. 2).以函数声明的方法定义的函数,函 ...

  8. const char* && string && String^ 类型转换

    const char* && string && String^ 类型转换 const char* ---> string const char * cw= &q ...

  9. Window 常用文件

    *.msm ntwdblib.dll是一款用于PHP连接MSSQL2005或2008的驱动文件,如果连接BDE的时候出现“cannot load an idapi service liabray fi ...

  10. D3.js 做一个简单的图表(条形图)

    柱形图是一种最简单的可视化图标,主要有矩形.文字标签.坐标轴组成. 本文为简单起见,只绘制矩形的部分,用以讲解如何使用 D3 在 SVG 画布中绘图. 一. 画布是什么 前几章的处理对象都是 HTML ...