Problem p03247 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define all(V) V.begin(), V.end()
#define MOD 1000000007

ll dat[1010][3];
ll arms[41];
short B[41];
string F[1010];

int main() {
  arms[0] = arms[1] = 1;
  for (int i = 2; i < 35; i++) {
    arms[i] = arms[i - 1] * 2;
  }
  ll S = (1LL << 34);
  int N;
  cin >> N;
  ll x, y;
  short z = 2;
  for (int i = 0; i < N; i++) {
    cin >> x >> y;
    dat[i][0] = x;
    dat[i][1] = y;
    if ((x + y) & 1) {
      if (z == 0)
        z = 5;
      if (z == 2)
        z = 1;
    } else {
      if (z == 1)
        z = 5;
      if (z == 2)
        z = 0;
    }
  }
  if (z == 5) {
    cout << -1 << endl;
    return 0;
  }

  if (z == 1)
    arms[0] = 0, S--;

  bool xc, yc;
  string ans;
  for (int i = 0; i < N; i++) {
    x = dat[i][0];
    if (x < 0)
      xc = 1, x = -x;
    else
      xc = 0;

    y = dat[i][1];
    if (y < 0)
      yc = 1, y = -y;
    else
      yc = 0;

    ll bt = (S - x - y) / 2;
    int bc = 1;

    B[0] = 6;
    for (int j = 1; j < 35; j++) {
      if (bt & 1)
        B[j] = 1;
      else
        B[j] = 0;
      bt /= 2;
    }

    for (int j = 0; j < 35; j++) {
      if (x & (1LL << j)) {
        if (B[j + 1]) {
          B[j + 1] = 5;
          x += (1LL << j);
        } else {
          B[j + 1] = 4;
          x -= (1LL << j);
        }
      } else {
        B[j + 1] += 6;
      }
    }

    ans = "";
    for (int j = 0; j < 35; j++) {
      if (arms[0] == 0)
        continue;
      if (B[j] == 4) {
        if (xc)
          ans += 'L';
        else
          ans += 'R';
      } else if (B[j] == 5) {
        if (xc)
          ans += 'R';
        else
          ans += 'L';
      } else if (B[j] == 6) {
        if (yc)
          ans += 'D';
        else
          ans += 'U';
      } else if (B[j] == 7) {
        if (yc)
          ans += 'U';
        else
          ans += 'D';
      } else
        ans += 'X';
    }
    F[i] = ans;
  }

  if (arms[0] == 0) {
    cout << 34 << endl;
    for (int i = 1; i < 35; i++) {
      cout << arms[i];
      if (i == 34)
        cout << endl;
      else
        cout << " ";
    }
    for (int j = 0; j < N; j++) {
      cout << F[j] << endl;
    }

    // VERIFY

    for (int i = 0; i < N; i++) {
      ans = F[i];
      x = 0, y = 0;
      for (int j = 0; j < 34; j++) {
        if (ans[j] == 'L')
          x -= arms[j + 1];
        if (ans[j] == 'R')
          x += arms[j + 1];
        if (ans[j] == 'U')
          y += arms[j + 1];
        if (ans[j] == 'D')
          y -= arms[j + 1];
      }
      if (x != dat[i][0] || y != dat[i][1]) {
        cout << "WA" << endl;
        cout << x << " " << y << endl;
        assert(0);
      }
    }

  } else {
    cout << 35 << endl;
    for (int i = 0; i < 35; i++) {
      cout << arms[i];
      if (i == 34)
        cout << endl;
      else
        cout << " ";
    }
    for (int j = 0; j < N; j++) {
      cout << F[j] << endl;
    }

    for (int i = 0; i < N; i++) {
      ans = F[i];
      x = 0, y = 0;
      for (int j = 0; j < 35; j++) {
        if (ans[j] == 'L')
          x -= arms[j];
        if (ans[j] == 'R')
          x += arms[j];
        if (ans[j] == 'U')
          y += arms[j];
        if (ans[j] == 'D')
          y -= arms[j];
      }
      if (x != dat[i][0] || y != dat[i][1]) {
        cout << x << " " << y << endl;
        cout << "WA" << endl;
        assert(0);
      }
    }
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define all(V) V.begin(), V.end()
#define MOD 1000000007

ll dat[1010][3];
ll arms[41];
short B[41];
string F[1010];

int main() {
  arms[0] = arms[1] = 1;
  for (int i = 2; i < 35; i++) {
    arms[i] = arms[i - 1] * 2;
  }
  ll S = (1LL << 34);
  int N;
  cin >> N;
  ll x, y;
  short z = 2;
  for (int i = 0; i < N; i++) {
    cin >> x >> y;
    dat[i][0] = x;
    dat[i][1] = y;
    if ((x + y) & 1) {
      if (z == 0)
        z = 5;
      if (z == 2)
        z = 1;
    } else {
      if (z == 1)
        z = 5;
      if (z == 2)
        z = 0;
    }
  }
  if (z == 5) {
    cout << -1 << endl;
    return 0;
  }

  if (z == 1)
    arms[0] = 0, S--;

  bool xc, yc;
  string ans;
  for (int i = 0; i < N; i++) {
    x = dat[i][0];
    if (x < 0)
      xc = 1, x = -x;
    else
      xc = 0;

    y = dat[i][1];
    if (y < 0)
      yc = 1, y = -y;
    else
      yc = 0;

    ll bt = (S - x - y) / 2;
    int bc = 1;

    B[0] = 6;
    for (int j = 1; j < 35; j++) {
      if (bt & 1)
        B[j] = 1;
      else
        B[j] = 0;
      bt /= 2;
    }

    for (int j = 0; j < 35; j++) {
      if (x & (1LL << j)) {
        if (B[j + 1]) {
          B[j + 1] = 5;
          x += (1LL << j);
        } else {
          B[j + 1] = 4;
          x -= (1LL << j);
        }
      } else {
        B[j + 1] += 6;
      }
    }

    ans = "";
    for (int j = 0; j < 35; j++) {
      if (arms[j] == 0)
        continue;
      if (B[j] == 4) {
        if (xc)
          ans += 'L';
        else
          ans += 'R';
      } else if (B[j] == 5) {
        if (xc)
          ans += 'R';
        else
          ans += 'L';
      } else if (B[j] == 6) {
        if (yc)
          ans += 'D';
        else
          ans += 'U';
      } else if (B[j] == 7) {
        if (yc)
          ans += 'U';
        else
          ans += 'D';
      } else
        ans += 'X';
    }
    F[i] = ans;
  }

  if (arms[0] == 0) {
    cout << 34 << endl;
    for (int i = 1; i < 35; i++) {
      cout << arms[i];
      if (i == 34)
        cout << endl;
      else
        cout << " ";
    }
    for (int j = 0; j < N; j++) {
      cout << F[j] << endl;
    }

    // VERIFY

    for (int i = 0; i < N; i++) {
      ans = F[i];
      x = 0, y = 0;
      for (int j = 0; j < 34; j++) {
        if (ans[j] == 'L')
          x -= arms[j + 1];
        if (ans[j] == 'R')
          x += arms[j + 1];
        if (ans[j] == 'U')
          y += arms[j + 1];
        if (ans[j] == 'D')
          y -= arms[j + 1];
      }
      if (x != dat[i][0] || y != dat[i][1]) {
        cout << "WA" << endl;
        cout << x << " " << y << endl;
        assert(0);
      }
    }

  } else {
    cout << 35 << endl;
    for (int i = 0; i < 35; i++) {
      cout << arms[i];
      if (i == 34)
        cout << endl;
      else
        cout << " ";
    }
    for (int j = 0; j < N; j++) {
      cout << F[j] << endl;
    }

    for (int i = 0; i < N; i++) {
      ans = F[i];
      x = 0, y = 0;
      for (int j = 0; j < 35; j++) {
        if (ans[j] == 'L')
          x -= arms[j];
        if (ans[j] == 'R')
          x += arms[j];
        if (ans[j] == 'U')
          y += arms[j];
        if (ans[j] == 'D')
          y -= arms[j];
      }
      if (x != dat[i][0] || y != dat[i][1]) {
        cout << x << " " << y << endl;
        cout << "WA" << endl;
        assert(0);
      }
    }
  }
}