设根为1。小于当前节点则走向2*i,大于当前节点则走向2*i+1,比较数组是否相同即可。
注意:数组要初始化。记录最大下标值可以减少循环长度。
#include<iostream>
#include<cstring>
using namespace std;
const int N = 1e5+10;
int a[N], b[N], max1, max2;
int index(int x, int c[], int k) {
int r = 1;
while(c[r]) {
if(x < c[r]) r = r * 2;
else r = r*2+1;
}
if(k) max1 = max(max1, r);
else max2 = max(max2, r);
return r;
}
int main() {
int n, l;
while(cin >> n, n) {
max1 = 1;
cin >> l;
memset(a, 0, sizeof a);
for(int i = 1; i <= n; ++ i) {
int x;
cin >> x;
a[index(x, a, 1)] = x;
}
while(l --) {
max2 = 1;
memset(b, 0, sizeof b);
for(int i = 1; i <= n; ++ i) {
int x;
cin >> x;
b[index(x, b, 0)] = x;
}
bool flag = true;//cout << max1 << " " << max2 << endl;
if(max1!=max2) flag = false;
else {
for(int i = 1; i <= max1; ++ i) {
// cout << a[i] << ' ' << b[i] << endl;
if(a[i]!=b[i]) {
flag = false;
break;
}
}
}
if(flag) puts("Yes");
else puts("No");
}
}
return 0;
}