4244 平衡树练习

 

思路:

  有节操的人不用set也不用map;

 

代码:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;

#define maxn 5000005

int n,m,ch[maxn][2],key[maxn],root;

inline void in(int &now)
{
    int if_z=1;now=0;
    char Cget=getchar();
    while(Cget>'9'||Cget<'0')
    {
        if(Cget=='-') if_z=-1;
        Cget=getchar();
    }
    while(Cget>='0'&&Cget<='9')
    {
        now=now*10+Cget-'0';
        Cget=getchar();
    }
    now*=if_z;
}

inline int tree_build(int l,int r)
{
    int now=l+r>>1;
    if(now>l) ch[now][0]=tree_build(l,now-1);
    if(now<r) ch[now][1]=tree_build(now+1,r);
    return now;
}

inline bool find(int x)
{
    int now=root;
    while(1)
    {
        if(x==key[now]) return true;
        if(x<key[now]) now=ch[now][0];
        else now=ch[now][1];
        if(!now) return false;
    }
}

int main()
{
    in(n),in(m);int pos;
    for(int i=1;i<=n;i++) in(key[i]);
    sort(key+1,key+n+1);
    n=unique(key+1,key+n+1)-key-1;
    root=tree_build(1,n);
    for(;m--;) in(pos),printf("%d ",find(pos));
    return 0;
}

 

相关文章:

  • 2021-10-22
  • 2021-10-08
  • 2021-08-21
  • 2021-12-25
  • 2021-12-14
  • 2021-11-10
  • 2021-09-25
猜你喜欢
  • 2021-07-09
  • 2021-11-02
  • 2021-05-25
  • 2021-10-30
  • 2021-07-30
  • 2021-12-01
  • 2021-10-07
相关资源
相似解决方案