Codeforces Round #367 (Div. 2) D. Vasiliy's Multiset(可持久化Trie)
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
.
题目链接:CF 706D
字典树水题,只是最近在膜可持久化字典树,拿这道题当模版写了一下,感觉跟主席树非常像,毕竟都是可持久化的思想,新建节点信息代替更改节点信息,写法也简单明了如果会主席树的话代码是不难理解的,只是最后返回的值是异或过的值,因为一个节点不再是模拟地去初始化、连接到链表尾,而是直接把节点的位置拿来标记为已使用,把信息复制到对应节点上去,因此需要一个变量cnt来记录这条路径有没有被覆盖过或者说有没有被使用过,有的话说明这条路到底是存在一个数,即用普通字典树的话来说就是这个点不为NULL,可以往下走。滋瓷范围内的Xor最大值的贪心查询
代码:
#include <bits/stdc++.h>
using namespace std;
const int N=200010;
struct Trie
{
int nxt[2];
int cnt;
};
Trie L[N*34];
int tot;
int root[N]; void init()
{
memset(L,0,sizeof(L));
tot=0;
}
void update(int &cur,int ori,int step,int n,int v)
{
cur=++tot;
L[cur]=L[ori];
L[cur].cnt+=v;
if(step<0)
return ;
int t=(n>>step)&1;
update(L[cur].nxt[t],L[ori].nxt[t],step-1,n,v);
}
int Find(int S,int E,int step,int n)
{
if(step<0)
return 0;
int t=(n>>step)&1;
if(L[L[E].nxt[t^1]].cnt-L[L[S].nxt[t^1]].cnt>0)
return (1<<step)+Find(L[S].nxt[t^1],L[E].nxt[t^1],step-1,n);
else
return Find(L[S].nxt[t],L[E].nxt[t],step-1,n);
}
int main(void)
{
int n,x,i;
char ops[3]; while (~scanf("%d",&n))
{
init();
update(root[1],root[0],31,0,1);
int op=1;
int sz=0;
for (i=1; i<=n; ++i)
{
scanf("%s",ops);
if(ops[0]=='+')
{
++sz;
++op;
scanf("%d",&x);
update(root[op],root[op-1],31,x,1);
}
else if(ops[0]=='-')
{
--sz;
++op;
scanf("%d",&x);
update(root[op],root[op-1],31,x,-1);
}
else
{
scanf("%d",&x);
if(!sz)
{
printf("%d\n",x);
continue;
}
printf("%d\n",Find(root[0],root[op],31,x));
}
}
}
}
Codeforces Round #367 (Div. 2) D. Vasiliy's Multiset(可持久化Trie)的更多相关文章
- 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 (字典树)
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 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(01字典树求最大异或值)
http://codeforces.com/contest/706/problem/D 题意:有多种操作,操作1为在字典中加入x这个数,操作2为从字典中删除x这个数,操作3为从字典中找出一个数使得与给 ...
- Codeforces Round #367 (Div. 2) C. Hard problem(DP)
Hard problem 题目链接: http://codeforces.com/contest/706/problem/C Description Vasiliy is fond of solvin ...
- Codeforces Round #367 (Div. 2) B. Interesting drink (模拟)
Interesting drink 题目链接: http://codeforces.com/contest/706/problem/B Description Vasiliy likes to res ...
- Codeforces Round #367 (Div. 2) A. Beru-taxi (水题)
Beru-taxi 题目链接: http://codeforces.com/contest/706/problem/A Description Vasiliy lives at point (a, b ...
随机推荐
- http://www.cnblogs.com/itsource/p/4266905.html
http://www.cnblogs.com/itsource/p/4266905.html
- Sprint回顾_团队听诊器
- SQL Prompt激活
SQL脚本越写越多,总是觉得编写效率太过于低下,这和打字速度无关.在我个人编写SQL脚本时,至少会把SQL的格式排列成易于阅读的,因为其他人会阅读到你的SQL,无论是在程序中或是脚本文件中,良好的排版 ...
- python安装setuptools
http://wenku.baidu.com/link?url=I-FCVFpHbP2oyCt1Gjb1X5xHk4P475dVU3j8rWd4b4VSuD-Wd86LdbC7bdYskZdtDfGK ...
- BZOJ 1029 & 丝帛贪心
题意: 小刚在玩JSOI提供的一个称之为“建筑抢修”的电脑游戏:经过了一场激烈的战斗,T部落消灭了所有z部落的入侵者.但是T部落的基地里已经有N个建筑设 施受到了严重的损伤,如果不尽快修复的话,这些建 ...
- The design of a distributed variant of Plato framework to support collaborated editing
A first thought system architecture (pulling mode) is one that the server doesn't keep client inform ...
- 【BZOJ】2078: [POI2004]WYS
题意: 给n个互不相交的多边形(边均平行于坐标轴),问最大深度.深度的定义是,若多边形A被多边形B包含,则\(dep[A]=max(dep[B])+1\).坐标系的深度为0.(n<=40000, ...
- linux命令之 top, free,ps
linux终端查看cpu和内存使用情况 t一.op进入全屏实时系统资源使用信息查看 PID:进程的ID USER:进程所有者 PR:进程的优先级别,越小越优先被执行 NInice:值 VIRT:进程占 ...
- php 实现推技术comet(转)
实现实时通信一般有两种方式:socket或comet.socket是比较好的解决方案,问题在于不是所有的浏览器都兼容,服务器端实现起来也稍微有点麻烦.相比之下,comet(基于HTTP长连接的&quo ...
- php归档函数(按时间)实现
今日开发本站需要用到按时间归档文章的功能,即按文档发布时间将文章文类,以实现检索和统计功能,于是自己写了一个, 现分享给大家,相信大家工作和学习中有可能会用到,实现原理很简单,即取出文章发布时间戳的年 ...