题目链接:

D. Vasiliy's Multiset

time limit per test

4 seconds

memory limit per test

256 megabytes

input

standard input

output

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:

  1. "+ x" — add integer x to multiset A.
  2. "- 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.
  3. "? 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.

Input

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.

Output

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.

Example
input
10
+ 8
+ 9
+ 11
+ 6
+ 1
? 3
- 8
? 3
? 8
? 11
output
11
10
14
13
题意:
 
+表示吧这个数加到集合中,-表示把这个数从集合中减去一次,?表示集合里面的一个y使的x^y最大;
 
思路:
 
trie树,把这些数都转化成01串插入到trie树种,?的时候从高位到低位,是0的时候就尽量取1,是1的时候就尽量取0这样贪心就好了;比赛的时候数组开小了;
 
AC代码;
 
#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树)的更多相关文章

  1. 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 ...

  2. Codeforces 706 D. Vasiliy's Multiset (字典树贪心)

    题目链接:http://codeforces.com/contest/706/problem/D 题意很简单不多说. 把一个数当作二进制插入字典树中,按照xor的性质,1找0,0找1,贪心即可. 注意 ...

  3. 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 ...

  4. 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 ...

  5. Codeforces 615C Running Track(DP + Trie树)

    题目大概说给两个串,问最少要用多少个第一个串的子串(可以翻转)拼成第二个串. UVa1401,一个道理..dp[i]表示前缀i拼接成功所需最少的子串,利用第一个串所有子串建立的Trie树往前枚举转移. ...

  6. Codeforces 633C Spy Syndrome 2 【Trie树】+【DFS】

    <题目链接> 题目大意:给定一个只有小写字母组成的目标串和m个模式串(里面可能有大写字母),记目标串反过来后的串为S,让你从m个模式串中选出若干个组成S串(不区分大小写).输出任意一种方案 ...

  7. Codeforces 514C Watto and Mechanism 【Trie树】+【DFS】

    <题目链接> 题目大意:输入n个单词构成单词库,然后进行m次查询,每次查询输入一个单词(注意这些单词只由a,b,c构成),问该单词库中是否存在与当前查询的单词有且仅有一个字符不同的单词. ...

  8. CodeForces 706D Vasiliy's Multiset (字典树查询+贪心)

    题意:最开始的时候有一个集合,集合里面只有一个元素0,现在有q次操作,操作分为3种: + x: 表示向集合中添加一个元素x - x:表示删除集合中值为x的一个元素 ? x:表示查询集合中与x异或的最大 ...

  9. 【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. Linux是怎么启动的(整理)

    昨天笔试考了一道关于linux系统启动的过程,当时没答上来,现在整理出来(其实并不复杂). 按下电源按钮的直到欢迎页出来之后,linux总共做的事可以分为五步来完成. 1.  BIOS加电自检: 加电 ...

  2. Node.js学习笔记(2)——关于异步编程风格

    Node.js的异步编程风格是它的一大特点,在代码中就是体现在回调中. 首先是代码的顺序执行: function heavyCompute(n, callback) { var count = 0, ...

  3. jQuery与ajax的应用(一)

    <body> <div id="resText"></div> <div id="reshtml"></d ...

  4. Visual Studio 2017 for Mac Preview

    Microsoft Visual Studio 2017 for Mac Preview 下载+安装+案例Demo 目录: 0. 前言 1. 在线安装器 2. 安装VS 3. HelloWorld 4 ...

  5. 【WPF学习笔记】[转]周银辉之WPF中的动画 && 晓风影天之wpf动画——new PropertyPath属性链

    (一)WPF中的动画 动画无疑是WPF中最吸引人的特色之一,其可以像Flash一样平滑地播放并与程序逻辑进行很好的交互.这里我们讨论一下故事板. 在WPF中我们采用Storyboard(故事板)的方式 ...

  6. tps 与 事务平均响应时间关系对答(转)

    问者:每秒处理的事务数和事务的平均响应时间 怎么个关系,有关系吗 kaku21:举个例子:一个高速路 有10个入口,每个入口每秒钟只能进1辆车,请问1秒钟最多能进几辆车?? 问者:10 kaku21: ...

  7. Firefox与chrome同步书签

    1. 导出书签,保存为bookmarks.html 2. chrome导入即可

  8. 我的Java开发学习之旅------>Java经典排序算法之插入排序

    一.算法原理 插入排序法:所谓插入排序法乃是将一个数目插入该占据的位置. 假设我们输入的是 "53,27,36,15,69,  42" 我们从第二个数字开始,这个数字是27,我们的 ...

  9. WordPress用户角色及其权限管理编辑插件:User Role Editor汉化版

    如果Wordpress默认的用户角色及权限不能满足您的需求,又觉得修改代码编辑用户权限太麻烦.那不妨试试User Role Editor,Wordpress用户角色及其权限管理编辑插件. User R ...

  10. 将socket通信实现多进程

    我们知道,使用TCP协议需要提前建立连接,这样就只能一对一的传输,但是这样感觉十分单一,如果实现一个服务器能同时和多个客户端同信了? 这里就需要用到多线程. 处理的不同之处就在于:每一个接进来的客户都 ...