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 ...
随机推荐
- 1070 Bash游戏 V4
1070 Bash游戏 V4 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题 有一堆石子共有N个.A B两个人轮流拿,A先拿.每次拿的数量最少1个,最多不超过对手上 ...
- 【转】在服务器上排除问题的头五分钟&常用命令
转自:https://blog.csdn.net/l821133235/article/details/80103106(在服务器上排除问题的头五分钟) 遇到服务器故障,问题出现的原因很少可以一下就想 ...
- Machine Learning - week 2 - 编程练习
3. % J = COMPUTECOST(X, y, theta) computes the cost of using theta as the % parameter for linear r ...
- Printing tools 自定义模板打印的实现
#ArcGIS for Server 自定义打印两种方法 友好阅读版本: http://gishub.info/2013/09/17/printingtools/ ## 前言使用web打印会遇到中文乱 ...
- Java你不知道的那些事儿—Java隐藏特性
转载自:http://www.cnblogs.com/lanxuezaipiao/p/3460373.html 每 种语言都很强大,不管你是像我一样的初学者还是有过N年项目经验的大神,总会有你不知道的 ...
- Linux中的正则表达式
* 前一个字符匹配0次或任意次. 匹配除了换行符外任意一个字符^ 匹配行首$ 匹配行尾[] ...
- 小程序 欢迎页面 navigateTo和tabBar不能同时指向一个路径
小程序navigateTo和tabBar不能同时指向一个路径 wx.navigateTo和wx.redirectTo不允许跳转到tabBar页面,只能用wx.switchTab跳转到tabBar页面. ...
- spring下配置shiro
1.web.xml中加入shiro的过滤器: <!-- Spring --> <!-- 配置Spring配置文件路径 --> <context-param> < ...
- sublime使用心得
1.ctrl + shift +p 命令面板 ---> toggle_side_bar 2.ctrl + shift +p 命令面板 --->reindent lines 3.ctrl + ...
- Get Region Information from IP Address with Taobao API
通过淘宝的API "http://ip.taobao.com/service/getIpInfo.php?ip=*.*.*.*" 来获得你要查询的IP地址的国家,地区,城市,ISP ...