1 条题解
-
0
信友队第2题
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 5; struct Node { int s[2]; long long cnt[2], sum[2]; int sz; Node() { sz = 0; s[0] = s[1] = 0; cnt[0] = cnt[1] = 0; } } tr[maxn*20]; int cnt; int n, m; long long a[maxn], p; void init() { tr[cnt = 1] = Node(); tr[0].sz = 0; tr[1].sz = 0; } void ins(int a) { for (int u = 1, i = 32; i >= 0; i--) { tr[u].sz++; int x = ((1ll << i-1) & a) ? 1 : 0; if (!tr[u].s[x]) { tr[u].s[x] = ++cnt; tr[cnt] = Node(); } if (x) { int ls = tr[u].s[0]; tr[u].cnt[0] += tr[ls].sz; } else { int rs = tr[u].s[1]; tr[u].cnt[1] += tr[rs].sz; } u = tr[u].s[x]; } } long long tot[33][2]; void dfs(int u, int d) { if (!u) return; for (int i = 0; i < 2; i++) { tot[d][i] += tr[u].cnt[i]; // if (tr[u].cnt[i]) cout << "\td = " << d << " : " << tr[u].cnt[i] << endl; } for (int i = 0; i < 2; i++) dfs(tr[u].s[i], d-1); } int main() { freopen("xor.in", "r", stdin); freopen("xor.out", "w", stdout); init(); scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) { scanf("%d", a+i); ins(a[i]); } dfs(1, 31); // for (int i = 31; i >= 0; i--) // printf("tot[%2d][0] = %5lld ; tot[%2d][1] = %5lld\n", i, tot[i][0], i, tot[i][1]); while (m--) { scanf("%lld", &p); long long ans = 0; for (int i = 31; i >= 0; i--) ans += (p & (1ll << i)) ? tot[i][0] : tot[i][1]; printf("%lld\n", ans); } return 0; }
- 1
信息
- ID
- 10
- 时间
- 1000ms
- 内存
- 256MiB
- 难度
- (无)
- 标签
- (无)
- 递交数
- 0
- 已通过
- 0
- 上传者