1783: 秋实大哥与快餐店
Time Limit: 1 Sec Memory Limit: 128 MB
Submit: 78 Solved: 12
[Submit][Status][Web Board]
Description
朝为田舍郎,暮登天子堂。秋实大哥从小就怀抱有远大的理想,所以他开了一家快餐店。
秋实大哥根据菜的口感,给每一道菜一个唯一的CID,同时对于前来的客人,根据他们的口味喜好,秋实大哥会给每一个客人一个PID。
对于一个标号为PID的客人,他对标号为CID的菜的喜爱程度为PID∧CID(∧表示按位异或),该值越大表示越喜欢。
秋实大哥实在太忙了,现在他需要你来帮忙照看一下他的店铺。
Input
第一行包含一个整数n,表示秋实大哥的餐馆内现在有n道菜。接下来一行包含n个整数,分别表示每一道菜的CID。
接下来一行包含一个整数m,表示接下来发生了m件事。接下来的m行,每一行为以下两种事件之一:
0 c : 表示秋实大哥最新研制出一道标号为c的菜
1 p : 表示来了一位标号为p的客人,请你在已有的菜中找出一道他最喜爱的菜
1≤n,m≤100000,0≤PID,CID≤1000000。
Output
对于每一个1 p事件输出一个整数,表示该客人最喜欢的菜的标号
Sample Input
1
1
3
1 1
0 2
1 1
Sample Output
1
2
因为题目说了是cid^pid最大的数,所以根据异或相同则0,不同则1的性质可知,只要是高位二进制各异就是最大的数了。所以我们可以用菜的价值来建立一颗0-1的二叉树。也就是01字典树。
第一种写法:
#include<bits/stdc++.h>
using namespace std;
#define MAXN 100005
#define ll long long
int n,m;
int trie[32*MAXN][2];
ll val[32*MAXN];
int tot;
void insert(ll d)
{
int root=0;
for(int i=32;i>=0;i--)
{
int id=(d>>i)&1;
if(!trie[root][id]) trie[root][id]=++tot;
root=trie[root][id];
}
val[root]=d;
}
ll query(ll d)
{
int root=0;
for(int i=32;i>=0;i--)
{
int id=(d>>i)&1;
if(trie[root][id^1]) root=trie[root][id^1];
else root=trie[root][id];
}
return val[root];
}
int main()
{
while(~scanf("%d",&n)){//题目没有说要多组输入,但是是多组输入的要不然就会Wrong
memset(trie,0,sizeof trie);
for(int i=0;i<n;i++)
{
ll d;scanf("%lld",&d);
insert(d);
}
scanf("%d",&m);
for(int i=0;i<m;i++)
{
int t;ll d;scanf("%d%lld",&t,&d);
if(t&1)printf("%lld\n",query(d));
else insert(d);
}
}
return 0;
}
第二种是在网上看到别的大佬写的 (搞不清里面的rt<<1|1和rt<<1)
#include<bits/stdc++.h>
using namespace std;
int n,m;
const int maxbit = 20;
struct node
{
int L,R,id;
}tire[1<<22];
//其实就是一颗0-1组成的二叉树的叶子节点处标记一个菜品id
void Insert(int x)//插入一个数
{
int rt = 1;
for(int i=1;i<=maxbit;++i){//一共有20位
if((x >>(maxbit - i))&1){//x当前二进制的maxbit-i+1位当前是否为1
tire[rt].R = 1;//标记在右子树
rt=(rt<<1|1);
}
else{
tire[rt].L = 1;//标记在左子树
rt=(rt<<1);printf("%d**\n",rt);
}
}
//printf("%d**\n",rt);
tire[rt].id = x;//放入菜品id
}
int query(int x)
{
int rt = 1,tar = ~x;
for(int i=1;i<=maxbit;++i){
if((tar >>(maxbit-i))&1){//取反后的数在右子树
if(tire[rt].R){//如果右子树标记过,则肯定是这个数最大
rt = rt<<1|1;
}
else {//否则应该走回与x原来的二进制位才能继续找最大
rt = rt << 1;
}
}
else {
if(tire[rt].L){
rt = rt<<1;
}
else {
rt = rt<<1|1;
}
}
}
return tire[rt].id;
}
int main()
{
int i,j,k,c,p;
while(~scanf("%d",&n))
{
memset(tire,0,sizeof(tire));
for(i=0;i<n;++i){
scanf("%d",&k);
Insert(k);
}
scanf("%d",&m);
for(i=0;i<m;++i){
scanf("%d%d",&c,&p);
if(c&1){
printf("%d\n",query(p));
}
else {
Insert(p);
}
}
}
return 0;
}