ACdream 1063 字典树
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 字典树的更多相关文章
- hdu 4825 && acdream 1063 01字典树异或问题
题意: 给一个集合,多次询问,每次给一个k,问你集合和k异或结果最大的哪个 题解: 经典的01字典树问题,学习一哈. 把一个数字看成32位的01串,然后查找异或的时候不断的沿着^为1的路向下走即可 # ...
- 萌新笔记——用KMP算法与Trie字典树实现屏蔽敏感词(UTF-8编码)
前几天写好了字典,又刚好重温了KMP算法,恰逢遇到朋友吐槽最近被和谐的词越来越多了,于是突发奇想,想要自己实现一下敏感词屏蔽. 基本敏感词的屏蔽说起来很简单,只要把字符串中的敏感词替换成"* ...
- [LeetCode] Implement Trie (Prefix Tree) 实现字典树(前缀树)
Implement a trie with insert, search, and startsWith methods. Note:You may assume that all inputs ar ...
- 字典树+博弈 CF 455B A Lot of Games(接龙游戏)
题目链接 题意: A和B轮流在建造一个字,每次添加一个字符,要求是给定的n个串的某一个的前缀,不能添加字符的人输掉游戏,输掉的人先手下一轮的游戏.问A先手,经过k轮游戏,最后胜利的人是谁. 思路: 很 ...
- 萌新笔记——C++里创建 Trie字典树(中文词典)(一)(插入、遍历)
萌新做词典第一篇,做得不好,还请指正,谢谢大佬! 写了一个词典,用到了Trie字典树. 写这个词典的目的,一个是为了压缩一些数据,另一个是为了尝试搜索提示,就像在谷歌搜索的时候,打出某个关键字,会提示 ...
- 山东第一届省赛1001 Phone Number(字典树)
Phone Number Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^ 题目描述 We know that if a phone numb ...
- 字典树 - A Poet Computer
The ACM team is working on an AI project called (Eih Eye Three) that allows computers to write poems ...
- trie字典树详解及应用
原文链接 http://www.cnblogs.com/freewater/archive/2012/09/11/2680480.html Trie树详解及其应用 一.知识简介 ...
- HDU1671 字典树
Phone List Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
随机推荐
- JAVA中抽象类不可以实例化,却可以创建数组
这是我定义的一个抽象类: 如果你试图创建一个对象,当然是不行的,抽象类不能用new运算符创建对象. 这是错误提示,还记得instantiate这个单词吗?在我的这篇随笔第二篇(那些JAVA程序BUG中 ...
- A joke about regular expression
As the old computer science joke goes: “Let’s say you have a problem, andyou decide to solve it with ...
- codeforces 555B Case of Fugitive
题目连接: http://codeforces.com/problemset/problem/555/B 题目大意: 有n个岛屿(岛屿在一列上,可以看做是线性的,用来描述岛屿位置的是起点与终点),m个 ...
- 题解报告:hdu 1032 The 3n + 1 problem(克拉兹问题)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1032 Problem Description Problems in Computer Science ...
- Java中的流(2)字节流-InputStream和OutputStream
字节流的两个顶层类是抽象类:InputStream和OutputStream 1. OutputStream void write(int b) 往流中写一个字节b void write(byte b ...
- github下载下来的C#控制台小游戏[含源码]
早就听说了github是世界最大的源码库,但自己却不是很懂,今天去研究了下,注册了一个帐号,然后在上面搜索了一下C# game,然后发现有许多的游戏. 随意地选择了一个,感觉比较简单,于是就下载了下来 ...
- 172 Factorial Trailing Zeroes 阶乘后的零
给定一个整数 n,返回 n! 结果尾数中零的数量.注意: 你的解决方案应为对数时间复杂度. 详见:https://leetcode.com/problems/factorial-trailing-ze ...
- SP CAML工具
直接一直使用CAML做一些简单的SP列表查询,突然想对CAML进一步了解,于是找到两个常用工具,做以记录: 1 Caml Query Builder : 用于编写CAML查询,对初学者可以了解查询语句 ...
- mysql自动获取时间日期
实现方式: 1.将字段类型设为 TIMESTAMP 2.将默认值设为 CURRENT_TIMESTAMP 举例应用: 1.MySQL 脚本实现用例 --添加CreateTime 设置默认时间 C ...
- [BZOJ1878][SDOI2009]HH的项链 莫队
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1878 不带修改的莫队,用一个桶记录一下当前区间中每种颜色的数量就可以做到$O(1)$更新了 ...