题目链接:

  http://codeforces.com/contest/706/problem/D

题目大意

  三种操作,1.添加一个数,2.删除一个数,3.查询现有数中与x异或最大值。(可重复)

题目思路:

  【字典树】【贪心】

  维护一个字典树,左0右1。查询时从上往下走。

  

 //
//by coolxxx
//
#include<iostream>
#include<algorithm>
#include<string>
#include<iomanip>
#include<memory.h>
#include<time.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
//#include<stdbool.h>
#include<math.h>
#define min(a,b) ((a)<(b)?(a):(b))
#define max(a,b) ((a)>(b)?(a):(b))
#define abs(a) ((a)>0?(a):(-(a)))
#define lowbit(a) (a&(-a))
#define sqr(a) ((a)*(a))
#define swap(a,b) ((a)^=(b),(b)^=(a),(a)^=(b))
#define mem(a,b) memset(a,b,sizeof(a))
#define eps (1e-8)
#define J 10000000
#define MAX 0x7f7f7f7f
#define PI 3.1415926535897
#define N 5000004
using namespace std;
typedef long long LL;
int cas,cass;
int n,m,lll,ans;
int t[N];
int ch[N][];
char c[];
void add(int x)
{
int i,j,k=;
for(i=;i>=;i--)
{
j=((<<i)&x)>;
if(!ch[k][j])ch[k][j]=++lll;
k=ch[k][j];
t[k]++;
}
}
void del(int x)
{
int i,j,k=;
for(i=;i>=;i--)
{
j=((<<i)&x)>;
k=ch[k][j];
t[k]--;
}
}
int find(int x)
{
int i,j,k=;
ans=;
for(i=;i>=;i--)
{
j=((<<i)&x)==;
if(t[ch[k][j]])
{
ans|=(<<i);
k=ch[k][j];
}
else k=ch[k][-j];
}
return ans;
}
int main()
{
#ifndef ONLINE_JUDGE
// freopen("1.txt","r",stdin);
// freopen("2.txt","w",stdout);
#endif
int i,j,x;
// for(scanf("%d",&cas);cas;cas--)
// for(scanf("%d",&cas),cass=1;cass<=cas;cass++)
// while(~scanf("%s",s))
while(~scanf("%d",&n))
{
mem(ch,);
mem(t,);
lll=;
add();
for(i=;i<=n;i++)
{
scanf("%s%d",c,&x);
if(c[]=='+')
add(x);
else if(c[]=='-')
del(x);
else printf("%d\n",find(x));
}
}
return ;
}
/*
// //
*/

【字典树】【贪心】Codeforces 706D Vasiliy's Multiset的更多相关文章

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

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

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

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

  3. CodeForces 706D Vasiliy's Multiset

    字典树. 比较经典的题目了.把每一个数字都插入到字典树中,询问的时候如果$x$的第$i$位是$p$,那么尝试着在字典树上往$pXOR1$的节点走下去,没有$pXOR1$节点的话再走$p$的.删除操作的 ...

  4. HDU 4825 Xor Sum(经典01字典树+贪心)

    Xor Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 132768/132768 K (Java/Others) Total ...

  5. NEUOJ711 异星工厂 字典树+贪心

    题意:你可以收集两个不相交区间的权值,区间权值是区间异或,问这两个权值和最大是多少 分析:很多有关异或求最大的题都是利用01字典树进行贪心,做这个题的时候我都忘了...最后是看别人代码的时候才想起来这 ...

  6. HDU 5536 Chip Factory 字典树+贪心

    给你n个数,a1....an,求(ai+aj)^ak最大的值,i不等于j不等于k 思路:先建字典树,暴力i,j每次删除他们,然后贪心找k,再恢复i,j,每次和答案取较大的,就是答案,有关异或的貌似很多 ...

  7. 51nod 1526 分配笔名(字典树+贪心)

    题意: 班里有n个同学.老师为他们选了n个笔名.现在要把这些笔名分配给每一个同学,每一个同学分配到一个笔名,每一个笔名必须分配给某个同学.现在定义笔名和真名之间的相关度是他们之间的最长公共前缀.设笔名 ...

  8. hdu4825 01字典树+贪心

    从高位向低位构造字典树,因为高位得到的数更大. AC代码: #include<cstdio> using namespace std; typedef long long LL; cons ...

  9. ACM学习历程—POJ 3764 The xor-longest Path(xor && 字典树 && 贪心)

    题目链接:http://poj.org/problem?id=3764 题目大意是在树上求一条路径,使得xor和最大. 由于是在树上,所以两个结点之间应有唯一路径. 而xor(u, v) = xor( ...

随机推荐

  1. [Angualr 2] Using FormBuilder

    There are two main functions we’ll use on FormBuilder: • control - creates a new Control• group - cr ...

  2. Win7下Qt5.2中使用OpenGL的glu函数库无法使用的解决方案

          最近在Window7使用Qt5.2学习OpenGL时,出现了以OpenGL中glu开头的函数库无法使用的错误,例如: 'gluPerspective'  was not declared ...

  3. [转] 关于c++的头文件依赖

    http://www.cnblogs.com/yvesliao/p/3938730.html PS: 使用单向依赖 正在看google c++编程规范,里面对头文件依赖是这么说的: 1 2 3 4 5 ...

  4. Android运行时注解

    Android的注解有编译时注解和运行时注解,本文就介绍下运行时注解. 其实非常简单,直接上代码:本文主要是替代传统的findViewById()的功能,就是在我们Activity中不需要再使用fin ...

  5. js的定位实现和ip查询

    sina的api var GetLocationFromSina = function (successFunc, errorFunc) { $.getScript('http://int.dpool ...

  6. Modernizr——为HTML5和CSS3而生!

    原文地址:http://www.alistapart.com/articles/taking-advantage-of-html5-and-css3-with-modernizr/ 堂主译文地址:ht ...

  7. DataTable去重复方法

    //去掉重复行 DataTable table=new DataTable(); DataView dv = table.DefaultView; table = dv.ToTable(true, n ...

  8. TSQL Challenge 2

    和之前发布的TSQL Challenge 1是同一系列的文章,看到那篇学习哪篇,没有固定的顺序,只为锻炼下思维. Compare rows in the same table and group th ...

  9. 用DOM实现文章采集-HtmlAgilityPack实现html解析

    Html Agility Pack 是CodePlex 上的一个开源项目.它提供了标准的DOM API 和XPath 支持! 下载地址:http://htmlagilitypack.codeplex. ...

  10. 直接拿来用的15个jQuery代码片段

    1.预加载图片 1 2 3 4 5 6 7 8 9 10 11 12 (function($) {   var cache = [];   // Arguments are image paths r ...