CodeForces 706D Vasiliy's Multiset
字典树。
比较经典的题目了。把每一个数字都插入到字典树中,询问的时候如果$x$的第$i$位是$p$,那么尝试着在字典树上往$pXOR1$的节点走下去,没有$pXOR1$节点的话再走$p$的。删除操作的话可以记录一下每一个节点出现了几次,$Insert$的时候$s[p].cnt++$,$Delete$的时候$s[p].cnt--$,如果发现$s[p].cnt==0$,那么将以$p$为根的树全删了,也就是$p$的$father$到$p$的路标为$-1$。
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<iostream>
using namespace std;
typedef long long LL;
const double pi=acos(-1.0),eps=1e-;
void File()
{
freopen("D:\\in.txt","r",stdin);
freopen("D:\\out.txt","w",stdout);
} const int maxn=;
struct X { int nx[], cnt; }s[*maxn];
int n,x,h,sz; char op[]; int add()
{
sz++; s[sz].cnt=;
s[sz].nx[]=s[sz].nx[]=-;
return sz;
} void Insert(int x)
{
int p=;
for(int i=;i>=;i--)
{
if((<<i)&x) h=; else h=;
if(s[p].nx[h]==-) s[p].nx[h]=add();
p=s[p].nx[h]; s[p].cnt++;
}
} void Delete(int x)
{
int p=;
for(int i=;i>=;i--)
{
if((<<i)&x) h=; else h=;
int t=p; p=s[p].nx[h]; s[p].cnt--;
if(s[p].cnt==) s[t].nx[h]=-;
}
} int Find(int x)
{
int p=,res=;
for(int i=;i>=;i--)
{
if((<<i)&x) h=; else h=;
if(s[p].nx[h^]!=-) p=s[p].nx[h^],res=res+(<<i);
else if(s[p].nx[h]!=-)p=s[p].nx[h];
}
return res;
} int main()
{
scanf("%d",&n);
s[].nx[]=s[].nx[]=-; s[].cnt=;
sz=; Insert();
for(int i=;i<=n;i++)
{
scanf("%s%d",op,&x);
if(op[]=='+') Insert(x);
else if(op[]=='-') Delete(x);
else printf("%d\n",Find(x));
}
return ;
}
CodeForces 706D Vasiliy's Multiset的更多相关文章
- 【字典树】【贪心】Codeforces 706D Vasiliy's Multiset
题目链接: http://codeforces.com/contest/706/problem/D 题目大意: 三种操作,1.添加一个数,2.删除一个数,3.查询现有数中与x异或最大值.(可重复) 题 ...
- Codeforces 706D Vasiliy's Multiset(可持久化字典树)
[题目链接] http://codeforces.com/problemset/problem/706/D [题目大意] 要求实现一个集合中的三个操作,1:在集合中加入一个元素x,2:从集合中删除一个 ...
- CodeForces 706D Vasiliy's Multiset (字典树查询+贪心)
题意:最开始的时候有一个集合,集合里面只有一个元素0,现在有q次操作,操作分为3种: + x: 表示向集合中添加一个元素x - x:表示删除集合中值为x的一个元素 ? x:表示查询集合中与x异或的最大 ...
- 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 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 (0/1-Trie树)
Vasiliy's Multiset 题目链接: http://codeforces.com/contest/706/problem/D Description Author has gone out ...
- trie树 Codeforces Round #367 D Vasiliy's Multiset
// trie树 Codeforces Round #367 D Vasiliy's Multiset // 题意:给一个集合,初始有0,+表示添加元素,-去除元素,?询问集合里面与x异或最大的值 / ...
- 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 Trie
题目链接: http://codeforces.com/contest/706/problem/D D. Vasiliy's Multiset time limit per test:4 second ...
随机推荐
- VIJOS1107 求树的最长链
vijos1107环游大同80天 学习了一下求树的最长链的方法 最简单的思路就是两次dfs 两次dfs分别有什么用呢? 第一次dfs,求出某个任意的点能到达的最远的点 第二次dfs,从所搜到的最远的点 ...
- Vnix项目正式启动
历经3年的学习时间,我从Puppy Linux到各种常见的Linux发行版,从Gentoo Linux再到LFS,期间学会了LiveCD.中文化定制.服务器搭建.Google Key Search.C ...
- web前端学习笔记
web前端学习笔记(CSS盒子的定位) 相对定位 使用相对定位的盒子的位置常以标准流的排版方式为基础,然后使盒子相对于它在原本的标准位置偏移指定的距离.相对定位的盒子仍在标准流中,它后面的盒子仍以标准 ...
- npm ERR! Error: socket hang up
when i use npm to install express, it goes this message: npm info it worked if it ends with ok npm i ...
- ASP.NET Web API 2.0新特性:Attribute Routing1
ASP.NET Web API 2.0新特性:Attribute Routing[上篇] 对于一个针对ASP.NET Web API的调用请求来说,请求的URL和对应的HTTP方法的组合最终决定了目标 ...
- C# IE代理操作
public class IPProxy { [System.Runtime.InteropServices.DllImport("wininet.dll", SetLastErr ...
- C#开发 “因为某项目未能生成,所以无法发布”
今天把笔记本电脑中开发的项目复制到台式机上,启用调试都正常.准备发布的时候却提示“因为某项目未能生成,所以无法发布”的错误. 从网上查找资料可以通过以下方法解决: 在项目属性的签名标签中,创建测试证书 ...
- Arduino 各种模块篇 RGB LED灯
示例代码: 类似与这样的led,共阴rgb led,通过调节不同的亮度,组合成不同的颜色. 示例代码: /* 作者:极客工坊 时间:2012年12月18日 IDE版本号:1.0.1 发布地址:www. ...
- asp.net中的路由系统
ASP.NET MVC重写了ASP.NET管道HttpModule和处理程序HttpHandler.MVC自定义了MvcHandler实现了Controller的激活和Action的执行.但是在请求到 ...
- HDOJ 1755 - A Number Puzzle 排列数字凑同余,状态压缩DP
dp [ x ] [ y ] [ z ] 表示二进制y所表示的组合对应的之和mod x余数为z的最小数... 如可用的数字为 1 2 3 4...那么 dp [ 7 ] [ 15 ] [ 2 ] = ...