题目链接:

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

D. Vasiliy's Multiset

time limit per test:4 seconds
memory limit per test:256 megabytes
#### 问题描述
> Author has gone out of the stories about Vasiliy, so here is just a formal task description.
>
> You are given q queries and a multiset A, initially containing only integer 0. There are three types of queries:
> 1. "+ x" — add integer x to multiset A.
> 2. "- x" — erase one occurrence of integer x from multiset A. It's guaranteed that at least one x is present in the multiset A before this query.
> 3. "? x" — you are given integer x and need to compute the value , i.e. the maximum value of bitwise exclusive OR (also know as XOR) of integer x and some integer y from the multiset A.
>
> Multiset is a set, where equal elements are allowed.

输入

The first line of the input contains a single integer q (1 ≤ q ≤ 200 000) — the number of queries Vasiliy has to perform.

Each of the following q lines of the input contains one of three characters '+', '-' or '?' and an integer xi (1 ≤ xi ≤ 109). It's guaranteed that there is at least one query of the third type.

Note, that the integer 0 will always be present in the set A.

输出

For each query of the type '?' print one integer — the maximum value of bitwise exclusive OR (XOR) of integer xi and some integer from the multiset A.

样例

sample input
10
+ 8
+ 9
+ 11
+ 6
+ 1
? 3
- 8
? 3
? 8
? 11 sample output
11
10
14
13

题意

维护一个multiset,支持插入删除,并且对于查询“? X”输出还在集合中的元素中与X最大的异或和。

题解

用trie树来维护集合,对于x,把它拆成32位二进制,然后按照从高位开始存到Trie里面,对于查询,只要在深搜Trie树的时候从高位开始故意往与x相反的方向跑就可以了。

代码

#include<map>
#include<cmath>
#include<queue>
#include<vector>
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define X first
#define Y second
#define mkp make_pair
#define lson (o<<1)
#define rson ((o<<1)|1)
#define mid (l+(r-l)/2)
#define sz() size()
#define pb(v) push_back(v)
#define all(o) (o).begin(),(o).end()
#define clr(a,v) memset(a,v,sizeof(a))
#define bug(a) cout<<#a<<" = "<<a<<endl
#define rep(i,a,b) for(int i=(a);i<(b);i++) typedef __int64 LL;
typedef vector<int> VI;
typedef pair<int,int> PII;
typedef vector<pair<int,int> > VPII; const int INF=1e9;
const LL INFL=0x3f3f3f3f3f3f3f3fLL;
const double eps=1e-8; //start---------------------------------------------------------------------- const int maxnode=1e7+10;
const int maxm=34; int arr[66],tot;
int ch[maxnode][2];
int val[maxnode],tag[maxnode];
struct Trie{
int sz;
Trie(){
sz=1; clr(ch[0],0);
clr(val,0);
clr(tag,-1);
}
void insert(int x,int type){
int tmp=x;
int u=0, n=maxm;
tot=0;
clr(arr,0);
while(x){ arr[tot++]=x%2; x/=2; }
for(int i=n-1;i>=0;i--){
int c=arr[i];
val[u]+=type;
if(!ch[u][c]){
clr(ch[sz],0);
val[sz]=0;
ch[u][c]=sz++;
}
u=ch[u][c];
}
val[u]+=type;
tag[u]=tmp;
}
int query(int x){
int tmp=x;
int ret=0;
int u=0, n=maxm;
tot=0;
clr(arr,0);
while(x){ arr[tot++]=x%2; x/=2; }
int su=1;
for(int i=n-1;i>=0;i--){
int c=arr[i];
// printf("c:%d\n",c);
// printf("val[%d]:%d\n",u,val[u]);
if(ch[u][c^1]&&val[ch[u][c^1]]>0){
u=ch[u][c^1];
}else if(ch[u][c]&&val[ch[u][c]]>0){
u=ch[u][c];
}
else{
su=0;
break;
}
}
if(su) ret=max(tmp,tag[u]^tmp);
else ret=tmp;
return ret;
}
}trie; int main() {
int q;
scanf("%d",&q);
while(q--){
char cmd[11]; int x;
scanf("%s%d",cmd,&x);
if(cmd[0]=='+') trie.insert(x,1);
else if(cmd[0]=='-') trie.insert(x,-1);
else if(cmd[0]=='?') printf("%d\n",trie.query(x));
}
return 0;
} //end-----------------------------------------------------------------------

Notes

对于xor的操作,十有八九是要拆位考虑,拆完之后可以考虑用线段树,Trie树之类的数据结构去维护。

