Problem p03334 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (int i = (a); i <= (b); ++i)
#define rrp(i, a, b) for (int i = (a); i >= (b); --i)
#define gc() getchar()
#define fir first
#define sec second
typedef pair<int, int> pii;
typedef long double db;
typedef long long ll;
template <typename tp> inline void read(tp &x) {
  x = 0;
  char tmp;
  bool key = 0;
  for (tmp = gc(); !isdigit(tmp); tmp = gc())
    key = (tmp == '-');
  for (; isdigit(tmp); tmp = gc())
    x = (x << 3) + (x << 1) + (tmp ^ '0');
  if (key)
    x = -x;
}
template <typename tp> inline void ckmn(tp &x, tp y) { x = x < y ? x : y; }
template <typename tp> inline void ckmx(tp &x, tp y) { x = x < y ? y : x; }

const int N = 610;
int id[N][N], cnt, d1, d2, bel[2][N], num[2][2], n, rec[N * N];
int uni[N], dis[N];
int getfa(int pos) {
  if (pos == uni[pos])
    return pos;
  int ret = getfa(uni[pos]);
  dis[pos] ^= dis[uni[pos]];
  return uni[pos] = ret;
}
void merge(int x, int y) {
  if (getfa(x) != getfa(y)) {
    int tmp = dis[x] ^ dis[y] ^ 1;
    x = getfa(x), y = getfa(y);
    dis[x] = tmp;
    uni[x] = y;
  }
}
void doit(int d, int *b) {
  rep(i, 1, cnt) uni[i] = i, dis[i] = 0;
  rep(i, 0, n - 1) rep(j, 0, n - 1) {
    rep(x, 0, n) if (x * x <= d && rec[d - x * x] != -1) {
      int y = rec[d - x * x];
      if (i + x < n && j + y < n)
        merge(id[i][j], id[i + x][j + y]);
      if (i + x < n && j - y >= 0)
        merge(id[i][j], id[i + x][j - y]);
      if (i + y < n && j + x < n)
        merge(id[i][j], id[i + y][j + x]);
      if (i + y < n && j - x >= 0)
        merge(id[i][j], id[i + y][j - x]);
    }
  }
  rep(i, 1, cnt) {
    getfa(i);
    b[i] = dis[i];
  }
}
int main() {
  read(n);
  read(d1), read(d2);
  n = n * 2;
  rep(i, 0, n - 1) rep(j, 0, n - 1) id[i][j] = ++cnt;
  memset(rec, -1, sizeof rec);
  rep(i, 0, n) rec[i * i] = i;
  doit(d1, bel[0]);
  doit(d2, bel[1]);
  rep(i, 1, cnt)++ num[bel[0][i]][bel[1][i]];
  rep(a, 0, 1) rep(b, 0, 1) if (num[a][b] >= n * n / 4) {
    int cur = 0;
    rep(i, 0, n - 1) rep(j, 0, n - 1) {
      if (bel[0][id[i][j]] == a && bel[1][id[i][j]] == b) {
        if (cur < n * n / 4)
          printf("%d %d\n", i, j), ++cur;
      }
    }
    return 0;
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (int i = (a); i <= (b); ++i)
#define rrp(i, a, b) for (int i = (a); i >= (b); --i)
#define gc() getchar()
#define fir first
#define sec second
typedef pair<int, int> pii;
typedef long double db;
typedef long long ll;
template <typename tp> inline void read(tp &x) {
  x = 0;
  char tmp;
  bool key = 0;
  for (tmp = gc(); !isdigit(tmp); tmp = gc())
    key = (tmp == '-');
  for (; isdigit(tmp); tmp = gc())
    x = (x << 3) + (x << 1) + (tmp ^ '0');
  if (key)
    x = -x;
}
template <typename tp> inline void ckmn(tp &x, tp y) { x = x < y ? x : y; }
template <typename tp> inline void ckmx(tp &x, tp y) { x = x < y ? y : x; }

const int N = 610;
int id[N][N], cnt, d1, d2, bel[2][N * N], num[2][2], n, rec[N * N];
int uni[N * N], dis[N * N];
int getfa(int pos) {
  if (pos == uni[pos])
    return pos;
  int ret = getfa(uni[pos]);
  dis[pos] ^= dis[uni[pos]];
  return uni[pos] = ret;
}
void merge(int x, int y) {
  if (getfa(x) != getfa(y)) {
    int tmp = dis[x] ^ dis[y] ^ 1;
    x = getfa(x), y = getfa(y);
    dis[x] = tmp;
    uni[x] = y;
  }
}
void doit(int d, int *b) {
  rep(i, 1, cnt) uni[i] = i, dis[i] = 0;
  rep(i, 0, n - 1) rep(j, 0, n - 1) {
    rep(x, 0, n) if (x * x <= d && rec[d - x * x] != -1) {
      int y = rec[d - x * x];
      if (i + x < n && j + y < n)
        merge(id[i][j], id[i + x][j + y]);
      if (i + x < n && j - y >= 0)
        merge(id[i][j], id[i + x][j - y]);
      if (i + y < n && j + x < n)
        merge(id[i][j], id[i + y][j + x]);
      if (i + y < n && j - x >= 0)
        merge(id[i][j], id[i + y][j - x]);
    }
  }
  rep(i, 1, cnt) {
    getfa(i);
    b[i] = dis[i];
  }
}
int main() {
  read(n);
  read(d1), read(d2);
  n = n * 2;
  rep(i, 0, n - 1) rep(j, 0, n - 1) id[i][j] = ++cnt;
  memset(rec, -1, sizeof rec);
  rep(i, 0, n) rec[i * i] = i;
  doit(d1, bel[0]);
  doit(d2, bel[1]);
  rep(i, 1, cnt)++ num[bel[0][i]][bel[1][i]];
  rep(a, 0, 1) rep(b, 0, 1) if (num[a][b] >= n * n / 4) {
    int cur = 0;
    rep(i, 0, n - 1) rep(j, 0, n - 1) {
      if (bel[0][id[i][j]] == a && bel[1][id[i][j]] == b) {
        if (cur < n * n / 4)
          printf("%d %d\n", i, j), ++cur;
      }
    }
    return 0;
  }
  return 0;
}