模板集合

template<typename T,int P=10005,typename Cmp=less<T>>
class Heap{
  private:
    T h[P]; Cmp cmp; int tot;
    inline void Swap(T &pa,T &pb)
      {T t=pa; pa=pb,pb=t;}
    inline void Up(int pla){
      int nv=pla,d=nv/2;
      while(d)
        if(cmp(h[nv],h[d]))
          Swap(h[nv],h[d]),nv=d,d=nv/2;
        else break;
    }
    inline void Down(int pla){
      int nv=pla,d=nv*2;
      while(d<=tot){
        if(d<tot&&cmp(h[d+1],h[d])) d++;
        if(cmp(h[d],h[nv]))
          Swap(h[d],h[nv]),nv=d,d=nv*2;
        else break;
      }
    }
  public:
    inline void clear()
      {memset(h,0,sizeof h),tot=0;}
    inline bool empty()
      {return tot<=0;}
    inline void push(T x)
      {h[++tot]=x,Up(tot);}
    inline void pop()
      {h[1]=h[tot--],Down(1);}
    inline T top()
      {return h[1];}
    inline int size()
      {return tot;}
};
posted @ 2023-04-07 21:31  IOIAK_wanguan  阅读(22)  评论(0)    收藏  举报