4244 平衡树练习
时间限制: 2 s 空间限制: 256000 KB 题目等级 : 钻石 Diamond
题目描述 Description
判断一些数字在一个数列中是否存在。
输入描述 Input Description
第一行输入两个正整数m和n。
第二行m个数字表示这个数列。
第三行n个数字表示需要判断的数字。
输出描述 Output Description
输出共一行n个0或1,0表示这个数字不存在,1表示存在。
样例输入 Sample Input
2 2
2 4
2 5
样例输出 Sample Output
1 0
数据范围及提示 Data Size & Hint
输入数字保证不超过MaxInt。
有节操的人不用set
//本来想写平衡树的 ,二叉排序树就A掉了
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int n,m;
#define maxn 1000000
struct Tree{
int lc,rc,val;
}tre[maxn];
int tot=0,root=0;
void Tree_Add(int &cur,int value){
if(!cur){
cur = ++tot;
tre[cur].val=value;
return ;
}
if(value<tre[cur].val) Tree_Add(tre[cur].lc,value);
else Tree_Add(tre[cur].rc,value);
}
bool Find(int now,int x){
if(!now) return 0;
if(tre[now].val==x) return 1;
if(x<tre[now].val) return Find(tre[now].lc,x);
else return Find(tre[now].rc,x);
}
int main(){
scanf("%d%d",&m,&n);
for(int x,i=1;i<=m;i++){
scanf("%d",&x);
Tree_Add(root,x);
}
for(int x,i=1;i<=n;i++){
scanf("%d",&x);
printf("%d ",Find(root,x));
}
return 0;
}