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年江西理工大学C语言程序设计竞赛(高级组)
A 解法:DP+二分 dp[i]=max(dp[i],dp[j]+p[i].v)(i>j) dp[i]表示建立i点之后能够获得的最大值 int n,M; struct node { int l, ...
- 3D数学基础学习之向量一
向量-数学定义 对数学家而言,向量就是一个数字列表,对程序员而言则是另一种相似的概念,数组. 向量-几何定义 a.向量的大小就是向量的长度(模),向量的长度非负 b.向量的方向描述了空间中向量的指向. ...
- css 打字动画
使用 css 将文字逐字打出 <h1>css is awesome</h1> 要使<h1>标签里的文字逐字打出,对应的样式如下: h1{ width: 14ch;/ ...
- Jeff Dean
"--出自"关于 Jeff Dean 的事实" 其实,"关于 Jeff Dean 的事实"这个G+ 帖中描述的并非是真实的.不过有人大费周折为他建立了 ...
- anaconda win10安装报错:UnicodeDecodeError解决方法
Traceback (most recent call last): File , in <module> import conda.cli File , in <module> ...
- ArcGIS10.2中文版破解教程
ArcGIS10.2中文版前些时间早就出炉了,下载了但是一直没有安装,听说了ArcGIS10.2云处理能力和影像处理能力都增强了!网上经常遇到一些朋友安装失败的问题,现在特此做一个教程!分享一下安装成 ...
- 【leetcode❤python】 396. Rotate Function
#-*- coding: UTF-8 -*- #超时# lenA=len(A)# maxSum=[]# count=0# while count ...
- redhat6.3安装MySQL-server-5.6.13-1.el6.x86_64.rpm
redhat6.3安装MySQL-server-5.6.13-1.el6.x86_64.rpm 首先下载下面三个文件: [plain] MySQL-client-5.6.13-1.el6.x ...
- 如何设置WIN10任务栏
1.鼠标右键点击任务栏 然后点击锁定任务栏,去掉前面的钩 2.然后鼠标右击任务栏 选择工具栏 点击新建工具栏 3.打开后点击新建文件夹,可以重命名,然后选择刚才新建的文件夹 4.此时任务栏就有了 5. ...
- Android Activity的加载模式和onActivityResult方法之间的冲突
前言 今天在调试程序时,发现在某一Activity上点击返回键会调用该Activity的onActivityResult()方法.我一开始用log,后来用断点跟踪调试半天,还是百思不得其解.因为之前其 ...