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 ...
随机推荐
- Linux 安装、卸载程序
一, RPM 安装: rpm -ivh xxx.rpm 重新安装: rpm -ivh -replacepkgs xxx.rpm 卸载: rpm -e xxx.rpm 二,ta ...
- 浅谈js中的MVC
MVC是什么? MVC是一种架构模式,它将应用抽象为3个部分:模型(数据).视图.控制器(分发器) 本文将用一个经典的例子todoList来展开 一个事件发生的过程(通信单向流动): 1.用户在视图V ...
- cocos2d-x-lua基础系列教程五(lua单例)
lua-单例 function newAccount(initlizedBanlance) local self = {balance = initlizedBanlance} local show ...
- 淘宝数据库OceanBase SQL编译器部分 源代码阅读--解析SQL语法树
OceanBase是阿里巴巴集团自主研发的可扩展的关系型数据库,实现了跨行跨表的事务,支持数千亿条记录.数百TB数据上的SQL操作. 在阿里巴巴集团下,OceanBase数据库支持了多个重要业务的数据 ...
- hadoop权威指南学习
通常情况下,处理少量的大型文件更容易.更有效,为什么呢? map阶段中的键如果不需要可以忽略掉? MapReduce过程也可以用于本地文件的处理,但是如果是要使用到集群的话还需要HDFS. Data ...
- 计算两个GPS坐标点的距离
计算两个GPS坐标点的距离,第一个参数是第一个点的维度,第二个参数是第一个点的经度 http://yuninglovekefan.blog.sohu.com/235655696.html /** * ...
- [nio]dawn的基本概念
1.dawn是单线程的: 为什么单线程?现实中非常多程序都是单线程的.比方redis,memcache,nodejs.mmorpgserver..... . 採用单线程有两大优点,首先,不须要使用锁, ...
- 【题解】DZY Loves Chinese
[题解]DZY Loves Chinese II 不吐槽这题面了... 考虑如何维护图的连通性,如果把图的变成一颗的\(dfs\)生成树,那么如果把一个节点的父边和他接下来所有的返祖边删除,那么我们就 ...
- CentOS 7 安装、配置、使用 PostgreSQL 9.5(一)安装及基础配置
一直不知道怎么读这个数据库的名字,在官网上找到了文档.PostgreSQL is pronounced Post-Gres-Q-L. 读音 What is PostgreSQL? How is it ...
- ABAP-创建客户
CALL METHOD CMD_EI_API=>MAINTAIN_BAPI FUNCTION Z_CS_RFC_OA002 . *"------------------------ ...