Codeforces Round #367 (Div. 2) D. Vasiliy's Multiset Trie的更多相关文章

  1. Codeforces Round #367 (Div. 2) D. Vasiliy's Multiset trie树

    D. Vasiliy's Multiset time limit per test 4 seconds memory limit per test 256 megabytes input standa ...

  2. Codeforces Round #367 (Div. 2) D. Vasiliy's Multiset

    题目链接:Codeforces Round #367 (Div. 2) D. Vasiliy's Multiset 题意: 给你一些操作,往一个集合插入和删除一些数,然后?x让你找出与x异或后的最大值 ...

  3. Codeforces Round #367 (Div. 2) D. Vasiliy's Multiset (0/1-Trie树)

    Vasiliy's Multiset 题目链接: http://codeforces.com/contest/706/problem/D Description Author has gone out ...

  4. Codeforces Round #367 (Div. 2) D. Vasiliy's Multiset(可持久化Trie)

    D. Vasiliy's Multiset time limit per test 4 seconds memory limit per test 256 megabytes input standa ...

  5. Codeforces Round #367 (Div. 2)D. Vasiliy's Multiset (字典树)

    D. Vasiliy's Multiset time limit per test 4 seconds memory limit per test 256 megabytes input standa ...

  6. Codeforces Round #367 (Div. 2) D. Vasiliy's Multiset(01字典树求最大异或值)

    http://codeforces.com/contest/706/problem/D 题意:有多种操作,操作1为在字典中加入x这个数,操作2为从字典中删除x这个数,操作3为从字典中找出一个数使得与给 ...

  7. Codeforces Round #367 (Div. 2) C. Hard problem(DP)

    Hard problem 题目链接: http://codeforces.com/contest/706/problem/C Description Vasiliy is fond of solvin ...

  8. Codeforces Round #367 (Div. 2) B. Interesting drink (模拟)

    Interesting drink 题目链接: http://codeforces.com/contest/706/problem/B Description Vasiliy likes to res ...

  9. Codeforces Round #367 (Div. 2) A. Beru-taxi (水题)

    Beru-taxi 题目链接: http://codeforces.com/contest/706/problem/A Description Vasiliy lives at point (a, b ...

随机推荐

  1. JavaScript 中 Property 和 Attribute 的区别详解

    property 和 attribute非常容易混淆,两个单词的中文翻译也都非常相近(property:属性,attribute:特性),但实际上,二者是不同的东西,属于不同的范畴. property ...

  2. vue调用豆瓣API加载图片403问题

    "豆瓣API是有请求次数限制的”,这会引发图片在加载的时候出现403问题,视图表现为“图片加载不出来”,控制台表现为报错403. 其实是豆瓣限制了图片的加载,我自己用了一个办法把图片缓存下来 ...

  3. JSON API免费接口 各种提供JSON格式数据返回服务网站的API接口

    这里为大家搜集了一些能够返回JSON格式的服务接口.部分需要用JSONP调用. 电商接口 京东获取单个商品价格接口: http://p.3.cn/prices/mgets?skuIds=J_商品ID& ...

  4. 初识Pentaho(一)

    学习一门语言或工具,首先得知道该工具的用途是什么.Pentaho 的官方定义是一个集数据集成和数据分析于一体的平台.这样的解释还是有点模糊.还是看其有哪些特点吧:  ☐可以进行数据集成.谈到数据集成这 ...

  5. golang实现LRU,转载学习

    package main type LRUNode struct { key string val interface{} prev *LRUNode next *LRUNode } type LRU ...

  6. JavaScript预解析

    定义:JavaScript"预解析",可以理解为把变量或函数预先解析到它们被使用的环境中. 通俗点讲,即认为浏览器在正式运行JavaScript代码前, 第一步,会预先根据关键字v ...

  7. Java 高级应用编程 第一章 工具类

    一.Java API Java API简介 1.API (Application Programming Interface) 应用程序接口 2.Java中的API,就是JDK提供的各种功能的Java ...

  8. 20155335 俞昆 2016-2017-2 《Java程序设计》第九周学习总结

    学号 2016-2017-2 <Java程序设计>第九周学习总结 ##JDBC入门 在正式介绍JDBC前,已知JDBC是用来执行SQL的解决方案,开发人员使用JDBC的标准接口,开发人员不 ...

  9. 北京Uber优步司机奖励政策(1月5日)

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...

  10. 成都Uber优步司机奖励政策(8月31日~9月6日)

    本周(8月31日-9月6日),优步成都继续推出丰厚保底奖励,日保底总金额最高575元,每周保底最高可获得3595元.优步还加大了乘客端折扣力度,最低五折坐车!单子超多,上线就有单,接单接不停!欢迎各位 ...