Problem p02626 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
#define REP(i, n) for (int i = 0, _n = (int)(n); i < _n; ++i)
#define ALL(v) (v).begin(), (v).end()
#define CLR(t, v) memset(t, (v), sizeof(t))
template <class T1, class T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &a) {
  return os << "(" << a.first << "," << a.second << ")";
}
template <class T> void pv(T a, T b) {
  for (T i = a; i != b; ++i)
    cout << (*i) << " ";
  cout << endl;
}
template <class T> void chmin(T &a, const T &b) {
  if (a > b)
    a = b;
}
template <class T> void chmax(T &a, const T &b) {
  if (a < b)
    a = b;
}

ll nextLong() {
  ll x;
  scanf("%lld", &x);
  return x;
}

struct State {
  ll a;
  int i;
  int c;
};

ll solve(ll A1, ll A2, ll A3) {
  // cout << A1 << " " << A2 << " " << A3 << endl;
  if (A3 == 0) {
    if (A1 == A2)
      return 0;
    ll d = A1 - A2;
    if (d < 0)
      return -1;
    if (d % 2)
      return -1;
    return d / 2;
  }
  if (((A1 ^ A2) & 1) != (A3 & 1))
    return -1;
  if ((A1 ^ A2) == A3)
    return 0;

  ll S = A1 + A2;
  ll X = A3;

  queue<State> q;
  q.push((State){0, 61, 0});

  ll ans = -1;

  while (!q.empty()) {
    ll a = q.front().a;
    int i = q.front().i;
    int c = q.front().c;
    q.pop();

    if (a > A1)
      continue;
    if (i == -1) {
      if (c)
        continue;
      if (!(1 <= a && a <= A1))
        continue;
      if (a + (a ^ X) != S)
        continue;
      chmax(ans, a);
      continue;
    }

    int s = (S >> i & 1);
    int x = (X >> i & 1);

    if (x == 0) {
      ll na = a;
      if (c == 1) {
        na |= (1LL << i);
      }
      int nc = s;
      q.push({na, i - 1, nc});
    } else if (x == 1) {
      if (c == 1 && s == 1)
        return -1;
      if (c == 0 && s == 0)
        return -1;
      ll na = a | (1LL << i);

      q.push({na, i - 1, c});

      if (a + (1LL << i) * 2 > A1)
        q.push({a, i - 1, c});
    }
  }

  if (ans == -1)
    return -1;
  return A1 - ans;
}

int main2() {
  int N = nextLong();
  vector<ll> A(N);
  REP(i, N) A[i] = nextLong();
  for (int i = 3; i < N; i++)
    A[2] ^= A[i];

  ll ans = solve(A[0], A[1], A[2]);
  cout << ans << endl;
  return 0;
}

int main() {

#ifdef LOCAL
  for (; !cin.eof(); cin >> ws)
#endif
    main2();
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
#define REP(i, n) for (int i = 0, _n = (int)(n); i < _n; ++i)
#define ALL(v) (v).begin(), (v).end()
#define CLR(t, v) memset(t, (v), sizeof(t))
template <class T1, class T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &a) {
  return os << "(" << a.first << "," << a.second << ")";
}
template <class T> void pv(T a, T b) {
  for (T i = a; i != b; ++i)
    cout << (*i) << " ";
  cout << endl;
}
template <class T> void chmin(T &a, const T &b) {
  if (a > b)
    a = b;
}
template <class T> void chmax(T &a, const T &b) {
  if (a < b)
    a = b;
}

ll nextLong() {
  ll x;
  scanf("%lld", &x);
  return x;
}

struct State {
  ll a;
  int i;
  int c;
};

ll solve(ll A1, ll A2, ll A3) {
  // cout << A1 << " " << A2 << " " << A3 << endl;
  if (A3 == 0) {
    if (A1 == A2)
      return 0;
    ll d = A1 - A2;
    if (d < 0)
      return -1;
    if (d % 2)
      return -1;
    return d / 2;
  }
  if (((A1 ^ A2) & 1) != (A3 & 1))
    return -1;
  if ((A1 ^ A2) == A3)
    return 0;

  ll S = A1 + A2;
  ll X = A3;

  queue<State> q;
  q.push((State){0, 61, 0});

  ll ans = -1;

  while (!q.empty()) {
    ll a = q.front().a;
    int i = q.front().i;
    int c = q.front().c;
    q.pop();

    if (a > A1)
      continue;
    if (i == -1) {
      if (c)
        continue;
      if (!(1 <= a && a <= A1))
        continue;
      if (a + (a ^ X) != S)
        continue;
      chmax(ans, a);
      continue;
    }

    int s = (S >> i & 1);
    int x = (X >> i & 1);

    if (x == 0) {
      ll na = a;
      if (c == 1) {
        na |= (1LL << i);
      }
      int nc = s;
      q.push({na, i - 1, nc});
    } else if (x == 1) {
      if (c == 1 && s == 1)
        return -1;
      if (c == 0 && s == 0)
        return -1;
      ll na = a | (1LL << i);
      q.push({na, i - 1, c});
      q.push({a, i - 1, c});
    }
  }

  if (ans == -1)
    return -1;
  return A1 - ans;
}

int main2() {
  int N = nextLong();
  vector<ll> A(N);
  REP(i, N) A[i] = nextLong();
  for (int i = 3; i < N; i++)
    A[2] ^= A[i];

  ll ans = solve(A[0], A[1], A[2]);
  cout << ans << endl;
  return 0;
}

int main() {

#ifdef LOCAL
  for (; !cin.eof(); cin >> ws)
#endif
    main2();
  return 0;
}