ACdream 1063 字典树

                                          平衡树

神奇的cxlove有一颗平衡树,其树之神奇无法用语言来描述 OrzOrz。 这棵树支持3种操作: 1、加入一个数到树中,维护平衡树的合法性; 2、给一个数X,用O(1)的时间求出来树中的数Y使得 Y ^ X 最大(异或操作, Pascal 写作 xor , 0 ^ 0 = 0 , 1 ^ 1 = 0 , 0 ^ 1 = 1 , 1 ^ 0 = 1 , 2 ^ 3 = 1) 3、给一个数X,用O(1)的时间求出来树中的数Y使得 Y ^ X 最小(异或操作, Pascal 写作 xor , 0 ^ 0 = 0 , 1 ^ 1 = 0 , 0 ^ 1 = 1 , 1 ^ 0 = 1 , 2 ^ 3 = 1) 请你帮忙实现这颗树。 cxlove由于过于牛,有些事情是无法做到的,你能在1s以内通过就好了。 最后补充一句,cxlove是世界冠军!

Input

第一行是数据组数T (T ≤ 100)。

对于每组数据,第一行是操作数N (N ≤ 10000)。

以下 N 行,每行一个字符串一个数字,分别代表:

insert X ,加入数 X

qmax X ,求第2种操作的答案

qmin X ,求第3种操作的答案

输入保证不存在空树Query的情况 (1 ≤ X ≤ 1e9)

Output

对于操作 2 , 3 输出相应的答案。

Sample Input

1

4

insert 1

insert 2

qmin 1

qmax 1

Sample Output

0

3

Hint

Huge input and output. Please do not use: 1. cin ans cout of C++ 2. scanner of Java(Maybe BufferedReader is quicker)

Source

buaads

Manager

传送门

题目名相当不厚道,很显然求xor最大最小值使用01 tree才对。

01字典树,同字典树,只是将数字当做01串进行处理,那么对于一个数,求最大xor就往方向走,求最小xor就往同方向走就好

#include<queue>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#define ll long long
#define inf 1000000000
#define mod 1000000007
using namespace std;
int read()
{
int x=0,f=1;
char ch=getchar();
while(ch<'0'||ch>'9')
{
if(ch=='-')f=-1;
ch=getchar();
}
while(ch>='0'&&ch<='9')
{
x=x*10+ch-'0';
ch=getchar();
}
return x*f;
}
const int N=320000;
const int sigma=2;
int ch[N][sigma];
int var[N],sz;
void clear(){
sz=1;
memset(ch[0],0,sizeof(ch[0]));
}
void insert(int x){
int u=0;
for(int i=31;i>=0;i--){
int c=(x>>i)&1;
if(!ch[u][c]){
memset(ch[sz],0,sizeof(ch[sz]));
ch[u][c]=sz;
var[sz++]=0;
}
u=ch[u][c];
}
var[u]=x;
}
int search(int x){
int u=0;
for(int i=31;i>=0;i--){
int c=(x>>i)&1;
if(ch[u][c^1]) u=ch[u][c^1];
else u=ch[u][c];
}
return var[u];
}
int search2(int x){
int u=0;
for(int i=31;i>=0;i--){
int c=(x>>i)&1;
if(ch[u][c]) u=ch[u][c];
else u=ch[u][c^1];
}
return var[u];
}
int main(){
int T=read();
while(T--)
{
int n;
n=read();
clear();
char op[30];
int x;
ll tmp;
for(int i=0; i<n; i++)
{
scanf("%s",op);x=read();
if(strcmp(op,"insert")==0) insert(x);
else if(strcmp(op,"qmax")==0)
{
tmp=search(x);
printf("%lld\n",tmp^x);
}
else
{
tmp=search2(x);
printf("%lld\n",tmp^x);
}
}
}
return 0;
}

