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

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

#define int long long
int sum, tmpa;
const long long LINF = 1e13;
typedef pair<int, int> P;
typedef pair<P, int> PI;
map<PI, int> m;
int solve(int A, int B, int bit) {
  if (A < 0)
    return LINF;
  if (m.count(PI(P(A, B), bit)))
    return m[PI(P(A, B), bit)];
  if (!bit)
    return 0;
  if ((sum ^ A ^ B) & bit) {
    if (sum & bit) {
      if (B & bit) {
        int move = A - bit + 1;
        if (~(B + move) & bit)
          return LINF;
        return m[PI(P(A, B), bit)] =
                   move + solve((A - move) & ~bit, (B + move) & ~bit, bit >> 1);
      }
      int move = bit - B;
      return m[PI(P(A, B), bit)] =
                 move + solve((A - move) & ~bit, (B + move) & ~bit, bit >> 1);
    } else {
      if (B & bit)
        return LINF;
      int move = A - bit + 1;
      if ((B + move) & bit) {
        move = bit - B;
        if (~(A - move) & bit)
          return LINF;
      }
      return m[PI(P(A, B), bit)] =
                 move + solve((A - move) & ~bit, (B + move) & ~bit, bit >> 1);
    }
  }
  if (sum & bit) {
    if (B & bit)
      return m[PI(P(A, B), bit)] = solve(A & ~bit, B & ~bit, bit >> 1);
    int tmp = solve(A & ~bit, B & ~bit, bit >> 1);
    if (tmp < tmpa)
      return m[PI(P(A, B), bit)] = tmp;
    int move = max(A - bit + 1, bit - B);
    return m[PI(P(A, B), bit)] =
               move + solve((A - move) & ~bit, (B + move) & ~bit, bit >> 1);
  }
  return m[PI(P(A, B), bit)] = solve(A & ~bit, B & ~bit, bit >> 1);
}
int solve2(int A, int B) {
  for (int i = 0; i < A; i++)
    if (((A - i) ^ (B + i) ^ sum) == 0)
      return i;
  return -1;
}

signed main() {

  int n;
  cin >> n;
  int A, B;
  cin >> A >> B;
  tmpa = A;
  int a;
  for (int i = 2; i < n; i++) {
    scanf("%ld", &a);
    sum ^= a;
  }
  int ans = solve(A, B, 1LL << 39);
  if (ans >= A)
    ans = -1;
  cout << ans << endl;

  /*
  for(int i=0;i<1000;i++){
    int A=rand()%100000+1,B=rand()%LINF+1;tmpa=A;
    sum=rand()%LINF;
    int ans=solve(A,B,0,1LL<<39),ans2=solve2(A,B);
    if(ans>=tmpa)ans=-1;
    if(ans!=ans2)cout<<A<<" "<<B<<" "<<sum<<" "<<ans<<" "<<ans2<<endl;
  }
  */
}


Error: TLE

Fixed submission:

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

#define int long long
int sum, tmpa;
const long long LINF = 1e13;
int solve(int A, int B, int ans, int bit) {
  if (A < 0 || ans >= tmpa)
    return LINF;
  if (!bit)
    return ans;
  if ((sum ^ A ^ B) & bit) {
    if (sum & bit) {
      if (B & bit) {
        int move = A - bit + 1;
        if (~(B + move) & bit)
          return LINF;
        return solve((A - move) & ~bit, (B + move) & ~bit, ans + move,
                     bit >> 1);
      }
      int move = bit - B;
      return solve((A - move) & ~bit, (B + move) & ~bit, ans + move, bit >> 1);
    } else {
      if (B & bit)
        return LINF;
      int move = A - bit + 1;
      if ((B + move) & bit) {
        move = bit - B;
        if (~(A - move) & bit)
          return LINF;
      }
      return solve((A - move) & ~bit, (B + move) & ~bit, ans + move, bit >> 1);
    }
  }
  if (sum & bit) {
    if (B & bit)
      return solve(A & ~bit, B & ~bit, ans, bit >> 1);
    int tmp = solve(A & ~bit, B & ~bit, ans, bit >> 1);
    if (tmp < tmpa)
      return tmp;
    int move = max(A - bit + 1, bit - B);
    return solve((A - move) & ~bit, (B + move) & ~bit, ans + move, bit >> 1);
  }
  return solve(A & ~bit, B & ~bit, ans, bit >> 1);
}
int solve2(int A, int B) {
  for (int i = 0; i < A; i++)
    if (((A - i) ^ (B + i) ^ sum) == 0)
      return i;
  return -1;
}

signed main() {

  int n;
  cin >> n;
  int A, B;
  cin >> A >> B;
  tmpa = A;
  int a;
  for (int i = 2; i < n; i++) {
    scanf("%ld", &a);
    sum ^= a;
  }
  int ans = solve(A, B, 0, 1LL << 39);
  if (ans >= A)
    ans = -1;
  cout << ans << endl;

  /*
  for(int i=0;i<1000;i++){
    int A=rand()%100000+1,B=rand()%LINF+1;tmpa=A;
    sum=rand()%LINF;
    int ans=solve(A,B,0,1LL<<39),ans2=solve2(A,B);
    if(ans>=tmpa)ans=-1;
    if(ans!=ans2)cout<<A<<" "<<B<<" "<<sum<<" "<<ans<<" "<<ans2<<endl;
  }
  */
}