1 条题解

  • 0
    @ 2025-5-29 19:45:07
    #include <bits/stdc++.h>
    using namespace std;
    const int maxn = 2e5 + 5;
    
    struct Node {
    	int s[2], cnt;
    } tr[maxn*20];
    
    int idx = 1;
    
    void ins(int num) {
    	int u = 1;
    	for (int d = 19; d >= 0; d--) {
    		int x = (num>>d) & 1;
    		if (!tr[u].s[x])
    			tr[u].s[x] = ++idx;
    		u = tr[u].s[x];
    		tr[u].cnt++;
    	}
    }
    
    int f(int x, int k) {
    	int u = 1, ans = 0;
    	for (int d = 19; d >= 0; d--) {
    		int kk = (k>>d) & 1;
    		int xx = (x>>d) & 1;
    		if (kk == 1) {
    			int v = tr[u].s[xx];
    			ans += tr[v].cnt;
    		}
    		u = tr[u].s[kk^xx];
    		if (!u) return ans;
    	}
    	if (u) ans += tr[u].cnt;
    	return ans;
    }
    
    int n, q, a, x, k;
    
    int main() {
    	cin >> n >> q;
    	for (int i = 0; i < n; i++) {
    		cin >> a;
    		ins(a);
    	}
    	while (q--) {
    		cin >> x >> k;
    		cout << f(x, k) << endl;
    	}
    	return 0;
    }
    
    • 1

    信息

    ID
    87
    时间
    1000ms
    内存
    256MiB
    难度
    10
    标签
    (无)
    递交数
    2
    已通过
    2
    上传者