Codeforces Round #367 (Div. 2)D. Vasiliy's Multiset (字典树)
D. Vasiliy's Multiset
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
.
得学姐指导----涉及到XOR的建树都建字典树。
哇哈哈~
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
const int maxn = 5e6+;
struct node
{
int next[];
int v;
};
node tree[maxn];
int sz = ;
void build(int x,int v)
{
int root = ;
for(int i=;i>=;i--)
{
int id = (x>>i)&;
if(tree[root].next[id]==)
{
memset(tree[sz].next,,sizeof(tree[sz].next));
tree[sz].v = ;
tree[root].next[id] = sz++;
}
root = tree[root].next[id];
tree[root].v+=v;
}
}
void match(int x)
{
int root = ;
x = ~x;
int ans = ;
for(int i=;i>=;i--)
{
ans *= ;
int id = (x>>i)&;
if(tree[root].next[id]&&tree[tree[root].next[id]].v)
{
ans++;
root = tree[root].next[id];
}
else
{
root = tree[root].next[-id];
}
}
printf("%d\n",ans);
}
int main()
{
int n,x;
char s[];
cin>>n; /* for(int i=0;i<=maxn-1;i++)
{
tree[i].v = 0;
memset(tree[i].next,0,sizeof(tree[i].next));
}*/
build(,);
for(int i=;i<=n;i++)
{
scanf("%s %d",s,&x);
if(s[]=='+')
{
build(x,);
}
else if(s[]=='-')
{
build(x,-);
}
else
{
match(x);
}
}
return ;
}
Codeforces Round #367 (Div. 2)D. Vasiliy's Multiset (字典树)的更多相关文章
- 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
题目链接: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
题目链接: 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(可持久化Trie)
D. Vasiliy's Multiset time limit per test 4 seconds memory limit per test 256 megabytes input standa ...
- Codeforces Round #367 (Div. 2)---水题 | dp | 01字典树
A.Beru-taxi 水题:有一个人站在(sx,sy)的位置,有n辆出租车,正向这个人匀速赶来,每个出租车的位置是(xi, yi) 速度是 Vi;求人最少需要等的时间: 单间循环即可: #inclu ...
- Codeforces Round #367 (Div. 2) D. Vasiliy's Multiset(01字典树求最大异或值)
http://codeforces.com/contest/706/problem/D 题意:有多种操作,操作1为在字典中加入x这个数,操作2为从字典中删除x这个数,操作3为从字典中找出一个数使得与给 ...
- Codeforces Round #311 (Div. 2) E. Ann and Half-Palindrome 字典树/半回文串
E. Ann and Half-Palindrome Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contes ...
- Codeforces Round #291 (Div. 2) C. Watto and Mechanism [字典树]
传送门 C. Watto and Mechanism time limit per test 3 seconds memory limit per test 256 megabytes input s ...
随机推荐
- Underscore.js 的模板功能介绍与应用
Underscore是一个非常实用的JavaScript库,提供许多编程时需要的功能的支持,他在不扩展任何JavaScript的原生对象的情况下提供很多实用的功能,需要了解的朋友可以详细参考下 U ...
- ios导航栏适配
我们做屏幕导航栏横竖屏适配的时候,会发现top的值多少都有一点的偏移,加了背景色之后从0开始,不加背景色从64开始,解决方法self.extendedLayoutIncludesOpaqueBars ...
- Anaroid WebView详解大全
资源描述: 1.android提供了webView控件专门用来浏览网页.然后在程序中装载webView控件,设置属性,比如:颜色.字体.要访问的网址等.通过loadUrl方法设置当前webView需要 ...
- qmake pro工程设置
----我的生活,我的点点滴滴!! #以下是在terminal里面编译,想编译哪个就修改pro文件 #================================================= ...
- Spring 实例化bean的三种方式:
方法一:使用构造器实例化bean java代码: package com.model; public class User { private String username; public User ...
- tomcat连接数设置
如何加大tomcat连接数 在tomcat配置文件server.xml中的<Connector ... />配置中,和连接数相关的参数有:minProcessors:最小空闲连接线程数,用 ...
- mongodb分片认证
启动configsvr 1. 确保mongdb的configsvr是采用service模式启动的,即从/etc/init.d下的脚本启动的,其用户是mongod. 2. 确保mongod的配置文件完全 ...
- SQL的自增列重置的方法
SQL的自增列挺好用,只是开发过程中一旦删除数据,标识列就不连续了 写起来 也很郁闷,所以查阅了一下标识列重置的方法 发现可以分为三种: --- 删除原表数据,并重置自增列 truncate tabl ...
- Drivers Dissatisfaction
Drivers Dissatisfaction time limit per test 4 seconds memory limit per test 256 megabytes input stan ...
- php杂乱
// // if ( $_GET['action'] == 'search' ) {// $_clean = array();// $_clean['stype ...