Problem p03334 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define fi first
#define se second
#define pii pair<int, int>
#define space putchar(' ')
#define enter putchar('\n')
#define mp make_pair
#define pb push_back
#define MAXN 100005
// #define ivorysi
using namespace std;
typedef long long int64;
template <class T> void read(T &res) {
  res = 0;
  T f = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    res = res * 10 + c - '0';
    c = getchar();
  }
  res *= f;
}
template <class T> void out(T x) {
  if (x < 0) {
    putchar('-');
    x = -x;
  }
  if (x >= 10)
    out(x / 10);
  putchar('0' + x % 10);
}
int N, D[2];
struct node {
  int to, next;
} E[1000005];
int head[400005], sumE, dx[] = {-1, -1, 1, 1}, dy[] = {1, -1, -1, 1};
int col[400005], cr[400005], cnt[5];
int to_int(pii p) { return p.fi * 2 * N + p.se; }
void add(int u, int v) {
  E[++sumE].to = v;
  E[sumE].next = head[u];
  head[u] = sumE;
}
void dfs(int u) {
  if (col[u] == -1)
    col[u] = 0;
  for (int i = head[u]; i; i = E[i].next) {
    int v = E[i].to;
    if (col[v] == -1) {
      col[v] = col[u] ^ 1;
      dfs(v);
    }
  }
}
int main() {
#ifdef ivorysi
  freopen("f1.in", "r", stdin);
#endif
  read(N);
  read(D[0]);
  read(D[1]);
  for (int t = 0; t <= 1; ++t) {
    memset(head, 0, sizeof(head));
    sumE = 0;
    for (int x = 0; x < 2 * N; ++x) {
      if (D[t] < x * x)
        break;
      int y = sqrt(D[t] - x * x);
      if (y * y != D[t] - x * x)
        continue;
      for (int i = 0; i < 2 * N; ++i) {
        for (int j = 0; j < 2 * N; ++j) {
          for (int k = 0; k < 4; ++k) {
            int tx = i + dx[k] * x, ty = j + dy[k] * y;
            if (tx >= 0 && tx < 2 * N && ty >= 0 && ty < 2 * N) {
              add(to_int(mp(i, j)), to_int(mp(tx, ty)));
            }
          }
        }
      }
    }
    memset(col, -1, sizeof(col));
    for (int i = 0; i < 4 * N * N; ++i) {
      if (col[i] == -1)
        dfs(i);
      cr[i] = cr[i] << 1 | col[i];
    }
  }
  for (int i = 0; i < 4 * N * N; ++i)
    ++cnt[cr[i]];
  int t = 0;
  for (int i = 1; i < 4; ++i) {
    if (cnt[i] > cnt[t])
      t = i;
  }
  int a = N * N;
  for (int i = 0; i < 4 * N * N; ++i) {
    if (!a)
      break;
    if (cr[i] == t) {
      out(i / (2 * N));
      space;
      out(i % (2 * N));
      enter;
      --a;
    }
  }
}


Fixed submission:
#include <bits/stdc++.h>
#define fi first
#define se second
#define pii pair<int, int>
#define space putchar(' ')
#define enter putchar('\n')
#define mp make_pair
#define pb push_back
#define MAXN 100005
// #define ivorysi
using namespace std;
typedef long long int64;
template <class T> void read(T &res) {
  res = 0;
  T f = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    res = res * 10 + c - '0';
    c = getchar();
  }
  res *= f;
}
template <class T> void out(T x) {
  if (x < 0) {
    putchar('-');
    x = -x;
  }
  if (x >= 10)
    out(x / 10);
  putchar('0' + x % 10);
}
int N, D[2];
struct node {
  int to, next;
} E[10000005];
int head[400005], sumE, dx[] = {-1, -1, 1, 1}, dy[] = {1, -1, -1, 1};
int col[400005], cr[400005], cnt[5];
int to_int(pii p) { return p.fi * 2 * N + p.se; }
void add(int u, int v) {
  E[++sumE].to = v;
  E[sumE].next = head[u];
  head[u] = sumE;
}
void dfs(int u) {
  if (col[u] == -1)
    col[u] = 0;
  for (int i = head[u]; i; i = E[i].next) {
    int v = E[i].to;
    if (col[v] == -1) {
      col[v] = col[u] ^ 1;
      dfs(v);
    }
  }
}
int main() {
#ifdef ivorysi
  freopen("f1.in", "r", stdin);
#endif
  read(N);
  read(D[0]);
  read(D[1]);
  for (int t = 0; t <= 1; ++t) {
    memset(head, 0, sizeof(head));
    sumE = 0;
    for (int x = 0; x < 2 * N; ++x) {
      if (D[t] < x * x)
        break;
      int y = sqrt(D[t] - x * x);
      if (y * y != D[t] - x * x)
        continue;
      for (int i = 0; i < 2 * N; ++i) {
        for (int j = 0; j < 2 * N; ++j) {
          for (int k = 0; k < 4; ++k) {
            int tx = i + dx[k] * x, ty = j + dy[k] * y;
            if (tx >= 0 && tx < 2 * N && ty >= 0 && ty < 2 * N) {
              add(to_int(mp(i, j)), to_int(mp(tx, ty)));
            }
          }
        }
      }
    }
    memset(col, -1, sizeof(col));
    for (int i = 0; i < 4 * N * N; ++i) {
      if (col[i] == -1)
        dfs(i);
      cr[i] = cr[i] << 1 | col[i];
    }
  }
  for (int i = 0; i < 4 * N * N; ++i)
    ++cnt[cr[i]];
  int t = 0;
  for (int i = 1; i < 4; ++i) {
    if (cnt[i] > cnt[t])
      t = i;
  }
  int a = N * N;
  for (int i = 0; i < 4 * N * N; ++i) {
    if (!a)
      break;
    if (cr[i] == t) {
      out(i / (2 * N));
      space;
      out(i % (2 * N));
      enter;
      --a;
    }
  }
}