Codeforces Round #367 (Div. 2) 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
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树的更多相关文章
- 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 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 (0/1-Trie树)
Vasiliy's Multiset 题目链接: http://codeforces.com/contest/706/problem/D Description Author has gone out ...
- 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(01字典树求最大异或值)
http://codeforces.com/contest/706/problem/D 题意:有多种操作,操作1为在字典中加入x这个数,操作2为从字典中删除x这个数,操作3为从字典中找出一个数使得与给 ...
- Codeforces Round #367 (Div. 2)---水题 | dp | 01字典树
A.Beru-taxi 水题:有一个人站在(sx,sy)的位置,有n辆出租车,正向这个人匀速赶来,每个出租车的位置是(xi, yi) 速度是 Vi;求人最少需要等的时间: 单间循环即可: #inclu ...
- 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 ...
- Codeforces Round #367 (Div. 2) C. Hard problem(DP)
Hard problem 题目链接: http://codeforces.com/contest/706/problem/C Description Vasiliy is fond of solvin ...
随机推荐
- 《从零开始学Swift》学习笔记(Day 36)——静态方法
原创文章,欢迎转载.转载请注明:关东升的博客 静态方法与静态属性类似,Swift中定义了静态方法,也称为类型方法.静态方法的定义与静态属性类似,枚举和结构体的静态方法使用的关键字是static:类静态 ...
- angularjs 遇见$scope,xxx=function()报错为该函数未定义
本包子今天遇见一个问题,就是明明写了$scope,xx=function()但是报错了,报错显示是该函数未定义,我就很着急的先将函数写成一个全局函数,就没问题,等下午有空的时候寻思了一下,为什么全局就 ...
- 关于angularjs的ng-repeat指令
(如果有说的不对,欢迎指教,更欢迎大家一起交流.) 关于angularjs的ng-repeat指令,想必每个学习angularjs的初学者都很有映像.那我也总结一下我使用ng-repeat的时候经验, ...
- 关于org.apache.shiro.SecurityUtils.getSubject().getSession()
Subject currentUser = SecurityUtils.getSubject(); Session session = currentUser.getSession(); s ...
- RedHat6/Centos6.5安装mongodb
1 设置mongodb目录 [root@localhost /]# cd home/ [root@localhost home]# mkdir mongodb/db 2 配置yum [root@loc ...
- 浅析僵尸进程&孤儿进程
0x01 前言 此文出自:https://www.cnblogs.com/Anker/p/3271773.html 博文主要用unix/linux举例,但道理没问题的同样有助于在Python中理解僵尸 ...
- 前端基础-html(2)
一.字体标签 字体标签包含:h1~h6.<font>.<u>.<b>.<strong>.<em>.<sup>.<sub&g ...
- Windows系统Python 安装第三方模块时,提示pip版本有问题
如果按照提示输入python -m pip install --upgrade pip 还不行, 那么执行easy_install --upgrade pip 即可 参考:https://stacko ...
- ubuntu ping响应慢的解决(转)
新装ubuntu之后感觉上网老是很慢,ping网站时每次ping指令都需要很久才能有响应,不过网络延迟却正常.后来发现是因为/etc/nsswitch.conf文件中hosts的配置有问题,做如下修改 ...
- django基本安装
一.web框架 1.什么是web框架? Web框架是一种开发框架,用来支持动态网站.网络应用程序及网络服务的开发.其类型有基于请求的和基于组件的两种框架. 本质上其实就是一个socket服务端,用户的 ...