#include<stdio.h>
#include<stdlib.h>
#define MaxData 1000;
typedef struct HeapStruct *MaxHeap;
typedef int ElementType;
struct HeapStruct
{
ElementType *Elements;
int Size;
int Capacity;
};
MaxHeap CreatHeap(int Capacity)
{
MaxHeap p = (MaxHeap)malloc(sizeof(struct HeapStruct));
p->Elements = (ElementType*)malloc(sizeof(ElementType) * (Capacity + 1));
p->Size = 0;
p->Capacity = Capacity;
p->Elements[0] = MaxData;
return p;
}
bool isFull(MaxHeap h)
{
return (h->Size == h->Capacity);
}
bool insertHeap(MaxHeap heap,ElementType item)
{
int i;
if(isFull(heap))
{
printf("最大栈已满\n");
return false;
}
i = ++heap->Size;
for( ; heap->Elements[i/2] < item; i/=2)
{
heap->Elements[i] = heap->Elements[i/2];
}
heap->Elements[i] = item;
return true;
}
bool isEmpty(MaxHeap h)
{
return (h->Size == 0);
}
ElementType DeleteMax(MaxHeap h)
{
int Parent,Child;
ElementType X,MaxItem;
if(isEmpty(h))
{
printf("最大栈已空\n");
return 0;
}
MaxItem = h->Elements[1];
X = h->Elements[h->Size--];
for(Parent = 1;Parent *2 <= h->Size;Parent = Child)
{
Child = Parent * 2;
if((Child!=h->Size) && (h->Elements[Child] < h->Elements[Child + 1]))
{
Child++;
}
if(X >= h->Elements[Child])
break;
else
h->Elements[Parent] = h->Elements[Child];
}
h->Elements[Parent] = X;
return MaxItem;
}
void PercDown(MaxHeap h,int t)
{
int Parent,Child;
int X;
X = h->Elements[t];
for(Parent = t;Parent *2 <=h->Size;Parent = Child)
{
Child = Parent *2;
if((Child!=h->Size) && (h->Elements[Child] < h->Elements[Child + 1]))
{
Child++;
}
if(X >= h->Elements[Child])
break;
else
h->Elements[Parent] = h->Elements[Child];
}
h->Elements[Parent] = X;
}
void sortHeap(MaxHeap h)
{
for(int i =h->Size/2;i >0 ;i--)
PercDown(h,i);
}
void printPath(MaxHeap h,int t)
{
int flag = 0;
for(int i =t;i > 0;i /= 2)
{
if(flag)
printf(" ");
printf("%d",h->Elements[i]);
flag = 1;
}
printf("\n");
}
int main()
{
int N,M;
int list[20];
ElementType item;
MaxHeap h = CreatHeap(20);
scanf("%d",&N);
scanf("%d",&M);
for(int i = 0;i < N;i++)
{
scanf("%d",&item);
insertHeap(h,item);
}
for(int i = 0;i < M;i++)
{
scanf("%d",&list[i]);
}
for(int i =0;i < M;i++)
printPath(h,list[i]);
return 0;
}