ACdream 1063 字典树的更多相关文章

  1. hdu 4825 && acdream 1063 01字典树异或问题

    题意: 给一个集合,多次询问,每次给一个k,问你集合和k异或结果最大的哪个 题解: 经典的01字典树问题,学习一哈. 把一个数字看成32位的01串,然后查找异或的时候不断的沿着^为1的路向下走即可 # ...

  2. 萌新笔记——用KMP算法与Trie字典树实现屏蔽敏感词(UTF-8编码)

    前几天写好了字典,又刚好重温了KMP算法,恰逢遇到朋友吐槽最近被和谐的词越来越多了,于是突发奇想,想要自己实现一下敏感词屏蔽. 基本敏感词的屏蔽说起来很简单,只要把字符串中的敏感词替换成"* ...

  3. [LeetCode] Implement Trie (Prefix Tree) 实现字典树(前缀树)

    Implement a trie with insert, search, and startsWith methods. Note:You may assume that all inputs ar ...

  4. 字典树+博弈 CF 455B A Lot of Games(接龙游戏)

    题目链接 题意: A和B轮流在建造一个字,每次添加一个字符,要求是给定的n个串的某一个的前缀,不能添加字符的人输掉游戏,输掉的人先手下一轮的游戏.问A先手,经过k轮游戏,最后胜利的人是谁. 思路: 很 ...

  5. 萌新笔记——C++里创建 Trie字典树(中文词典)(一)(插入、遍历)

    萌新做词典第一篇,做得不好,还请指正,谢谢大佬! 写了一个词典,用到了Trie字典树. 写这个词典的目的,一个是为了压缩一些数据,另一个是为了尝试搜索提示,就像在谷歌搜索的时候,打出某个关键字,会提示 ...

  6. 山东第一届省赛1001 Phone Number(字典树)

    Phone Number Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 We know that if a phone numb ...

  7. 字典树 - A Poet Computer

    The ACM team is working on an AI project called (Eih Eye Three) that allows computers to write poems ...

  8. trie字典树详解及应用

    原文链接    http://www.cnblogs.com/freewater/archive/2012/09/11/2680480.html Trie树详解及其应用   一.知识简介        ...

  9. HDU1671 字典树

    Phone List Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

随机推荐

  1. linux系统 for 大数据

    使用大数据前,需要选择linux系统,本人选择的是centos6.9系统,并且装在虚拟机上,并且要注意: 1.网络类型选择NAT. 2.语言设置为English(English). 3.操作系统的键盘 ...

  2. 【SpringCloud构建微服务系列】Feign的使用详解

    一.简介 在微服务中,服务消费者需要请求服务生产者的接口进行消费,可以使用SpringBoot自带的RestTemplate或者HttpClient实现,但是都过于麻烦. 这时,就可以使用Feign了 ...

  3. python正则表达式_总结

    正则表达式: 作用:正则表达式是用来查找字符串的. 之前:使用正则表达式首先要导入re模块(import re) re.match -- 从字符串的第一个单词开始匹配字符串.如果匹配到则返回一个对象: ...

  4. 笔记——malloc、free、不同数据类型操作、.pyc文件、python安装第三方包、验证一个网站的所有链接有效性

    C — malloc( ) and free( ) C 语言中使用malloc( )函数申请的内存空间,为什么一定要使用free释放? **malloc()函数功能:是从堆区申请一段连续的空间,函数结 ...

  5. linux mysql 8 安装

    http://blog.itpub.net/31015730/viewspace-2152272/ https://blog.csdn.net/HaHa_Sir/article/details/805 ...

  6. Python字符串对象常用方法

    安利一句话:字符串是不可变的对象,所以任何操作对原字符串是不改变的! 1.字符串的切割 def split(self, sep=None, maxsplit=-1): # real signature ...

  7. B - Archer

    Problem description SmallR is an archer. SmallR is taking a match of archer with Zanoes. They try to ...

  8. Linux环境下使用yum安装zip和unzip

    Linux环境下使用yum安装zip和unzip. yum install zip yum install unzip

  9. URAL1389. Roadworks(dp)

    1389 算个简单的树形DP吧 不知道是不是数据太水 竟然一A了 就是对于当前节点有没有被选中就行选最优 有没有被选中的意思是有没有与它相连的边被选中 #include <iostream> ...

  10. Android基础夯实--重温动画(四)之属性动画 ValueAnimator详解

    宝剑锋从磨砺出,梅花香自苦寒来:千淘万漉虽辛苦,吹尽狂沙始到金: 长风破浪会有时,直挂云帆济沧海 一.摘要 Animator类作为属性动画的基类,它是一个抽象类,它提供了实现动画的基本架构,但是我们不 ...