40亿个数,如果用无符号的long long数组来存,那么使用数组里的每一个元素的每一位代表一个数,具体为:
a[0] ---- 0~63
a[1] ---- 64~127
a[2] ---- 128~190
...
那么,40亿 bit/64 = 6.25*107 *8 byte = 500MB , 内存就满足了。
#include <iostream>
using namespace std;
#define maxn 62500000
unsigned long long a[maxn];
int main() {
unsigned n, m, x;
while (cin >> n >> m) {
memset(a, 0, sizeof(a));
for (int i=0; i<n; i++) {
cin >> x;
a[ x>>5 ] |= 1 << (x & 0x3F);
}
for (int i=0; i<m; i++) {
cin >> x;
if ( a[ x>>5 ] & ( 1 << (x & 0x3F) ) ) {
cout << "Yes" << endl;
} else {
cout << "No" << endl;
}
}
}
}