您的位置:首页 > 其它

USETC 1060 秋实大哥与快餐店

2016-03-31 13:57 351 查看
秋实大哥与快餐店
朝为田舍郎,暮登天子堂。秋实大哥从小就怀抱有远大的理想,所以他开了一家快餐店。
秋实大哥根据菜的口感,给每一道菜一个唯一的CIDCID,同时对于前来的客人,根据他们的口味喜好,秋实大哥会给每一个客人一个PIDPID。
对于一个标号为PIDPID的客人,他对标号为CIDCID的菜的喜爱程度为PID∧CIDPID∧CID(∧∧表示按位异或),该值越大表示越喜欢。
秋实大哥实在太忙了,现在他需要你来帮忙照看一下他的店铺。


Input

第一行包含一个整数nn,表示秋实大哥的餐馆内现在有nn道菜。
接下来一行包含nn个整数,分别表示每一道菜的CIDCID。
接下来一行包含一个整数mm,表示接下来发生了mm件事。
接下来的mm行,每一行为以下两种事件之一:
0
c : 表示秋实大哥最新研制出一道标号为c的菜

1 p : 表示来了一位标号为p的客人,请你在已有的菜中找出一道他最喜爱的菜
1≤n,m≤1000001≤n,m≤100000,0≤PID,CID≤10000000≤PID,CID≤1000000。


Output

对于每一个11 pp事件输出一个整数,表示该客人最喜欢的菜的标号。


因为是亦或,所以对查询以及已经存在的数字二进制化,然后构造一棵字典树,左结点为二进制下一位为0,右节点为二进制下一位为1,


每次查询的时候从高位到低位寻找与这一位相反的数,如果没有相反的再寻找相同的即可。

ACcode:

//************************************************************************//
//*Author : Handsome How                                                 *//
//************************************************************************//
//#pragma comment(linker, "/STA	CK:1024000000,1024000000")
#pragma warning(disable:4996)
#include <vector>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <algorithm>
#include <sstream>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <cassert>
#if defined(_MSC_VER) || __cplusplus > 199711L
#define aut(r,v) auto r = (v)
#else
#define aut(r,v) __typeof(v) r = (v)
#endif
#define each(it,o) for(aut(it, (o).begin()); it != (o).end(); ++ it)
#define fur(i,a,b) for(int i=(a);i<=(b);i++)
#define furr(i,a,b) for(int i=(a);i>=(b);i--)
#define cl(a) memset((a),0,sizeof(a))
using namespace std;
typedef long long LL;
//----------------------------------------------------
const int maxn = (2<<20)+100;
char ejz[30],ejzz[30];
int tree[maxn << 1];
int n, q,ans;
void init() {
cl(tree);
}

void ttt(int n) {
for (int i = 19; i >=0; i--)
{
ejz[i] = n % 2 + '0';
n = n / 2;
}
ejz[20] = 0;
}
void tttt(int n) {
for (int i = 19; i >= 0; i--)
{
ejzz[i] = n % 2 + '0';
n = n / 2;
}
ejzz[20] = 0;
}

void insert(int n,int r) {
if (n == 20) {
return;
}
if (ejz
== '0') {
tree[r<<1] = 1;
insert(n + 1, r << 1);
}
else {
tree[r<<1|1] = 1;
insert(n + 1, r << 1 | 1);
}
}

int p(int n) {
int b = 1;
for (int i = 0; i < n; i++)
{
b <<= 1;
}
return b;
}
void query(int n, int r) {
if (n == 20)return;
if (ejzz
== '1'&&tree[r << 1] == 1) {
query(n + 1, r << 1);
}
else if (ejzz
== '1'&&tree[n << 1] == 0) {
query(n + 1, r << 1 | 1);
ans = ans + p(19 - n);
}
else if (tree[r << 1 | 1]) {
query(n + 1, r << 1 | 1);
ans = ans + p(19 - n);
}
else {
query(n + 1, r << 1);
}
}

int main()
{
//freopen("E:\\data.in", "r", stdin);
ios :: sync_with_stdio(false);
while (scanf("%d", &n) != EOF)
{
int t,a,b;
init();
fur(i, 1, n) {
scanf("%d", &t);
ttt(t);
insert(0,1);
}
scanf("%d", &q);
fur(i, 1, q) {
scanf("%d%d", &a,&b);
if (a == 0) {
ttt(b);
insert(0, 1);
}
else {
tttt(b);
ans = 0;
query(0,1);
printf("%d\n",ans);
}
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: