codeforces 706D 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
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <bits/stdc++.h>
#include <stack>
#include <map> using namespace std; #define For(i,j,n) for(int i=j;i<=n;i++)
#define mst(ss,b) memset(ss,b,sizeof(ss)); typedef long long LL; template<class T> void read(T&num) {
char CH; bool F=false;
for(CH=getchar();CH<'0'||CH>'9';F= CH=='-',CH=getchar());
for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar());
F && (num=-num);
}
int stk[70], tp;
template<class T> inline void print(T p) {
if(!p) { puts("0"); return; }
while(p) stk[++ tp] = p%10, p/=10;
while(tp) putchar(stk[tp--] + '0');
putchar('\n');
} const LL mod=1e9+7;
const double PI=acos(-1.0);
const int inf=1e18;
const int N=2e5+10;
const int maxn=5e3+4;
const double eps=1e-12; char s[100];
int x;
int ch[32*N][2],sz=1,val[32*N][2],temp[50];
void in(int num)
{
int u=0;
for(int i=31;i>=0;i--)
{
int c=temp[i];val[u][c]+=num;
if(!ch[u][c])
{
ch[sz][0]=ch[sz][1]=0;
ch[u][c]=sz++;
}
u=ch[u][c];
}
}
int query()
{
int u=0,ans=0;
for(int i=31;i>=0;i--)
{
int c=temp[i];
if(ch[u][c^1]&&val[u][c^1])
{
ans|=(1<<i);
u=ch[u][c^1];
}
else u=ch[u][c];
}
return ans;
} int main()
{
mst(temp,0);
in(1);
int q;
read(q);
while(q--)
{
scanf("%s%d",s,&x);
mst(temp,0);
int cnt=0;
while(x)
{
temp[cnt++]=x%2;
x>>=1;
}
if(s[0]=='+')in(1);
else if(s[0]=='-')in(-1);
else printf("%d\n",query());
}
return 0;
}
codeforces 706D D. Vasiliy's Multiset(trie树)的更多相关文章
- 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 706 D. Vasiliy's Multiset (字典树贪心)
题目链接:http://codeforces.com/contest/706/problem/D 题意很简单不多说. 把一个数当作二进制插入字典树中,按照xor的性质,1找0,0找1,贪心即可. 注意 ...
- 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 615C Running Track(DP + Trie树)
题目大概说给两个串,问最少要用多少个第一个串的子串(可以翻转)拼成第二个串. UVa1401,一个道理..dp[i]表示前缀i拼接成功所需最少的子串,利用第一个串所有子串建立的Trie树往前枚举转移. ...
- Codeforces 633C Spy Syndrome 2 【Trie树】+【DFS】
<题目链接> 题目大意:给定一个只有小写字母组成的目标串和m个模式串(里面可能有大写字母),记目标串反过来后的串为S,让你从m个模式串中选出若干个组成S串(不区分大小写).输出任意一种方案 ...
- Codeforces 514C Watto and Mechanism 【Trie树】+【DFS】
<题目链接> 题目大意:输入n个单词构成单词库,然后进行m次查询,每次查询输入一个单词(注意这些单词只由a,b,c构成),问该单词库中是否存在与当前查询的单词有且仅有一个字符不同的单词. ...
- CodeForces 706D Vasiliy's Multiset (字典树查询+贪心)
题意:最开始的时候有一个集合,集合里面只有一个元素0,现在有q次操作,操作分为3种: + x: 表示向集合中添加一个元素x - x:表示删除集合中值为x的一个元素 ? x:表示查询集合中与x异或的最大 ...
- 【35.20%】【CF 706D】Vasiliy's Multiset
time limit per test 4 seconds memory limit per test 256 megabytes input standard input output standa ...
随机推荐
- 1. WPF学习之概述
<深入浅出WPF> 前言: C#专业的朋友推荐的WPF入门书籍<深入浅出WPF>,没学过的朋友从今天开始和我一起开启WPF学习之旅吧! 什么是WPF? WPF 是windows ...
- 使用T4模板为EF框架添加数据库实体注释
网上有一个解决方法如下: http://www.cnblogs.com/stone_w/archive/2012/10/25/2738345.html 不过我试了下没解决太麻烦了 而且一旦EF要重新生 ...
- Android OOM解决方案 :
清单文件里 给Application标签加上android:largeHeap="true"这行代码 这样会给你的app分配一个大内存 如果某个页面在绘制时会耗非常多的内存 ...
- kindeditor浏览器兼容性问题
1.kindeditor在IE下出现异常“对象不支持“attachEvent”属性或方法” 通过开发人员工具会发现: 这时问题就很明了,也就是IE11版本不支持“attachEvent”; 解决方案: ...
- meaven环境变量配置
首先,先到官网去下载maven.这里是官网的地址:http://maven.apache.org/download.cgi 请选择最新的版本下载,这里咱们下载的是apache-maven-3.1.1 ...
- 关于js全局变量数组push数据时dom中无数据的问题
今天着实悲催,这问题整了好几个小时才解决.废话不多说,上问题. 一开始我定义了许多全局变量放在me下. var me = { dgOrderDetails: null, dgVisitNumbers: ...
- 镜像回源主要用于无缝迁移数据到OSS,即服务已经在自己建立的源站或者在其他云产品上运行,需要迁移到OSS上,但是又不能停止服务,此时可利用镜像回写功能实现。
管理回源设置_管理文件_开发指南_对象存储 OSS-阿里云 https://help.aliyun.com/document_detail/31865.html 通过回源设置,对于获取数据的请求以多种 ...
- stacked generalization 堆积正则化 堆积泛化 加权特征线性堆积
https://en.wikipedia.org/wiki/Ensemble_learning Stacking Stacking (sometimes called stacked generali ...
- ubuntu 下解决sublime v3 中文输入法时 退格键删除不了拼音的问题
ubuntu下,sulime想要支持中文需要这样设置: 1.安装中文输入解决的github git clone https://github.com/lyfeyaj/sublime-text-imfi ...
- win10+UEFI下u盘安装ubuntu16.04
本人电脑是华硕,由于要求使用linux所以安装: 1.首先给linux划出一个大分区,感觉最少50G: win10下磁盘管理,在最后的分区中压缩出50g,空间,其他的不用问了,也不用继续分区,一个大的 ...