codeforces 706D (字典树)
题目链接: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 (字典树)的更多相关文章
- E - Petya and Exam CodeForces - 832B 字典树+搜索
E - Petya and Exam CodeForces - 832B 这个题目其实可以不用字典树写,但是因为之前写过poj的一个题目,意思和这个差不多,所以就用字典树写了一遍. 代码还是很好理解的 ...
- Watto and Mechanism CodeForces - 514C (字典树,哈希)
大意: 给定字符串集$S$, 每次询问给出字符串$a$, 求$S$中是否存在一个字符串恰好与$a$相差一个字符. 直接建字典树暴力复杂度是$O(n\sqrt{n})$, 也可以用set维护所有哈希值, ...
- Vitya and Strange Lesson CodeForces - 842D 字典树+交换节点
题意: Today at the lesson Vitya learned a very interesting function - mex. Mex of a sequence of number ...
- 【字典树】【贪心】Codeforces 706D Vasiliy's Multiset
题目链接: http://codeforces.com/contest/706/problem/D 题目大意: 三种操作,1.添加一个数,2.删除一个数,3.查询现有数中与x异或最大值.(可重复) 题 ...
- Codeforces 706D Vasiliy's Multiset(可持久化字典树)
[题目链接] http://codeforces.com/problemset/problem/706/D [题目大意] 要求实现一个集合中的三个操作,1:在集合中加入一个元素x,2:从集合中删除一个 ...
- CodeForces 706D Vasiliy's Multiset (字典树查询+贪心)
题意:最开始的时候有一个集合,集合里面只有一个元素0,现在有q次操作,操作分为3种: + x: 表示向集合中添加一个元素x - x:表示删除集合中值为x的一个元素 ? x:表示查询集合中与x异或的最大 ...
- 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 ...
- 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 ...
- Codeforces 455B A Lot of Games(字典树+博弈)
题目连接: Codeforces 455B A Lot of Games 题目大意:给定n.表示字符串集合. 给定k,表示进行了k次游戏,然后是n个字符串.每局開始.字符串为空串,然后两人轮流在末尾追 ...
随机推荐
- 2015弱校联盟(1) - I. Travel
I. Travel Time Limit: 3000ms Memory Limit: 65536KB The country frog lives in has n towns which are c ...
- CentOS 7下关于systemd的一些唠叨话一:systemd的特点和使用
摘要 近年来,Linux 系统的 init 进程经历了两次重大的演进,传统的 sysvinit 已经逐渐淡出历史舞台,新的 UpStart 和 systemd 各有特点,越来越多的 Linux 发行版 ...
- PHP操作MySQL的常用函数
某些情况下(如html中),调用php的变量时,要给变量加{},若要使字符串变量加上引号,则还需要在{}外加引号 如: $sql="select * from admin where use ...
- 【转】IP分片和TCP分段
ZC: 由于 TCP中 MSS 的关系,TCP 不会造成 IP分片和TCP分段 ! 1.http://zhidao.baidu.com/link?url=YCnR8B-1EN4-cgauRtwa-iV ...
- golang strings
package main import s "strings" //别名 import ( "fmt" ) var p = fmt.Println func m ...
- Oracle 优化 - CPU 问题
作为 OLTP 应用,一般不太有 CPU 问题,比较少 - 毕竟大多数问题都是 IO 引起:但是偶尔也会有. 问题判断 很简单 - OS 出现 CPU 很高的问题,持续高于 90% 应用可能会表现慢 ...
- Android Studio编译输出apk文件修改文件名
新建一个Android工程,默认编译会生成一个叫app-debug.apk或者叫app-release.apk文件,说实话,单纯看文件名,我都不到任何有用的信息,我希望输出的文件名是这样的: 模块名- ...
- mac 文本编辑器 文本编码Unicode utf-8 不适用的问题
在mac上使用默认的文本编辑器打开下载的xx.txt文件,如果文本是gbk的编码可能会出现 文本编码Unicode utf-8 不适用的打开错误,如下图 解决方式: 文本编辑---偏好设置-----打 ...
- C/C++ http协议加载sessionID
很多时候,使用一个既定的API虽然可以去完成一些任务,但是还是不如自己写来的那么随性和自由. http协议,最常用的get,post两种方式传输数据,关于post,有时候,需要用到加载cookie信息 ...
- Octopus系列之UploadValues异步上载
不多说了直接上代码 public void ProcessRequest(HttpContext context) { context.Response.ContentType = "tex ...