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
Note

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 .

题意:n个操作,+  x表示在一个Multiset中添加一个x的数;

        -  x表示在一个Multiset中删除一个x的数;

        ? x表示输出x与Multiset中一个最大的异或值

思路:trie数,找x的二进制为相反,相反的位置最大结果越优;

#include<bits/stdc++.h>
using namespace std;
#define ll __int64
#define mod 1000000007
#define pi (4*atan(1.0))
const int N=1e5+,M=4e6+,inf=1e9+;
int a[M][],sum[M],len;
void init()
{
memset(a,,sizeof(a));
memset(sum,,sizeof(sum));
len=;
}
void insertt(int x)
{
int num[];
memset(num,,sizeof(num));
int flag=;
while(x)
{
num[flag++]=x%;
x/=;
}
int u=,n=;
for(int i=n; i>=; i--)
{
if(!a[u][num[i]])
{
a[u][num[i]]=len++;
}
u=a[u][num[i]];
sum[u]++;
}
}
void del(int x)
{
int num[];
memset(num,,sizeof(num));
int flag=;
while(x)
{
num[flag++]=x%;
x/=;
}
int u=,n=;
for(int i=n; i>=; i--)
{
int v=a[u][num[i]];
sum[v]--;
if(!sum[v])
a[u][num[i]]=;
u=v;
}
}
int getans(int x)
{
int num[];
memset(num,,sizeof(num));
int flag=;
while(x)
{
num[flag++]=x%;
x/=;
}
for(int i=; i<=; i++)
num[i]=num[i]?:;
int u=,n=,v,w;
int ans=;
for(int i=n; i>=; i--)
{
if(num[i])
{
v=;
w=;
}
else
{
w=;
v=;
}
if(a[u][v])
{
u=a[u][v];
ans+=(<<i);
}
else
u=a[u][w];
}
return ans;
}
char ch[];
int main()
{
int x,y,z,i,t;
int T,cas;
init();
insertt();
scanf("%d",&T);
for(cas=; cas<=T; cas++)
{
scanf("%s %d",ch,&x);
if(ch[]=='+')
insertt(x);
else if(ch[]=='-')
del(x);
else
printf("%d\n",getans(x));
}
return ;
}

Codeforces Round #367 (Div. 2) D. Vasiliy's Multiset trie树的更多相关文章

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

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

  3. Codeforces Round #367 (Div. 2) D. Vasiliy's Multiset

    题目链接:Codeforces Round #367 (Div. 2) D. Vasiliy's Multiset 题意: 给你一些操作,往一个集合插入和删除一些数,然后?x让你找出与x异或后的最大值 ...

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

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

  6. Codeforces Round #367 (Div. 2) D. Vasiliy's Multiset(01字典树求最大异或值)

    http://codeforces.com/contest/706/problem/D 题意:有多种操作,操作1为在字典中加入x这个数,操作2为从字典中删除x这个数,操作3为从字典中找出一个数使得与给 ...

  7. Codeforces Round #367 (Div. 2)---水题 | dp | 01字典树

    A.Beru-taxi 水题:有一个人站在(sx,sy)的位置,有n辆出租车,正向这个人匀速赶来,每个出租车的位置是(xi, yi) 速度是 Vi;求人最少需要等的时间: 单间循环即可: #inclu ...

  8. Codeforces Round #333 (Div. 1) D. Acyclic Organic Compounds trie树合并

    D. Acyclic Organic Compounds   You are given a tree T with n vertices (numbered 1 through n) and a l ...

  9. Codeforces Round #367 (Div. 2) C. Hard problem(DP)

    Hard problem 题目链接: http://codeforces.com/contest/706/problem/C Description Vasiliy is fond of solvin ...

随机推荐

  1. Python3.6全栈开发实例[026]

    27.文件a.txt内容:每一行内容分别为商品名字,价钱,个数.apple 10 3tesla 100000 1mac 3000 2lenovo 30000 3chicken 10 3通过代码,将其构 ...

  2. kubernetes 搭建教程

    http://blog.csdn.net/u011563903/article/details/71037093

  3. 020-Spring Boot 监控和度量

    一.概述 通过配置使用actuator查看监控和度量信息 二.使用 2.1.建立web项目,增加pom <dependency> <groupId>org.springfram ...

  4. 201704 F-02创建财务凭证

    一.F-02 创建财务凭证-BDC 期初余额导入程序 程序:ZFIU_LDV_F_02_UPLOAD 创建F-02 凭证: *&-------------------------------- ...

  5. ABAP 断点篇-001

    断点技能不足! 6.2.4 可在调试画面设置break-point.方法:在代码前面双击 6.2.5 为指定语句设置断点方法:(1)选择菜单:Breakpoints->Breakpointat ...

  6. 【HTTP】WEB机器人

    <HTTP权威指南>学习摘要 Web Robot的自活跃(self-animating)用户代理. Web机器人是在不需要人工干预的情况下,自动进行一系列Web事务处理的软件程序,别名“爬 ...

  7. 源码编译搭建LAMP

    环境版本信息: RHEL 5.3 Apache / 2.4.16 PHP / 5.4.45 mysql-5.5.45 源代码编译 安装方式 1: configure 配置 以及定制我们的软件包 2: ...

  8. 流量分析系统---kafka集群部署

    1.集群部署的基本流程 Storm上游数据源之Kakfa 下载安装包.解压安装包.修改配置文件.分发安装包.启动集群 2.基础环境准备 安装前的准备工作(zk集群已经部署完毕)  关闭防火墙 chk ...

  9. Centos配置sftp

    sftp配置: ssh -V  使用ssh –V命令来查看openssh的版本,版本必须大于4.8p1,低于这个版本需要升级. 1.添加用户及用户组: groupadd sftp useradd -g ...

  10. Loadrunder脚本篇——web_custom_request做接口测试

    一.POST + JSON格式参数 例: web_custom_request("create", "URL=http://xxx.xxx.x.xx:1600/ditui ...