题目链接:
https://pintia.cn/problem-sets/1120858298571182080/problems/1120858493769895936
参考博客:
https://blog.csdn.net/fengkuangdewoniudada/article/details/61928920
package pat树;
//ac
//参考博客
//https://blog.csdn.net/fengkuangdewoniudada/article/details/61928920
import java.util.Scanner;
public class Main3堆中的路径 {
static int n;
static int m;
static int arr[];
public static void swap(int i,int j) {
int temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
public static void change(int i) {
while(i>1) {
if(arr[i/2]>arr[i]) swap(i/2,i);
else return;
i/=2;
}
}
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
n=in.nextInt();
m=in.nextInt();
arr=new int[n+1];
for(int i=1;i<=n;i++) {
arr[i]=in.nextInt();
if(i>1)change(i);
}
for(int i=0;i<m;i++) {
int a=in.nextInt();
while(a>=1) {
if(a==1)System.out.println(arr[a]);
else System.out.print(arr[a]+" ");
a/=2;
}
}
}
}