【35.20%】【CF 706D】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
.
【题解】
如果题目没看懂就看一下上面那个note的样例解释嘛。应该知道是什么意思了吧。
然后我把样例的前6个操作数的二进制列举一下。
如果不足4位就前面补0
1000
1001
1011
0110
0001
询问0011
思路是这样的
把前面5个数字加入字典树中。
然后从根节点开始。看到询问的二进制第一位是0.
0的话xor什么最好呢?当然是1.
那就看看从根节点往下有没有一个1有就往下走,没有的话就走0(0是一直存在的。所以肯定可以走);然后二进制的第二位也以此类推。
最后我们走到底的肯定是最大值。
这里涉及到一个原理就是
(二进制)
1000 > 0111
即2^x > 2^(x-1)+2^(x-2) + ...+ 2^0
实际上左边那个等于右边加1.
节点加入和删除的话
给每个节点都设置一个值。走到这个点就增加一下这个节点的值。然后要删除的时候仍旧走一遍这个路。然后递减路上走过的节点的值。
这3个操作其实很像的。
然后10^9 二进制的长度为30
【代码】
#include <cstdio> const int MAX_SIZE = 7000000; int q, v[MAX_SIZE][2] = { 0 }, totn = 0, num[MAX_SIZE] = { 0 };
int a[40]; //v[t][0],v[t][1]分别表示t的左右儿子,左儿子代表0 void add(int x)
{
int temp = 0, temp1 = x;
a[0] = 30;
while (temp1 > 0) //从后往左加入二进制各个位
{
a[a[0]] = temp1 & 1;
temp1 = temp1 >> 1;
a[0]--;
}
for (int i = 1; i <= a[0]; i++)//1..a[0]是补0
{
if (v[temp][0] == 0)
v[temp][0] = ++totn;
temp = v[temp][0];
num[temp]++;
}
for (int i = a[0] + 1; i <= 30; i++)//a[0]+1..30才是这个数的二进制
{
if (v[temp][a[i]] == 0)
v[temp][a[i]] = ++totn;
temp = v[temp][a[i]];
num[temp]++;
}
} void de_lete(int x)
{
int temp = 0, temp1 = x;
a[0] = 30;
while (temp1 > 0)
{
a[a[0]] = temp1 & 1;
temp1 = temp1 >> 1;
a[0]--;
}
for (int i = 1; i <= a[0]; i++)
{
temp = v[temp][0];
num[temp]--;
}
for (int i = a[0] + 1; i <= 30; i++)
{
temp = v[temp][a[i]];
num[temp]--;
}
} int query(int x)
{
int temp = 0, temp1 = x;
a[0] = 30;
while (temp1 > 0)
{
a[a[0]] = temp1 & 1;
temp1 = temp1 >> 1;
a[0]--;
}
int leijia = 0;
for (int i = 1; i <= a[0]; i++)
if (v[temp][1] != -1 && num[v[temp][1]] > 0)
{
temp = v[temp][1];
leijia += 1 << (30 - i); //代表2^(30-i)
}
else
temp = v[temp][0];
for (int i = a[0] + 1; i <= 30; i++)
if (v[temp][1 - a[i]] != -1 && num[v[temp][1 - a[i]]] > 0)
{
temp = v[temp][1 - a[i]];
leijia += 1 << (30 - i);
}
else
temp = v[temp][a[i]];
return leijia;
} void input_data()
{
scanf("%d", &q);
for (int i = 1; i <= q; i++)
{
char op[5];
int x;
scanf("%s%d", op, &x);
if (op[0] == '+')
add(x);
else
if (op[0] == '-')
de_lete(x);
else
printf("%d\n", query(x));
}
} int main()
{
//freopen("F:\\rush.txt", "r", stdin);
add(0);
input_data();
return 0;
}
【35.20%】【CF 706D】Vasiliy's Multiset的更多相关文章
- codeforces 706D D. Vasiliy's Multiset(trie树)
题目链接: D. Vasiliy's Multiset time limit per test 4 seconds memory limit per test 256 megabytes input ...
- 【codeforces】【比赛题解】#851 CF Round #432 (Div.2)
cf真的难…… 点我浏览丧题. [A]Arpa和她对墨西哥人浪的研究 Arpa正在对墨西哥人浪进行研究. 有n个人站成一排,从1到n编号,他们从时刻0开始墨西哥人浪. 在时刻1,第一个人站起来.在时刻 ...
- JAVA 基础编程练习题20 【程序 20 求前 20 项之和】
20 [程序 20 求前 20 项之和] 题目:有一分数序列:2/1,3/2,5/3,8/5,13/8,21/13...求出这个数列的前 20 项之和. 程序分析:请抓住分子与分母的变化规律. pac ...
- 【字典树】【贪心】Codeforces 706D Vasiliy's Multiset
题目链接: http://codeforces.com/contest/706/problem/D 题目大意: 三种操作,1.添加一个数,2.删除一个数,3.查询现有数中与x异或最大值.(可重复) 题 ...
- 【CF 453A】 A. Little Pony and Expected Maximum(期望、快速幂)
A. Little Pony and Expected Maximum time limit per test 1 second memory limit per test 256 megabytes ...
- 【实战Java高并发程序设计6】挑战无锁算法:无锁的Vector实现
[实战Java高并发程序设计 1]Java中的指针:Unsafe类 [实战Java高并发程序设计 2]无锁的对象引用:AtomicReference [实战Java高并发程序设计 3]带有时间戳的对象 ...
- 【实战Java高并发程序设计 3】带有时间戳的对象引用:AtomicStampedReference
[实战Java高并发程序设计 1]Java中的指针:Unsafe类 [实战Java高并发程序设计 2]无锁的对象引用:AtomicReference AtomicReference无法解决上述问题的根 ...
- 【Android】【录音】Android录音--AudioRecord、MediaRecorder
[Android][录音]Android录音--AudioRecord.MediaRecorder Android提供了两个API用于实现录音功能:android.media.AudioRecord. ...
- 【转】从外行的视角尝试讲解为什么这回丰田栽了【全文完】【v1.01】
转自:http://club.tgfcer.com/thread-6817371-1-1.html [第一部分]背景简介 前几年闹得沸沸扬扬的丰田刹不住事件最近又有新进展.十月底俄克拉荷马的一次庭审 ...
随机推荐
- php实现遍历文件目录
php实现遍历文件目录 一.总结 1.熟悉简单:很经典的例子,多看,然后发现熟悉了很简单 二.php实现遍历目录 php实现遍历目录 代码一: //遍历目录 function iteral($path ...
- 【2017"百度之星"程序设计大赛 - 初赛(A)】今夕何夕
[链接]http://bestcoder.hdu.edu.cn/contests/contest_showproblem.php?cid=775&pid=1005 [题意] 在这里写题意 [题 ...
- 洛谷 P1679 神奇的四次方数
P1679 神奇的四次方数 题目描述 在你的帮助下,v神终于帮同学找到了最合适的大学,接下来就要通知同学了.在班级里负责联络网的是dm同学,于是v神便找到了dm同学,可dm同学正在忙于研究一道有趣的数 ...
- asp.net 查询sql数据表的网页模板
最近因为工作需求,要制作一个网页模板,主要是用于快速开发,可以查询Sql数据表信息的模板, 昨天做好了,这个只是一个Demo,但是功能已经齐全了, 开发新的网站时,需要新增一个xml,复制粘贴网页的前 ...
- [1,2,3].forEach(alert);这样的写法有什么利和弊吗?
以下这个问题遇到了之后.问了太阳神,以下是太阳神的解答: [1,2,3].forEach(alert);这样的写法有什么利和弊吗? 首先forEach使用方法非常easy降低代码量, 可是也有非常多地 ...
- TeamViewer的下载、安装和使用(windows7、CentOS6.5和Ubuntu14.04(64bit))(图文详解)
不多说,直接上干货! TeamViewr是远程支持.远程访问.在线协作和会议软件. 分为从windows7.CentOS6.5和Ubuntu14.04(64bit) 系统来详解下载.安装和初步使用! ...
- 记一些stl的用法(持续更新)
有些stl不常用真的会忘qwq,不如在这里记下来,以后常来看看 C++中substr函数的用法 #include<string> #include<iostream> usin ...
- Eclipse如何从SVN更新和上传修改部分项目
1:右击项目,选择team菜单,点击与资源库同步 2:第一个箭头表示别人改动的部分 3:右击,更新,将同事改动的部分同步到自己的项目里面 4:第二个指向右边的箭头表示自己修改的文件 5:右击,提交,将 ...
- CODEVS——T 1993 草地排水 USACO
http://codevs.cn/problem/1993/ 时间限制: 2 s 空间限制: 256000 KB 题目等级 : 钻石 Diamond 题解 查看运行结果 题目描述 De ...
- 学习easyui疑问(三)
今天我学习easyui中碰到的还有一问题是:怎样创建一个表格? 首先,在easyui中文官网上提供的这样一种定义方式: <!--table--> <table id="tt ...