Problem p03334 (C++)

Buggy submission (status: Runtime Error):
// This amazing code is by Eric Sunli Chen.
#include <bits/stdc++.h>
using namespace std;
template <typename T> bool get_int(T &x) {
  char t = getchar();
  bool neg = false;
  x = 0;
  for (; (t > '9' || t < '0') && t != '-' && t != EOF; t = getchar())
    ;
  if (t == '-')
    neg = true, t = getchar();
  if (t == EOF)
    return false;
  for (; t <= '9' && t >= '0'; t = getchar())
    x = x * 10 + t - '0';
  if (neg)
    x = -x;
  return true;
}
template <typename T> void print_int(T x) {
  if (x < 0)
    putchar('-'), x = -x;
  short a[20] = {}, sz = 0;
  while (x > 0)
    a[sz++] = x % 10, x /= 10;
  if (sz == 0)
    putchar('0');
  for (int i = sz - 1; i >= 0; i--)
    putchar('0' + a[i]);
}
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define get1(a) get_int(a)
#define get2(a, b) (get1(a) && get1(b))
#define get3(a, b, c) (get1(a) && get2(b, c))
#define printendl(a) print_int(a), puts("")
typedef long long LL;
typedef unsigned long long uLL;
typedef pair<int, int> pii;
const int inf = 0x3f3f3f3f;
const LL Linf = 1ll << 61;
const double pi = acos(-1.0);

#define id(i, j) ((i)*n + (j))
int f1[800111], f2[800111];
int gf(int x, int *f) { return x == f[x] ? x : f[x] = gf(f[x], f); }
void un(int x, int y, int *f) { f[gf(x, f)] = gf(y, f); }

int n, d1, d2;

vector<pii> v1, v2;

int cnt[2][2], a[333][333], b[333][333];
int main() {
  get3(n, d1, d2);
  n <<= 1;
  for (int i = 0; i <= n; i++)
    for (int j = -n; j <= n; j++) {
      if (i * i + j * j == d1)
        v1.pb(mp(i, j));
      if (i * i + j * j == d2)
        v2.pb(mp(i, j));
    }
  for (int i = 0; i < n * n * 2; i++)
    f1[i] = f2[i] = i;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++) {
      int ni, nj;
      for (auto &v : v1) {
        ni = i + v.ff;
        nj = j + v.ss;
        if (ni >= 0 && ni < n && nj >= 0 && nj < n) {
          un(id(i, j) << 1, id(ni, nj) << 1 | 1, f1);
          un(id(i, j) << 1 | 1, id(ni, nj) << 1, f1);
        }
      }
      for (auto &v : v2) {
        ni = i + v.ff;
        nj = j + v.ss;
        if (ni >= 0 && ni < n && nj >= 0 && nj < n) {
          un(id(i, j) << 1, id(ni, nj) << 1 | 1, f2);
          un(id(i, j) << 1 | 1, id(ni, nj) << 1, f2);
        }
      }
    }

  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++) {
      a[i][j] = gf(id(i, j) << 1, f1) & 1;
      b[i][j] = gf(id(i, j) << 1, f2) & 1;
      cnt[a[i][j]][b[i][j]]++;
    }
  int k = n * n / 4;
  for (int k1 = 0; k1 < 2; k1++)
    for (int k2 = 0; k2 < 2; k2++)
      if (cnt[k1][k2] >= k) {
        for (int i = 0; i < n && k; i++)
          for (int j = 0; j < n && k; j++)
            if (a[i][j] == k1 && b[i][j] == k2) {
              printf("%d %d\n", i, j);
              k--;
            }
        break;
      }
  return 0;
}

Fixed submission:
// This amazing code is by Eric Sunli Chen.
#include <bits/stdc++.h>
using namespace std;
template <typename T> bool get_int(T &x) {
  char t = getchar();
  bool neg = false;
  x = 0;
  for (; (t > '9' || t < '0') && t != '-' && t != EOF; t = getchar())
    ;
  if (t == '-')
    neg = true, t = getchar();
  if (t == EOF)
    return false;
  for (; t <= '9' && t >= '0'; t = getchar())
    x = x * 10 + t - '0';
  if (neg)
    x = -x;
  return true;
}
template <typename T> void print_int(T x) {
  if (x < 0)
    putchar('-'), x = -x;
  short a[20] = {}, sz = 0;
  while (x > 0)
    a[sz++] = x % 10, x /= 10;
  if (sz == 0)
    putchar('0');
  for (int i = sz - 1; i >= 0; i--)
    putchar('0' + a[i]);
}
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define get1(a) get_int(a)
#define get2(a, b) (get1(a) && get1(b))
#define get3(a, b, c) (get1(a) && get2(b, c))
#define printendl(a) print_int(a), puts("")
typedef long long LL;
typedef unsigned long long uLL;
typedef pair<int, int> pii;
const int inf = 0x3f3f3f3f;
const LL Linf = 1ll << 61;
const double pi = acos(-1.0);

#define id(i, j) ((i)*n + (j))
int f1[800111], f2[800111];
int gf(int x, int *f) { return x == f[x] ? x : f[x] = gf(f[x], f); }
void un(int x, int y, int *f) { f[gf(x, f)] = gf(y, f); }

int n, d1, d2;

vector<pii> v1, v2;

int cnt[2][2], a[666][666], b[666][666];
int main() {
  get3(n, d1, d2);
  n <<= 1;
  for (int i = 0; i <= n; i++)
    for (int j = -n; j <= n; j++) {
      if (i * i + j * j == d1)
        v1.pb(mp(i, j));
      if (i * i + j * j == d2)
        v2.pb(mp(i, j));
    }
  for (int i = 0; i < n * n * 2; i++)
    f1[i] = f2[i] = i;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++) {
      int ni, nj;
      for (auto &v : v1) {
        ni = i + v.ff;
        nj = j + v.ss;
        if (ni >= 0 && ni < n && nj >= 0 && nj < n) {
          un(id(i, j) << 1, id(ni, nj) << 1 | 1, f1);
          un(id(i, j) << 1 | 1, id(ni, nj) << 1, f1);
        }
      }
      for (auto &v : v2) {
        ni = i + v.ff;
        nj = j + v.ss;
        if (ni >= 0 && ni < n && nj >= 0 && nj < n) {
          un(id(i, j) << 1, id(ni, nj) << 1 | 1, f2);
          un(id(i, j) << 1 | 1, id(ni, nj) << 1, f2);
        }
      }
    }

  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++) {
      a[i][j] = gf(id(i, j) << 1, f1) & 1;
      b[i][j] = gf(id(i, j) << 1, f2) & 1;
      cnt[a[i][j]][b[i][j]]++;
    }
  int k = n * n / 4;
  for (int k1 = 0; k1 < 2; k1++)
    for (int k2 = 0; k2 < 2; k2++)
      if (cnt[k1][k2] >= k) {
        for (int i = 0; i < n && k; i++)
          for (int j = 0; j < n && k; j++)
            if (a[i][j] == k1 && b[i][j] == k2) {
              printf("%d %d\n", i, j);
              k--;
            }
        break;
      }
  return 0;
}