提交时间:2025-10-27 13:14:01

运行 ID: 38759

#include <bits/stdc++.h> #include <bits/extc++.h> using namespace __gnu_pbds; using namespace std; #define int long long const int N = 5e5 + 5; int n, a[N], vis[N], ans; gp_hash_table<int, int> mp; int cnt; struct dat { int x, y, v; bool operator < (const dat &o) const { return v > o.v; } } b[N]; int id[N], p[N], p2[N]; signed main() { //freopen("outing.in", "r", stdin); // freopen("outing.out", "w", stdout); ios::sync_with_stdio(false); cin.tie(0), cout.tie(0); cin >> n; int lim = 0; for (int i = 1; i <= n; i++) { cin >> a[i]; lim = max(lim, __lg(a[i])); } for (int i = lim; ~i; i--) { mp.clear(); for (int j = 1; j <= n; j++) { if (vis[j]) continue; int t = a[j] % (1LL << i); if (!mp[t]) mp[t] = j; else { vis[mp[t]] = vis[j] = 1; b[++cnt] = {mp[t], j, i}; ans ^= ((a[mp[t]] ^ a[j]) - 1); mp[t] = 0; } } } for (int i = 1; i <= n; i++) if (!vis[i]) b[++cnt] = {i, i, -1}, ans ^= a[i]; sort(b + 1, b + cnt + 1); for (int i = 1; i <= cnt; i++) { // cout << i << " " << b[i].x << ' ' << b[i].y << " " << b[i].v << endl; id[b[i].x] = b[i].v, id[b[i].y] = b[i].v; p[b[i].x] = b[i].y, p[b[i].y] = b[i].x; } // cout << ans << endl; for (int k = lim; ~k; k--) { mp.clear(); for (int i = cnt, j = cnt; i; i--) { while (j && b[j].v <= b[i].v - 2) { int t = a[b[j].x] % (1LL << k); if (!mp[t]) mp[t] = b[j].x; t = a[b[j].y] % (1LL << k); if (!mp[t]) mp[t] = b[j].y; j--; } int t = a[b[i].x] % (1LL << k); if (mp[t] && !p2[b[i].x] && k > id[mp[t]]) p2[b[i].x] = mp[t]; t = a[b[i].y] % (1LL << k); if (mp[t] && !p2[b[i].y] && k > id[mp[t]]) p2[b[i].y] = mp[t]; } } // int mx = 0; for (int i = 1; i <= n; i++) { int x = p[i]; int tmp = ans; ans ^= (p[i] == i) ? a[i] : ((a[i] ^ a[p[i]]) - 1); if (p[i] == i) { cout << ans << ' '; ans = tmp; continue; } int cnt = 0; while (p2[x]) { cnt++; ans ^= (p[p2[x]] == p2[x]) ? a[p2[x]] : ((a[p2[x]] ^ a[p[p2[x]]]) - 1); ans ^= ((a[x] ^ a[p2[x]]) - 1); if (p[p2[x]] == p2[x]) break; x = p[p2[x]]; } // mx = max(mx, cnt); if (!p2[x]) ans ^= a[x]; cout << ans << ' '; ans = tmp; } //cerr<<clock()*1.0/CLOCKS_PER_SEC<<"s\n"; return 0; }