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. struts入门

    1.概念

  2. Mac nginx 配置

    nginx 安装: 在苹果系统下如果要安装nginx,首先要安装brew.安装brew可以查看网站:https://brew.sh: 一条命令即可搞定:/usr/bin/ruby -e "$ ...

  3. Minimum Palindromic Factorization(最少回文串分割)

    Minimum Palindromic Factorization(最少回文串分割) 以下内容大部分(可以说除了关于回文树的部分)来自论文A Subquadratic Algorithm for Mi ...

  4. Nginx源码分析--epoll模块

    Nginx采用epoll模块实现高并发的网络编程,现在对Nginx的epoll模块进行分析. 定义在src/event/modules/ngx_epoll_module.c中 1. epoll_cre ...

  5. gbdt和xgboost中feature importance的获取

    来源于stack overflow,其实就是计算每个特征对于降低特征不纯度的贡献了多少,降低越多的,说明feature越重要 I'll use the sklearn code, as it is g ...

  6. html 简单学习

    通过记事本,依照以下四步来创建您的第一张网页. 步骤一:启动记事本 如何启动记事本: 开始    所有程序        附件            记事本 步骤二:用记事本来编辑 HTML 在记事本 ...

  7. 响应式设计:根据不同设备引不同css样式

    <link rel="stylesheet" media="screen and (max-width:600px)" href="small. ...

  8. beego学习笔记(4):开发文档阅读(6)

    beego的响应流程: 1.监听的端口接收数据,默认是8080端口. 2.用户请求到达8080端口后,开始数据处理流程. 3.初始化CONTEXT对象.判断是否是WEBSOCKET请求,如果是,设置I ...

  9. ISSCC 2017论文导读 Session 14:A 288μW Programmable Deep-Learning Processor with 270KB On-Chip Weight

    A 288μW Programmable Deep-Learning Processor with 270KB On-Chip Weight Storage Using Non-Uniform Mem ...

  10. (转)Opencv卷积操作

    转自:http://www.2cto.com/kf/201312/267308.html Mask Operation filter2D函数 Last Edit 2013/12/24 所谓的Mask ...