Codevs 4244 平衡树练习

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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

七情六欲·

学生党不容易~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值