Advertisement
willy108

Magic xor basis code

Jul 8th, 2022
1,155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.78 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define rep(i, a, b) for(int i = a; i < (b); ++i)
  5. #define trav(a, x) for(auto& a : x)
  6. #define all(x) begin(x), end(x)
  7. #define sz(x) (int)(x).size()
  8. #define subnb true
  9. #define Lnb true
  10. typedef long long ll;
  11. typedef long double ld;
  12. typedef pair<int, int> pii;
  13. typedef pair<ll, ll> pll;
  14. typedef vector<int> vi;
  15.  
  16. const int N = (int)1e5 + 50, B = 60;
  17.  
  18. struct Ele {
  19.     ll x;
  20.     int tim;
  21. };
  22.  
  23. int n;
  24. vector<Ele> upd[N];
  25. ll a[N];
  26. map<ll, int> las;
  27. ll bas[B], sz = 0;
  28. int pos[B];
  29. int res[N];
  30. ll pw[B + 1];
  31.  
  32. int main(){
  33.     ios::sync_with_stdio(false);
  34.     cin.tie(NULL);
  35.  
  36.     pw[0] = 1;
  37.     rep(i, 1, B + 1) pw[i] = pw[i - 1] * 2;
  38.     cin >> n;
  39.     rep(i, 0, n) {
  40.         cin >> a[i];
  41.         if(!las.count(a[i]) || las[a[i]] == -1) {
  42.             las[a[i]] = i;
  43.         } else {
  44.             upd[las[a[i]]].push_back({a[i], i - 1});
  45.             las[a[i]] = -1;
  46.         }
  47.     }
  48.     for (auto &p : las) {
  49.         if(p.second != -1) upd[p.second].push_back({p.first, n - 1});
  50.     }
  51.     for(int i = 0; i < n; i++) {
  52.         for (auto [x, nowpos] : upd[i]) {
  53. //            cout << i << " " << nowpos << ": " << x << endl;
  54.             for(int j = B - 1; j >= 0; j--) {
  55.                 if(x & (1LL << j)) {
  56.                     if(!bas[j]) {
  57.                         bas[j] = x; pos[j] = nowpos;
  58.                         break;
  59.                     }
  60.                     if(pos[j] < nowpos) {
  61.                         swap(x, bas[j]);
  62.                         swap(nowpos, pos[j]);
  63.                     }
  64.                     x ^= bas[j];
  65.                 }
  66.             }
  67.         }
  68.         rep(j, 0, B) {
  69.             res[i] += bas[j] && pos[j] >= i;
  70.         }
  71.     }
  72.     for(int i = 0; i < n; i++) cout << pw[res[i]] << "\n";
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement