Problem p03334 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define sz(a) int(a.size())
#define clr(a) memset(a, 0, sizeof(a))
#define all(a) a.begin(), a.end()
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, int> pli;
const int inf = 1e9;
const ll Inf = 1e18;
const int N = 610;
const int mod = 0;
int gi() {
  int x = 0, o = 1;
  char ch = getchar();
  while ((ch < '0' || ch > '9') && ch != '-')
    ch = getchar();
  if (ch == '-')
    o = -1, ch = getchar();
  while (ch >= '0' && ch <= '9')
    x = x * 10 + ch - '0', ch = getchar();
  return x * o;
}
template <typename T> bool chkmax(T &a, T b) { return a < b ? a = b, 1 : 0; };
template <typename T> bool chkmin(T &a, T b) { return a > b ? a = b, 1 : 0; };
int add(int a, int b) { return a + b >= mod ? a + b - mod : a + b; }
int sub(int a, int b) { return a - b < 0 ? a - b + mod : a - b; }
void inc(int &a, int b) { a = (a + b >= mod ? a + b - mod : a + b); }
void dec(int &a, int b) { a = (a - b < 0 ? a - b + mod : a - b); }
int n, d1, d2, id[N][N], col[N][2], tot = 0;
pii p[N * N];
vector<pii> E[2];
void work(int d, int t) {
  for (int i = 0; i <= n && d - i * i >= 0; i++) {
    int j = sqrt(d - i * i);
    if (i * i + j * j != d)
      continue;
    E[t].pb(mp(i, j));
    E[t].pb(mp(-i, j));
    E[t].pb(mp(i, -j));
    E[t].pb(mp(-i, -j));
  }
}
bool in(int x) { return 0 <= x && x <= n; }
void dfs(int u, int t) {
  for (pii d : E[t]) {
    int x = p[u].fi + d.fi, y = p[u].se + d.se, v;
    if (!in(x) || !in(y) || ~col[v = id[x][y]][t])
      continue;
    col[v][t] = col[u][t] ^ 1, dfs(v, t);
  }
}
int main() {
  n = gi(), d1 = gi(), d2 = gi(), n = 2 * n - 1;
  for (int i = 0; i <= n; i++)
    for (int j = 0; j <= n; j++)
      id[i][j] = ++tot, p[tot] = mp(i, j);
  work(d1, 0), work(d2, 1);
  memset(col, -1, sizeof(col));
  for (int i = 1; i <= tot; i++)
    for (int j = 0; j < 2; j++)
      if (col[i][j] == -1)
        col[i][j] = 1, dfs(i, j);
  n = (n + 1) >> 1;
  for (int i = 1, cnt = 0; i <= tot; i++) {
    if (col[i][0] && col[i][1]) {
      ++cnt;
      printf("%d %d\n", p[i].fi, p[i].se);
      if (cnt == n * n)
        break;
    }
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define sz(a) int(a.size())
#define clr(a) memset(a, 0, sizeof(a))
#define all(a) a.begin(), a.end()
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, int> pli;
const int inf = 1e9;
const ll Inf = 1e18;
const int N = 610;
const int mod = 0;
int gi() {
  int x = 0, o = 1;
  char ch = getchar();
  while ((ch < '0' || ch > '9') && ch != '-')
    ch = getchar();
  if (ch == '-')
    o = -1, ch = getchar();
  while (ch >= '0' && ch <= '9')
    x = x * 10 + ch - '0', ch = getchar();
  return x * o;
}
template <typename T> bool chkmax(T &a, T b) { return a < b ? a = b, 1 : 0; };
template <typename T> bool chkmin(T &a, T b) { return a > b ? a = b, 1 : 0; };
int add(int a, int b) { return a + b >= mod ? a + b - mod : a + b; }
int sub(int a, int b) { return a - b < 0 ? a - b + mod : a - b; }
void inc(int &a, int b) { a = (a + b >= mod ? a + b - mod : a + b); }
void dec(int &a, int b) { a = (a - b < 0 ? a - b + mod : a - b); }
int n, d1, d2, id[N][N], col[N * N][2], tot = 0;
pii p[N * N];
vector<pii> E[2];
void work(int d, int t) {
  for (int i = 0; i <= n && d - i * i >= 0; i++) {
    int j = sqrt(d - i * i);
    if (i * i + j * j != d)
      continue;
    E[t].pb(mp(i, j));
    E[t].pb(mp(-i, j));
    E[t].pb(mp(i, -j));
    E[t].pb(mp(-i, -j));
  }
}
bool in(int x) { return 0 <= x && x <= n; }
void dfs(int u, int t) {
  for (pii d : E[t]) {
    int x = p[u].fi + d.fi, y = p[u].se + d.se, v;
    if (!in(x) || !in(y) || ~col[v = id[x][y]][t])
      continue;
    col[v][t] = col[u][t] ^ 1, dfs(v, t);
  }
}
int main() {
  n = gi(), d1 = gi(), d2 = gi(), n = 2 * n - 1;
  for (int i = 0; i <= n; i++)
    for (int j = 0; j <= n; j++)
      id[i][j] = ++tot, p[tot] = mp(i, j);
  work(d1, 0), work(d2, 1);
  memset(col, -1, sizeof(col));
  for (int i = 1; i <= tot; i++)
    for (int j = 0; j < 2; j++)
      if (col[i][j] == -1)
        col[i][j] = 1, dfs(i, j);
  n = (n + 1) >> 1;
  for (int i = 1, cnt = 0; i <= tot; i++) {
    if (col[i][0] && col[i][1]) {
      ++cnt;
      printf("%d %d\n", p[i].fi, p[i].se);
      if (cnt == n * n)
        break;
    }
  }
  return 0;
}