C - 秋实大哥与快餐店

Time Limit: 1 Sec  Memory Limit: 256 MB

题目连接

http://acm.uestc.edu.cn/#/contest/show/59

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

HINT

题意

题解:

字典树,因为是int范围,所以每个数拆成2进制 ,然后最多32位,然后直接插进去就好了,每次跑线段树的时候,都往反方向去跑就好了

代码:

#include <iostream>
#include <string.h>
#include <stdio.h> using namespace std;
typedef long long LL; class Trie
{
public:
Trie *next[];
Trie()
{
memset(next,NULL,sizeof(next));
}
}; Trie *root; void Insert(LL n)
{
Trie *p = root;
for(int i=;i>=;i--)
{
int id = (n >> i) & ;
if(p->next[id] == NULL)
p->next[id] = new Trie();
p = p->next[id];
}
} void Delete(Trie *T)
{
if(T == NULL) return;
for(int i=;i<;i++)
if(T->next[i] != NULL)
Delete(T->next[i]);
} LL Match(LL m)
{
m = ~m;
LL ans = ;
Trie *p = root;
for(int i=;i>=;i--)
{
ans <<= ;
int bit = (m >> i) & ;
if(bit)
{
if(p->next[])
{
p = p->next[];
ans++;
}
else
{
p = p->next[];
}
}
else
{
if(p->next[])
{
p = p->next[];
}
else
{
p = p->next[];
ans++;
}
}
}
return ans;
} int main()
{
int n,m;
root = new Trie();
scanf("%d",&n);
for(int i=;i<n;i++)
{
LL val;
scanf("%lld",&val);
Insert(val);
}
scanf("%d",&m);
while(m--)
{
int a;
scanf("%d",&a);
if(a==)
{
LL val;
scanf("%lld",&val);
Insert(val);
}
else
{
LL val;
scanf("%lld",&val);
printf("%lld\n",Match(val));
}
}
return ;
}

2015 UESTC 数据结构专题C题 秋实大哥与快餐店 字典树的更多相关文章

  1. 2015 UESTC 数据结构专题B题 秋实大哥与花 线段树 区间加,区间查询和

    B - 秋实大哥与花 Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/contest/show/59 De ...

  2. 2015 UESTC 数据结构专题E题 秋实大哥与家 线段树扫描线求矩形面积交

    E - 秋实大哥与家 Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/contest/show/59 De ...

  3. 2015 UESTC 数据结构专题A题 秋实大哥与小朋友 线段树 区间更新,单点查询,离散化

    秋实大哥与小朋友 Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/contest/show/59 Desc ...

  4. 2015 UESTC 数据结构专题N题 秋实大哥搞算数 表达式求值/栈

    秋实大哥搞算数 Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/problem/show/1074 Des ...

  5. 2015 UESTC 数据结构专题H题 秋实大哥打游戏 带权并查集

    秋实大哥打游戏 Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/contest/show/59 Descr ...

  6. 2015 UESTC 数据结构专题G题 秋实大哥去打工 单调栈

    秋实大哥去打工 Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/contest/show/59 Descr ...

  7. 2015 UESTC 数据结构专题D题 秋实大哥与战争 SET的妙用

    D - 秋实大哥与战争 Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/contest/show/59 D ...

  8. 2015 UESTC 数据结构专题D题 秋实大哥与战争 变化版本的线段树,合并区间,单点查询

    D - 秋实大哥与战争 Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/contest/show/59 D ...

  9. 2015 UESTC 搜索专题K题 秋实大哥の恋爱物语 kmp

    秋实大哥の恋爱物语 Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/contest/show/61 De ...

随机推荐

  1. offset宏的讲解【转】

    转自:http://blog.csdn.net/tigerjibo/article/details/8299584 1.offset宏讲解 #define offsetof(TYPE, MEMBER) ...

  2. 配置kernel的log buf大小(如果kmsg log被覆盖)

    如果在打印kmsg log时发现log被覆盖,log 的buf不够大可以使用默认配置调buf: defconfig CONFIG_LOG_BUF_SHIFT=20  (默认是17  2的17次方)   ...

  3. android 动态改变控件位置和大小 .

    动态改变控件位置的方法: setPadding()的方法更改布局位置. 如我要把Imageview下移200px:             ImageView.setPadding( ImageVie ...

  4. Tutorial 2: Requests and Responses

    转载自:http://www.django-rest-framework.org/tutorial/2-requests-and-responses/ Tutorial 2: Requests and ...

  5. u-boot引导内核过程

    目标板:2440 u-boot引导内核启动时,传入内核的参数为bootcmd=nand read.jffs2 0x30007FC0 kernel; bootm 0x30007FC0 一.nand re ...

  6. google浏览器中,使用clockwork 来调试

    参考:https://laravel-china.org/courses/laravel-package/1976/debugging-tool-under-chrome-itsgoingdclock ...

  7. day41 - 异步IO、协程

    目录 (见右侧目录栏导航) - 1. 前言- 2. IO的五种模型- 3. 协程    - 3.1 协程的概念- 4. Gevent 模块    - 4.1 gevent 基本使用    - 4.2 ...

  8. html基础-css-选择器

    <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...

  9. 构建最基础的Spring项目及所需要的jar包

    1.Spring 框架由6个模块组成 上图是spring4的结构图,不同版本结构略有差异,但模块都是相同的. 2.Core Container 核心容器 容器是Spring的核心部分,Core Con ...

  10. 学习build-web-application-with-golang第四章内容

    GITHUB网址: https://github.com/astaxie/build-web-application-with-golang 内容 4.表单 4.1 处理表单的输入 4.2 验证表单的 ...