Codeforces Round #367 (Div. 2)D. Vasiliy's Multiset (字典树)
D. Vasiliy's Multiset
4 seconds
256 megabytes
standard input
standard output
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:
- "+ x" — add integer x to multiset A.
- "- 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.
- "? 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.
10
+ 8
+ 9
+ 11
+ 6
+ 1
? 3
- 8
? 3
? 8
? 11
11
10
14
13
After first five operations multiset A contains integers 0, 8, 9, 11, 6 and 1.
The answer for the sixth query is integer — maximum among integers
,
,
,
and
.
得学姐指导----涉及到XOR的建树都建字典树。
哇哈哈~
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
const int maxn = 5e6+;
struct node
{
int next[];
int v;
};
node tree[maxn];
int sz = ;
void build(int x,int v)
{
int root = ;
for(int i=;i>=;i--)
{
int id = (x>>i)&;
if(tree[root].next[id]==)
{
memset(tree[sz].next,,sizeof(tree[sz].next));
tree[sz].v = ;
tree[root].next[id] = sz++;
}
root = tree[root].next[id];
tree[root].v+=v;
}
}
void match(int x)
{
int root = ;
x = ~x;
int ans = ;
for(int i=;i>=;i--)
{
ans *= ;
int id = (x>>i)&;
if(tree[root].next[id]&&tree[tree[root].next[id]].v)
{
ans++;
root = tree[root].next[id];
}
else
{
root = tree[root].next[-id];
}
}
printf("%d\n",ans);
}
int main()
{
int n,x;
char s[];
cin>>n; /* for(int i=0;i<=maxn-1;i++)
{
tree[i].v = 0;
memset(tree[i].next,0,sizeof(tree[i].next));
}*/
build(,);
for(int i=;i<=n;i++)
{
scanf("%s %d",s,&x);
if(s[]=='+')
{
build(x,);
}
else if(s[]=='-')
{
build(x,-);
}
else
{
match(x);
}
}
return ;
}
Codeforces Round #367 (Div. 2)D. Vasiliy's Multiset (字典树)的更多相关文章
- 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 ...
- Codeforces Round #367 (Div. 2) D. Vasiliy's Multiset
题目链接:Codeforces Round #367 (Div. 2) D. Vasiliy's Multiset 题意: 给你一些操作,往一个集合插入和删除一些数,然后?x让你找出与x异或后的最大值 ...
- 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 ...
- Codeforces Round #367 (Div. 2) D. Vasiliy's Multiset Trie
题目链接: http://codeforces.com/contest/706/problem/D D. Vasiliy's Multiset time limit per test:4 second ...
- 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 ...
- Codeforces Round #367 (Div. 2)---水题 | dp | 01字典树
A.Beru-taxi 水题:有一个人站在(sx,sy)的位置,有n辆出租车,正向这个人匀速赶来,每个出租车的位置是(xi, yi) 速度是 Vi;求人最少需要等的时间: 单间循环即可: #inclu ...
- Codeforces Round #367 (Div. 2) D. Vasiliy's Multiset(01字典树求最大异或值)
http://codeforces.com/contest/706/problem/D 题意:有多种操作,操作1为在字典中加入x这个数,操作2为从字典中删除x这个数,操作3为从字典中找出一个数使得与给 ...
- 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 #291 (Div. 2) C. Watto and Mechanism [字典树]
传送门 C. Watto and Mechanism time limit per test 3 seconds memory limit per test 256 megabytes input s ...
随机推荐
- 剑指offer 旋转数组
class Solution { public: int minNumberInRotateArray(vector<int> rotateArray) { //常规的遍历方法时间是O(N ...
- Linux(centos5.0+)unison+inotify-tools触发式双向自动同步
192.168.1.11是server1, 192.168.1.22是server2. [1]安装inotify-tools 各大linux发行版本都有inotify-tools软件包,建议通过y ...
- 【jsp exception】如何处理jsp页面的错误
根据jsp对错误的处理方式不同可以将其分为局部异常处理和全局异常处理.局部异常处理适用于个别jsp页面,当这些页面发生错误后,采取特殊的处理方式:全局异常处理适用于所有jsp页面,当所有页面发生某些指 ...
- IT项目各阶段管理
- oracle中的exists 和in
有两个简单例子,以说明 “exists”和“in”的效率问题 1) select * from T1 where exists(select 1 from T2 where T1.a=T2.a) ; ...
- Android NDK and OpenCV Development With Android Studio
Android NDK and OpenCV Development With Android Studio Hujiawei 172 2014年10月22日 发布 推荐 0 推荐 收藏 4 收藏,5 ...
- Break on _NSLockError() to debug.
*** -[NSCondition dealloc]: condition (<NSCondition: 0x1039a450> '(null)') deallocated while s ...
- android cts 命令的说明
Host help showthis message 帮助文档 exit exitcts command line 退出CTS ls 全部用l替代,--plan直接用p替代,也即 l p .其他类似 ...
- maven实战_测试覆盖率插件使用
原文:http://www.cnblogs.com/yucongblog/p/5297051.html 1.环境准备 <project> ... <reporting> < ...
- Javascript模块化编程:AMD规范及require.js用法【转】 - loheonly的笔记 - 前端网(W3Cfuns)
http://www.w3cfuns.com/blog-5425789-5399326.html