Problem p02914 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

using namespace std;

#define forn(i, n) for (int64_t i = 0; i < (int64_t)(n); ++i)
#define endl '\n'

template <typename T> void _dbg(const char *_s, T _h) {
  cerr << _s << " = " << _h << "\n";
}

template <typename T, typename... Ts>
void _dbg(const char *_s, T _h, Ts... _t) {
  for (int _b = 0; ((_b += *_s == '(') -= *_s == ')') != 0 || *_s != ',';)
    cerr << *_s++;
  cerr << " = " << _h << ",";
  _dbg(_s + 1, _t...);
}

#define int long long

#ifdef LOCAL
#define dbg(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
#else
#define dbg(...)
#endif

const int N = 500 * 1000 + 17;
const int MOD = 1000 * 1000 * 1000 + 7;

int n;
int a[N], c[N];

bool read() {
  if (!(cin >> n))
    return false;

  forn(i, n) cin >> a[i];

  return true;
}

int getrank(int mask, int nn) {
  for (int b = 59; b >= 0; --b) {
    int j = -1;

    forn(i, nn) if ((c[i] >> b) == 1) {
      if (j == -1)
        j = i;
      else
        c[i] ^= c[j];
    }
  }

  int res = 0;

  forn(i, nn) res += (c[i] != 0);

  return res;
}

bool check(int mask) {
  forn(i, n) c[i] = (a[i] & mask);

  auto r1 = getrank(mask, n);

  forn(i, n) c[i] = (a[i] & mask);

  c[n] = mask;
  auto r2 = getrank(mask, n + 1);
  return r1 == r2;
}

void solve() {
  int mask = 0, ans = 0;

  for (int b = 59; b >= 0; --b) {
    int cnt = 0;

    forn(i, n) cnt += ((a[i] >> b) & 1);

    if (cnt > 0 && cnt % 2 == 0) {
      mask |= (1ll << b);

      if (!check(mask))
        mask ^= (1ll << b);
    } else if (cnt > 0) {
      ans += (1ll << b);
    }
  }

  cout << ans + 2ll * mask << endl;
}

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);

  while (read())
    solve();

  return 0;
}


Fixed submission:
#pragma GCC optimize("O3")

#include <bits/stdc++.h>

using namespace std;

#define forn(i, n) for (int64_t i = 0; i < (int64_t)(n); ++i)
#define endl '\n'

template <typename T> void _dbg(const char *_s, T _h) {
  cerr << _s << " = " << _h << "\n";
}

template <typename T, typename... Ts>
void _dbg(const char *_s, T _h, Ts... _t) {
  for (int _b = 0; ((_b += *_s == '(') -= *_s == ')') != 0 || *_s != ',';)
    cerr << *_s++;
  cerr << " = " << _h << ",";
  _dbg(_s + 1, _t...);
}

#define int long long

#ifdef LOCAL
#define dbg(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
#else
#define dbg(...)
#endif

const int N = 500 * 1000 + 17;
const int MOD = 1000 * 1000 * 1000 + 7;

int n;
int a[N], c[N];

bool read() {
  if (!(cin >> n))
    return false;

  forn(i, n) cin >> a[i];

  return true;
}

int getrank(int mask, int nn) {
  for (int b = 59; b >= 0; --b) {
    int j = -1;

    forn(i, nn) if ((c[i] >> b) == 1) {
      if (j == -1)
        j = i;
      else
        c[i] ^= c[j];
    }
  }

  int res = 0;

  forn(i, nn) res += (c[i] != 0);

  return res;
}

bool check(int mask) {
  forn(i, n) c[i] = (a[i] & mask);

  auto r1 = getrank(mask, n);

  forn(i, n) c[i] = (a[i] & mask);

  c[n] = mask;
  auto r2 = getrank(mask, n + 1);
  return r1 == r2;
}

void solve() {
  int mask = 0, ans = 0;

  for (int b = 59; b >= 0; --b) {
    int cnt = 0;

    forn(i, n) cnt += ((a[i] >> b) & 1);

    if (cnt > 0 && cnt % 2 == 0) {
      mask |= (1ll << b);

      if (!check(mask))
        mask ^= (1ll << b);
    } else if (cnt > 0) {
      ans += (1ll << b);
    }
  }

  cout << ans + 2ll * mask << endl;
}

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);

  while (read())
    solve();

  return 0;
}