Problem p03247 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define For(i, l, r) for (int i = (l), i##end = (r); i <= i##end; i++)
#define Fordown(i, r, l) for (int i = (r), i##end = (l); i >= i##end; i--)
#define debug(x) cout << #x << " = " << x << endl

using namespace std;

typedef long long ll;

template <typename T> inline bool chkmin(T &x, T y) {
  return y < x ? x = y, 1 : 0;
}
template <typename T> inline bool chkmax(T &x, T y) {
  return x < y ? x = y, 1 : 0;
}

const int INF = 0x3f3f3f3f;
const int N = 1e3 + 10, Bit = 35;

struct node {
  int x, y;
} Pos[N];
char Opt[N][Bit];
int n, cnt, Val[Bit];

inline int read() {
  int x = 0, flag = 1;
  char ch = getchar();
  while (!isdigit(ch) && ch != '-')
    ch = getchar();
  if (ch == '-')
    flag = -1, ch = getchar();
  while (isdigit(ch))
    x = (x << 3) + (x << 1) + (ch - '0'), ch = getchar();
  return x * flag;
}

inline void Work(int val) {
  int id = ++cnt;
  For(i, 1, n) {
    if (abs(Pos[i].x) > abs(Pos[i].y)) {
      if (Pos[i].x > 0) {
        Pos[i].x -= val;
        Opt[i][id] = 'R';
      } else {
        Pos[i].x += val;
        Opt[i][id] = 'L';
      }
    } else {
      if (Pos[i].y > 0) {
        Pos[i].y -= val;
        Opt[i][id] = 'U';
      } else {
        Pos[i].y += val;
        Opt[i][id] = 'D';
      }
    }
  }
}

int main() {
#ifndef ONLINE_JUDGE
  freopen("ARC103D.in", "r", stdin);
  freopen("ARC103D.out", "w", stdout);
#endif

  n = read();
  int val = -1, tot = 0;
  For(i, 1, n) {
    Pos[i].x = read(), Pos[i].y = read();
    if (val < 0)
      val = (Pos[i].x + Pos[i].y) & 1;
    else if (((Pos[i].x + Pos[i].y) & 1) != val)
      return puts("-1"), 0;
  }
  Fordown(i, 30, 0) Work(1 << i), Val[++tot] = 1 << i;
  if (!val)
    Work(1), Val[++tot] = 1;
  printf("%d\n", tot);
  For(i, 1, tot) printf("%d%c", Val[i], i == iend ? '\n' : ' ');
  For(i, 1, n) {
    For(j, 1, tot) printf("%c", Opt[i][j]);
    puts("");
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define For(i, l, r) for (int i = (l), i##end = (r); i <= i##end; i++)
#define Fordown(i, r, l) for (int i = (r), i##end = (l); i >= i##end; i--)
#define debug(x) cout << #x << " = " << x << endl

using namespace std;

typedef long long ll;

template <typename T> inline bool chkmin(T &x, T y) {
  return y < x ? x = y, 1 : 0;
}
template <typename T> inline bool chkmax(T &x, T y) {
  return x < y ? x = y, 1 : 0;
}

const int INF = 0x3f3f3f3f;
const int N = 1e3 + 10, Bit = 35;

struct node {
  int x, y;
} Pos[N];
char Opt[N][Bit];
int n, cnt, Val[Bit];

inline int read() {
  int x = 0, flag = 1;
  char ch = getchar();
  while (!isdigit(ch) && ch != '-')
    ch = getchar();
  if (ch == '-')
    flag = -1, ch = getchar();
  while (isdigit(ch))
    x = (x << 3) + (x << 1) + (ch - '0'), ch = getchar();
  return x * flag;
}

inline void Work(int val) {
  int id = ++cnt;
  For(i, 1, n) {
    if (abs(Pos[i].x) > abs(Pos[i].y)) {
      if (Pos[i].x > 0) {
        Pos[i].x -= val;
        Opt[i][id] = 'R';
      } else {
        Pos[i].x += val;
        Opt[i][id] = 'L';
      }
    } else {
      if (Pos[i].y > 0) {
        Pos[i].y -= val;
        Opt[i][id] = 'U';
      } else {
        Pos[i].y += val;
        Opt[i][id] = 'D';
      }
    }
  }
}

int main() {

  n = read();
  int val = -1, tot = 0;
  For(i, 1, n) {
    Pos[i].x = read(), Pos[i].y = read();
    if (val < 0)
      val = (Pos[i].x + Pos[i].y) & 1;
    else if (((Pos[i].x + Pos[i].y) & 1) != val)
      return puts("-1"), 0;
  }
  Fordown(i, 30, 0) Work(1 << i), Val[++tot] = 1 << i;
  if (!val)
    Work(1), Val[++tot] = 1;
  printf("%d\n", tot);
  For(i, 1, tot) printf("%d%c", Val[i], i == iend ? '\n' : ' ');
  For(i, 1, n) {
    For(j, 1, tot) printf("%c", Opt[i][j]);
    puts("");
  }
  return 0;
}