#include <bits/stdc++.h>using namespace std;
constexpr int N = 5 + 5e5, B = 256;
int n, q, f[B][B][9];
int diff(int u, int v) { return __builtin_popcount(u^v); }
void ins(int y) {
int y1 = y >> 8 & 255, y0 = y & 255;
for (int x0 = 0; x0 < B; ++x0)
for (int l = diff(x0, y0); l <= 8; ++l)
++f[y1][x0][l];
}
int que(int x, int k) {
int x1 = x >> 8 & 255, x0 = x & 255, ans = 0;
for (int y1 = 0; y1 < B; ++y1) {
int l = k - diff(x1, y1);
if (l >= 0)
ans += f[y1][x0][min(l, 8)];
}
return ans;
}
int main() {
cin.tie(0)->sync_with_stdio(0);
cin >> n >> q;
while (n--) {
int y;
cin >> y;
ins(y);
}
while (q--) {
int x, k;
cin >> x >> k;
cout << que(x, k) << '\n';
}
return 0;
}
如果 O(n)=O(q),可以通过取 2w0=W/w 即 w0=(w−logw)/2,使复杂度达到 nwW/w+W/wqW=(n+q)wW。另外可以在原来的条件下将 f 的第三维换为树状数组,复杂度 (n+q)logwW。不过效率上一般没有收益。